本篇记录了Ubuntu
系统通过APT
安装MySQL Server
的具体配置过程。
环境
系统环境 | 版本 |
---|---|
操作系统 | Ubuntu 18.04 |
Mysql | 5.7 |
APT安装MySQL Server
sudo apt install -y mysql-server
添加root用户
- 获取初始用户和密码
➜ ~ sudo cat /etc/mysql/debian.cnf
# Automatically generated for Debian scripts. DO NOT TOUCH!
[client]
host = localhost
user = debian-sys-maint
password = wIKGi0O8NfBjZsF6
socket = /var/run/mysqld/mysqld.sock
[mysql_upgrade]
host = localhost
user = debian-sys-maint
password = wIKGi0O8NfBjZsF6
socket = /var/run/mysqld/mysqld.sock
- 使用初始用户连接MySQL Server
mysql -u debian-sys-maint -p
- 检测
root
用户
使用下面命令可以看到,root
用户密码为空。这是由于此时root
用户使用了auth_socket
插件,该插件不关心密码,它只检查用户是否使用UNIX
套接字进行连接,然后比较用户名。
mysql> SELECT User, Host, HEX(authentication_string) FROM mysql.user;
+------------------+-----------+------------------------------------------------------------------------------------+
| User | Host | HEX(authentication_string) |
+------------------+-----------+------------------------------------------------------------------------------------+
| root | localhost | |
| mysql.session | localhost | 2A5448495349534E4F544156414C494450415353574F52445448415443414E42455553454448455245 |
| mysql.sys | localhost | 2A5448495349534E4F544156414C494450415353574F52445448415443414E42455553454448455245 |
| debian-sys-maint | localhost | 2A42424642344139433038464535433736463846423033334133344131463631423431383332354243 |
+------------------+-----------+------------------------------------------------------------------------------------+
5 rows in set (0.00 sec)
mysql> SELECT User, Plugin FROM mysql.user;
+------------------+-----------------------+
| User | Plugin |
+------------------+-----------------------+
| root | auth_socket |
| mysql.session | mysql_native_password |
| mysql.sys | mysql_native_password |
| debian-sys-maint | mysql_native_password |
+------------------+-----------------------+
5 rows in set (0.00 sec)
- 配置
root
用户
如果要配置密码,需要在同一命令中同时更改插件并设置密码。
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456';
设置root用户的任意地址访问
此时的root
用户仅仅支持localhost
的访问方式,想支持任意地址的访问还需执行以下操作
- 新增允许任何主机连接的
root
用户
grant all privileges on *.* to 'root'@'%' identified by '123456' with grant option;
flush privileges;
- 修改
/etc/mysql/mysql.conf.d/mysqld.cnf
,找到以下语句并注释掉
# bind-address = 127.0.0.1
- 重启
MySQL
服务
sudo service mysql restart
网友评论