Ubuntu安装Mysql很简单,但是也会遇到一些问题。
我的Ubuntu版本是18.04.1,安装mysql版本是5.7。
Ubuntu安装MySQL的步骤如下:
1. sudo apt-get install mysql-server
2. apt-get install mysql-client
3. sudo apt-get install libmysqlclient-dev
安装以后,通过mysql --version命令查看版本信息:
mysql Ver 14.14 Distrib 5.7.26, for Linux (x86_64) using EditLine wrapper
mysql是安装成功了,但是直接输入mysql,却显示没有权限:
ERROR 1045 (28000): Access denied for user 'qingyuan'@'localhost' (using password: NO)
根据网上的建议,查看mysql到底有没有启动:
sudo netstat -tap |grep mysql
输出显示是成功的:
tcp 0 0 localhost:mysql 0.0.0.0:* LISTEN 4892/mysqld
继续跟随网上建议,关闭mysql,然后重启
systemctl restart mysqld
输出如下:
Failed to start mysqld.service: Unit mysqld.service not found.
输出显示:没有mysqld.service这个单元。
输入mysqld命令,输出如下:
mysqld: Can't change dir to '/var/lib/mysql/' (Errcode: 13 - Permission denied)
2019-07-01T13:44:44.228936Z 0 [Warning] Changed limits: max_open_files: 1024 (requested 5000)
2019-07-01T13:44:44.229209Z 0 [Warning] Changed limits: table_open_cache: 431 (requested 2000)
2019-07-01T13:44:44.479228Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2019-07-01T13:44:44.596275Z 0 [Warning] Can't create test file /var/lib/mysql/ubuntu.lower-test
2019-07-01T13:44:44.597278Z 0 [Note] mysqld (mysqld 5.7.26-0ubuntu0.18.04.1) starting as process 3561 ...
2019-07-01T13:44:44.905374Z 0 [Warning] Can't create test file /var/lib/mysql/ubuntu.lower-test
2019-07-01T13:44:44.905433Z 0 [Warning] Can't create test file /var/lib/mysql/ubuntu.lower-test
2019-07-01T13:44:44.936710Z 0 [Warning] One can only use the --user switch if running as root
2019-07-01T13:44:44.937074Z 0 [ERROR] failed to set datadir to /var/lib/mysql/
2019-07-01T13:44:44.937156Z 0 [ERROR] Aborting
2019-07-01T13:44:44.939133Z 0 [Note] Binlog end
2019-07-01T13:44:44.958845Z 0 [Note] mysqld: Shutdown complete
但是,切换到root后,却可以直接访问mysql了
这到底是为什么?
这得从Ubuntu下安装高版本的MySql有关。在Ubuntu上安装MySQL时,对于root用户,可以设置空密码。
输入如下指令查看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 | 2A33383636414146424141463045393833383341423435353142373433363739333531383934423043 |
+------------------+-----------+------------------------------------------------------------------------------------+
我们看到,root的密码确实为空。
试着改变root的密码,输入如下指令:
ALTER USER 'root'@'localhost' IDENTIFIED BY 'test';
输出为:
Query OK, 0 rows affected (0.00 sec)
然后发现,root的密码仍然为空,继续尝试:
SET PASSWORD FOR 'root'@'localhost' = PASSWORD('test');
输出:
Query OK, 0 rows affected, 2 warnings (0.02 sec)
仍然没有成功,并且有两个警告。我们看看警告是什么:
show warnings;
输出:
+---------+------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Level | Code | Message |
+---------+------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Warning | 1287 | 'SET PASSWORD FOR = PASSWORD('')' is deprecated and will be removed in a future release. Please use SET PASSWORD FOR = '' instead |
| Note | 1699 | SET PASSWORD has no significance for user 'root'@'localhost' as authentication plugin does not support it. |
+---------+------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
2 rows in set (0.04 sec)
输出显示当前使用的授权插件不支持修改密码,那我们看一下当前的授权插件是什么:
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 |
+------------------+-----------------------+
我们看到,root采用的是授权插件是auth_socket,也即通过套接字进行鉴权。如果你安装的版本是5.7,并且没有为root用户提供密码(安装时并不需要),那么就会使用auth_socket插件,这个插件不关心也不需要密码,它只是通过UNIX套接字的用户名进行验证,如果验证通过,那么就鉴权成功。这就是root用户可以正常使用mysql的原因,因为root用户在mysql的user表中。
如果我们想配置密码,那么就需要同时改变插件并且设置密码。如果是先改变插件,然后设置密码就会不起作用。插件也会回滚到auth_socket。
输入如下命令:
update mysql.user set authentication_string=PASSWORD('123456'), plugin='mysql_native_password' where user='root';
输出如下:
Query OK, 1 row affected, 1 warning (0.09 sec)
Rows matched: 1 Changed: 1 Warnings: 1
查看操作后的用户信息
mysql> SELECT User, Host, HEX(authentication_string) ,plugin FROM mysql.user;
+------------------+-----------+------------------------------------------------------------------------------------+-----------------------+
| User | Host | HEX(authentication_string) | plugin |
+------------------+-----------+------------------------------------------------------------------------------------+-----------------------+
| root | localhost | 2A36424234383337454237343332393130354545343536384444413744433637454432434132414439 | mysql_native_password |
| mysql.session | localhost | 2A5448495349534E4F544156414C494450415353574F52445448415443414E42455553454448455245 | mysql_native_password |
| mysql.sys | localhost | 2A5448495349534E4F544156414C494450415353574F52445448415443414E42455553454448455245 | mysql_native_password |
| debian-sys-maint | localhost | 2A33383636414146424141463045393833383341423435353142373433363739333531383934423043 | mysql_native_password |
+------------------+-----------+------------------------------------------------------------------------------------+-----------------------+
此时root用户确实有了密码 。我们查看一下warning是什么:
mysql> show warnings;
+---------+------+-------------------------------------------------------------------+
| Level | Code | Message |
+---------+------+-------------------------------------------------------------------+
| Warning | 1681 | 'PASSWORD' is deprecated and will be removed in a future release. |
+---------+------+-------------------------------------------------------------------+
1 row in set (0.00 sec)
警告显示,PASSWORD被废弃并且在未来版本中要被移除。可以通过如下命令
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456';
退出mysql,重启mysql,退出root用户,切换到普通用户,用设置的密码123456,root用户登录mysql。
mysql -u root -p
然后输入密码:123456,登录成功:
qingyuan@ubuntu:~$ mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.26-0ubuntu0.18.04.1 (Ubuntu)
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
参考资料
1. Ubuntu18.04安装MySQL后普通用户无法登录
2. Change user password in MySQL 5.7 with “plugin: auth_socket”