Ubuntu启动数据库失败(不知道初始密码惹的祸)

前面费尽心思终于装好了Linux环境下的MySQL,今天终于有时间,激动的打开了虚拟机,输入命令:

chujun@chujun:~$ sudo service mysql start
[sudo] password for chujun: 
chujun@chujun:~$ mysql -u root -p
Enter password: 
ERROR 1698 (28000): Access denied for user 'root'@'localhost'

然后我 Ubuntu启动数据库失败(不知道初始密码惹的祸)_第1张图片 我的数据库呢?不应该是输入密码后给我欢迎的回应吗???为啥给我的是这个?

然后打开百度可以寻找前辈们的帮助。

这个Roobtyan前辈的文章让我了解了,原来是因为安装MySQL时程序没有提醒我输入密码【程序为什么要提醒我设置密码?】,导致密码没有初始化,我们使用默认命令 mysql -u root -p 登录失败就是正常现象了。

接下来我们需要想想办法,搞定root账户的密码!我们先找到初始密码。可以使用下面的命令:

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

这里的password里就是MySQL的初始密码,然后我们就可以用初始密码登录后去修改掉它!

chujun@chujun:~$ mysql -udebian-sys-maint -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.24-0ubuntu0.18.10.1 (Ubuntu)

Copyright (c) 2000, 2018, 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.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 

终于进数据库了!!!【撒花~撒花~】

然后我们需要修改PLUGIN设置(出现这个问题的原因就在这)——这里我还没懂为什么需要修改,是发现很多前辈都有提到这个,我们先试试吧!

update mysql.user set authentication_string = password('123456'),plugin='mysql_native_password'where user='root';
【最后请不要忘记分号;否则命令不完整】

mysql> update mysql.user set authentication_string = password('123456'),plugin='mysql_native_password'where user='root';
Query OK, 1 row affected, 1 warning (0.37 sec)
Rows matched: 1  Changed: 1  Warnings: 1

mysql>

然后我就输入了exit命令退出了数据库,准备重新登录试试新密码是否设置成功。

mysql> exit
Bye
hujun@chujun:~$ mysql -u root -p
Enter password: 
ERROR 1698 (28000): Access denied for user 'root'@'localhost'

我的内心是这样的 Ubuntu启动数据库失败(不知道初始密码惹的祸)_第2张图片 感觉刚刚的一切都白折腾了。

再看了下前辈的文章,发现还需要重新启动一次mysql的服务,试试看!

/etc/init.d/mysql stop  关闭服务
/etc/init.d/mysql start  启动服务

然后再输入进入mysql命令

chujun@chujun:~$ /etc/init.d/mysql stop
[ ok ] Stopping mysql (via systemctl): mysql.service.
chujun@chujun:~$ /etc/init.d/mysql start
[ ok ] Starting mysql (via systemctl): mysql.service.
chujun@chujun-virtual-machine:~$ mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.24-0ubuntu0.18.10.1 (Ubuntu)

Copyright (c) 2000, 2018, 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.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 

成功进入!

试试show databases;命令,正常进入!数据库可以正常用啦!

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.17 sec)

mysql> 

 

你可能感兴趣的:(MySQL)