本文介绍MySQL 在 Windows系统和Linux系统安装方法。
Windows系统安装MySQL1、下载
选择要下载的MySQL版本
2、解压zip包
解压下载的zip压缩包
3、配置环境变量
在Path下添加D:\tools\mysql-8.0.16-winx64\bin
4、编写配置文件
在安装根目录下添加my-default.ini文件:D:\tools\mysql-8.0.16-winx64\my-default.ini
[mysqld]# 3306端口port=3306# mysql的安装目录basedir=D:\tools\mysql-8.0.16-winx64# 设置mysql数据库的数据的存放目录datadir=D:\tools\mysql-8.0.16-winx64\data# 允许最大连接数max_connections=200# 允许连接失败的次数,防止有人从该主机试图攻击数据库系统max_connect_errors=10# 服务端使用的字符集默认为UTF8character-set-server=utf8# 创建新表时将使用的默认存储引擎default-storage-engine=INNODB# 默认使用“mysql_native_password”插件认证default_authentication_plugin=mysql_native_password[mysql]# 设置mysql客户端默认字符集default-character-set=utf8[client]# 设置mysql客户端连接服务端时默认使用的端口port=3306default-character-set=utf85、MySQL服务安装mysqld –install MySQLmysqld install
报错1:
信息如下:
Install/Remove of the Service Denied
解决办法:
打开cmd.exe程序的时候选择“用管理员身份打开”。
报错2:
C:\WINDOWS\system32>mysqld –install MySQLThe service already exists!The current server installed: D:\xampp\mysql\bin\mysqld MySQL
解决办法:
mysqld -remove MySQL6、MySQL初始化
在MySQL安装目录的 bin 目录下执行命令:
mysqld –initialize –console
执行完成后,会打印 root 用户的初始默认密码:
C:\WINDOWS\system32>mysqld –initialize –console2019-05-15T09:27:48.338369Z 0 [System] [MY-013169] [Server] D:\tools\mysql-8.0.16-winx64\bin\mysqld.exe (mysqld 8.0.16) initializing of server in progress as process 153082019-05-15T09:28:50.242794Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: u#vTkx6L:dg,2019-05-15T09:29:42.201421Z 0 [System] [MY-013170] [Server] D:\tools\mysql-8.0.16-winx64\bin\mysqld.exe (mysqld 8.0.16) initializing of server has completed
启动MySQL服务
net start mysql7、更改密码和密码认证插件
进入D:\tools\mysql-8.0.16-winx64\bin路径下,执行
mysql -u root -p
然后输入第6步的密码:u#vTkx6L:dg
然后修改用户密码,在MySQL中执行命令:
ALTER USER ‘root’@’localhost’ IDENTIFIED WITH mysql_native_password BY ‘新密码’;8、查看默认数据库mysql
初始化完成后,初始化了名为mysql数据库,其中user表里面存储MySQL用户信息。
mysql> show databases;mysql> use mysql;mysql> show tables;
# 显示user表结构mysql> desc user;mysql> select user,host,authentication_string from mysql.user;
添加新的用户
除了管理员root用户,也可以添加其他用户。
允许本地 IP访问localhost的Mysql数据库mysql> create user ‘admin’@’localhost’ identified by ‘admin’;Query OK, 0 rows affected (0.22 sec)允许外网IP访问数据库
允许开放其他ip登录
mysql> create user ‘admin’@’%’ identified by ‘admin’; Query OK, 0 rows affected (0.08 sec)mysql> GRANT ALL PRIVILEGES ON e_menu.* TO ‘admin’@’%’ WITH GRANT OPTION; //将数据库e_menu授权给admin用户 Query OK, 0 rows affected (0.15 sec)mysql> flush privileges; //刷新授权Query OK, 0 rows affected (0.05 sec)
注意:上面的授权命令适用于MySQL8.0及之后的版本,MySQL8.0之前的版本使用如下命令授权
授权基本的查询修改权限:
查看用户权限
查看MYSQL数据库中所有用户
mysql> SELECT DISTINCT CONCAT(‘User: ”’,user,”’@”’,host,”’;’) AS query FROM mysql.user; ————————————— | query | ————————————— | User: ‘admin’@’%’; || User: ‘admin’@’localhost’; || User: ‘mysql.infoschema’@’localhost’; || User: ‘mysql.session’@’localhost’; || User: ‘mysql.sys’@’localhost’; || User: ‘root’@’localhost’; | ————————————— 6 rows in set (0.04 sec)mysql>Linux系统安装MySQL:Docker安装
Linux系统推荐使用docker安装,简单方便。如果没有安装docker,需要先安装一下,可参考容器技术介绍:Docker简介及安装
1、查看可用的 MySQL 版本
先查看一下可以安装的 MySQL 版本,MySQL镜像的可用版本可在docker hub中搜索查看:https://hub.docker.com/_/mysql?tab=tags
2、拉取 MySQL 镜像
拉取指定版本的MySQL
$ docker pull mysql:5.7
查看是否安装MySQL镜像
$ docker images[root@server ~]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEjenkins/jenkins latest 10fafa8484ea 4 weeks ago 721MBnginx latest f6d0b4767a6c 4 weeks ago 133MBmysql 5.7 9cfcce23593a 8 months ago 448MB3、运行容器
运行 mysql 容器:
$ docker run -itd –name mysql-test -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql:5.7
–name mysql-test:设置容器名
-p 3306:3306 :映射容器服务的 3306 端口到宿主机的 3306 端口。
MYSQL_ROOT_PASSWORD=123456:设置 MySQL 服务 root 用户的密码。
查看MySQL镜像是否启动成功:
$ docker ps[root@server ~]# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES2c5ae10a7543 mysql:5.7 “docker-entrypoint.s…” 7 months ago Up 23 minutes 0.0.0.0:3306->3306/tcp, 33060/tcp mysql-test4、进入mysql容器$ docker exec -it 容器ID或者容器名 /bin/bash[root@server ~]# docker exec -it mysql-test /bin/bashroot@2c5ae10a7543:/# exitexit[root@server ~]# docker exec -it 2c5ae10a7543 /bin/bashroot@2c5ae10a7543:/#
通过 root 和密码 123456 访问 MySQL 服务
方法一
$ docker inspect mysql-test
方法二
[root@server local]# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES3bab7ac2a2af jenkins/jenkins:lts “/sbin/tini — /usr/…” 9 hours ago Up 9 hours 0.0.0.0:8080->8080/tcp, 0.0.0.0:50000->50000/tcp jenkins2c5ae10a7543 mysql:5.7 “docker-entrypoint.s…” 6 days ago Up 8 hours 0.0.0.0:3306->3306/tcp, 33060/tcp mysql-test[root@server local]# docker exec -it mysql-test bashroot@2c5ae10a7543:/# cat /etc/hosts127.0.0.1 localhost::1 localhost ip6-localhost ip6-loopbackfe00::0 ip6-localnetff00::0 ip6-mcastprefixff02::1 ip6-allnodesff02::2 ip6-allrouters172.17.0.3 2c5ae10a7543root@2c5ae10a7543:/#
命令行连接mysql
root@2c5ae10a7543:/# mysql -h 172.17.0.3 -u root -pEnter password: Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 8Server version: 5.7.30 MySQL Community Server (GPL)Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.mysql>6、增加远程访问docker MySQL权限
在步骤4中,可以进入mysql容器后连接mysql数据库,那么怎么在外面(我的docker mysql安装在centos虚拟机中,想在宿主机上连接docker安装的mysql)也可以连接mysql容器呢?
前面已经讲过授权其他IP访问mysql,这里类似。先进入docker mysql容器内,连接mysql数据库,然后对root账户进行授权:
root@2c5ae10a7543:/# mysql -u root -pEnter password: Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 3Server version: 5.7.30 MySQL Community Server (GPL)Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.mysql> GRANT ALL PRIVILEGES ON *.* TO ‘root’@’%’ IDENTIFIED BY ‘123456’ WITH GRANT OPTION;Query OK, 0 rows affected, 1 warning (0.66 sec)mysql>
授权成功后,在宿主机上连接虚拟机上docker安装的mysql:
C:\WINDOWS\system32>mysql -h 192.168.30.8 -P 3306 -u root -pEnter password: ******Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 4Server version: 5.7.30 MySQL Community Server (GPL)Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.mysql> select user,host,authentication_string from mysql.user; ————— ———– ——————————————- | user | host | authentication_string | ————— ———– ——————————————- | root | localhost | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 || mysql.session | localhost | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE || mysql.sys | localhost | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE || root | % | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | ————— ———– ——————————————- 4 rows in set (0.11 sec)mysql>
连接成功!
数据库管理工具连接MySQL
除了通过命令行连接管理数据库,为了更加方便管理mysql数据库,有很多数据库管理工具可以通过GUI界面管理数据库,下面介绍数据库管理工具MySQL Workbench连接数据库方法。
1. 连接宿主机安装的mysql
安装成功后打开Workbench,添加连接,设置连接名,配置主机名,用户名。
输入密码,连接成功,可以看到数据库
2. 连接虚拟机安装的docker mysql
添加连接,设置连接名,配置主机名,用户名
输入密码,连接成功
也可以使用Navicat工具连接数据库,连接方法类似