admin

Ubuntu 18.04+ apt install 安装 Mysql
老套路,安装命令如下:$ sudo apt update $ sudo apt install mysql-ser...
扫描右侧二维码阅读全文
17
2019/09

Ubuntu 18.04+ apt install 安装 Mysql

老套路,安装命令如下:

$ sudo apt update
$ sudo apt install mysql-server
$ sudo mysql_secure_installation

注意第三条安装命令sudo mysql_secure_installation,这条命令是用来安装Mysql的一个安全脚本工具。
Ubuntu 18.04中以上命令运行完,可以发现,并没有像Ubuntu 14.04那样让我们在安装的时候输入Mysql的密码,所以此时的Mysql是没有密码的。

此时,我们运行如下脚本:

$ sudo mysql_secure_installation

运行时会出现一系列的提示,比如设置是否允许远程连接(貌似无效,还是要手动修改配置)、是否删除test库、设置root用户密码(设置后依旧无法直接登录,还需要进一步修改,后面介绍)、设置用户密码强度等。

之所以前面设置密码无效,是因为在Ubuntu 18.04中,MySQL 5.7及以上版本默认是用auth_socket插件验证的而不是密码验证,所以我们需要修改验证方法为mysql_native_password

首先,执行如下命令:

$ mysql

查看用户的验证方法:

mysql> SELECT user,authentication_string,plugin,host FROM mysql.user;

比如下面这样:

+------------------+-------------------------------------------+-----------------------+-----------+
| user             | authentication_string                     | plugin                | host      |
+------------------+-------------------------------------------+-----------------------+-----------+
| root             |                                           | auth_socket           | localhost |
| mysql.session    | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost |
| mysql.sys        | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost |
| debian-sys-maint | *CC744277A401A7D25BE1CA89AFF17BF607F876FF | mysql_native_password | localhost |
+------------------+-------------------------------------------+-----------------------+-----------+
4 rows in set (0.00 sec)

可以看到root用户的验证方式为auth_socketauthentication_string为空。

继续运行如下命令改变验证方法及登录密码(注意:此命令会改变之前运行脚本时设置的密码):

mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '你的密码';

继续执行:

mysql> FLUSH PRIVILEGES;

使以上修改生效。

检查修改是否生效:

mysql> SELECT user,authentication_string,plugin,host FROM mysql.user;

输出结果:

+------------------+-------------------------------------------+-----------------------+-----------+
| user             | authentication_string                     | plugin                | host      |
+------------------+-------------------------------------------+-----------------------+-----------+
| root             | *3636DACC8616D997782ADD0839F92C1571D6D78F | mysql_native_password | localhost |
| mysql.session    | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost |
| mysql.sys        | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost |
| debian-sys-maint | *CC744277A401A7D25BE1CA89AFF17BF607F876FF | mysql_native_password | localhost |
+------------------+-------------------------------------------+-----------------------+-----------+
4 rows in set (0.00 sec)

可以看到root用户已经改为mysql_native_password验证。

Last modification:September 17th, 2019 at 06:52 pm
If you think my article is useful to you, please feel free to appreciate

Leave a Comment