在Ubuntu18.04通过apt-get安装mysql服务

ub64@ub64-1804-1:~$ sudo apt-get install mysql-client-core-5.7

ub64@ub64-1804-1:~$ sudo apt-get install mysql-server

安装完成,中间过程并没有提示设置mysql的默认密码。不管了,先用root尝试登陆mysql试试。

ub64@ub64-1804-1:~$ mysql
ERROR 1045 (28000): Access denied for user 'ub64'@'localhost' (using password: NO)

ub64@ub64-1804-1:~$ mysql -u root
ERROR 1698 (28000): Access denied for user 'root'@'localhost'

ub64@ub64-1804-1:~$ mysql -uub64
ERROR 1045 (28000): Access denied for user 'ub64'@'localhost' (using password: NO)

ub64@ub64-1804-1:~$ mysql -uub64 -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'ub64'@'localhost' (using password: YES)

提示报错了,不管是root还是本机用户名,都无法登陆。测试一下重启服务吧,还是不行。

ub64@ub64-1804-1:~$ service mysql start
==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-units ===
Authentication is required to start 'mysql.service'.
Authenticating as: ub64-1804-1 (ub64)
Password:
==== AUTHENTICATION COMPLETE ===

ub64@ub64-1804-1:~$ mysql
ERROR 1045 (28000): Access denied for user 'ub64'@'localhost' (using password: NO)
ub64@ub64-1804-1:~$ ps -ef | grep mysql
mysql    14285     1  0 05:14 ?        00:00:00 /usr/sbin/mysqld --daemonize --pid-file=/run/mysqld/mysqld.pid

进程服务运行正常,但是还是登陆不上。祭出百度神器,找到同样的问题。

https://www.cnblogs.com/super-zhangkun/p/9435974.html

好了,打开conf配置文件,查看一下默认设置的账号密码

ub64@ub64-1804-1:~$ sudo view /etc/mysql/debian.cnf
# Automatically generated for Debian scripts. DO NOT TOUCH!
[client]
host     = localhost
user     = debian-sys-maint
password = i1OVIwwgMWs8MAKj
socket   = /var/run/mysqld/mysqld.sock
[mysql_upgrade]
host     = localhost
user     = debian-sys-maint
password = i1OVIwwgMWs8MAKj
socket   = /var/run/mysqld/mysqld.sock

原来安装完成后,默认使用的不再是以前的root了,而是使用debian-sys-maint用户和这一大串随机密码,顺利登陆。查询一下现有的用户情况。


mysql> select host, user, plugin, authentication_string from mysql.user;
+-----------+------------------+-----------------------+-------------------------------------------+
| host      | user             | plugin                | authentication_string                     |
+-----------+------------------+-----------------------+-------------------------------------------+
| localhost | root             |                       |                                         |
| localhost | mysql.session    | mysql_native_password | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| localhost | mysql.sys        | mysql_native_password | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| localhost | debian-sys-maint | mysql_native_password | *2D3B703CBCBE5E317141B9EC7FA780ABB991F93E |
+-----------+------------------+-----------------------+-------------------------------------------+
4 rows in set (0.00 sec)

接下来为了使用方便,那就是修改一下默认的root账号的登陆设置了。由于mysql5.7的密码存储在authentication_string字段中,password()方法设置登陆密码123456。好吧,虽然不知道什么意思,但是和debian-sys-maint一样,修改默认的认证方式为"mysql_native_password"。

mysql> update mysql.user set authentication_string=password('123456') where user='root'and Host = 'localhost';
Query OK, 0 rows affected, 1 warning (0.00 sec)
Rows matched: 1  Changed: 0  Warnings: 1

mysql> update mysql.user set plugin="mysql_native_password" where user='root'and Host = 'localhost';
Query OK, 0 rows affected (0.00 sec)
Rows matched: 1  Changed: 0  Warnings: 0

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

测试使用root用户登录完成,ok。

主要转载、修改参考来源:https://www.cnblogs.com/super-zhangkun/p/9435974.html

10-06 15:48