Ubuntu18.04解决mysql重置登录用户密码一直失败的问题

apt命令安装完mysql后,直接输入mysql就可进入mysql,

root@hecs-x-xlarge-2-linux-20200630100243:~# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.30-0ubuntu0.18.04.1 (Ubuntu)

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

显然这是不安全的,想要设置登录时需要用户名和密码的方式。
网上有很多教程,都是无法解决的,无论改什么都还是直接输入mysql就可进入数据库。
直到后面看到这个教程后才解决这个问题Ubuntu18.04修改密码

  1. 找到安装时默认生成的默认账户在终端上输入 sudo cat /etc/mysql/debian.cnf 如下显示的账号和密码
root@hecs-x-xlarge-2-linux-20200630100243:~# cat /etc/mysql/debian.cnf
# Automatically generated for Debian scripts. DO NOT TOUCH!
[client]
host     = localhost
user     = debian-sys-maint
password = CgApIoTxZ2gYZ3B0
socket   = /var/run/mysqld/mysqld.sock
[mysql_upgrade]
host     = localhost
user     = debian-sys-maint
password = CgApIoTxZ2gYZ3B0
socket   = /var/run/mysqld/mysqld.sock

注意,这里默认用户名为debian-sys-maint,默认密码为CgApIoTxZ2gYZ3B0
2. 使用默认的用户名和密码来进行登录

root@hecs-x-xlarge-2-linux-20200630100243:~# mysql -u debian-sys-maint -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.30-0ubuntu0.18.04.1 (Ubuntu)

Copyright (c) 2000, 2020, 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>
  1. 进入mysql数据库修改密码
mysql> use mysql;
# Reading table information for completion of table and column names
# You can turn off this feature to get a quicker startup with -A
# Database changed

mysql> update mysql.user set authentication_string=password('123456') where user='root' and Host ='localhost';
# ERROR 1819 (HY000): Your password does not satisfy the current policy requirements

根据错误提示知道是因为新密码不符合mysql默认的密码规范,那么我们就需要更改密码规范或者设置一个满足密码规范的密码

  1. 查看密码规范
mysql> SHOW VARIABLES LIKE 'validate_password%';
+--------------------------------------+--------+
| Variable_name                        | Value  |
+--------------------------------------+--------+
| validate_password_check_user_name    | OFF    |
| validate_password_dictionary_file    |        |
| validate_password_length             | 8      |
| validate_password_mixed_case_count   | 1      |
| validate_password_number_count       | 1      |
| validate_password_policy             | MEDIUM |
| validate_password_special_char_count | 1      |
+--------------------------------------+--------+
7 rows in set (0.00 sec)
  1. 修改密码长度为6
mysql> set global validate_password_length=6;
# Query OK, 0 rows affected (0.00 sec)
  1. 设置密码规则为简单
mysql> set global validate_password_policy=0;
# Query OK, 0 rows affected (0.00 sec)
  1. 重新尝试为root设置新的密码
mysql> update mysql.user set authentication_string=password('123456') where user='root' and Host ='localhost';
# Query OK, 1 row affected, 1 warning (0.00 sec)
# Rows matched: 1  Changed: 1  Warnings: 1

mysql> update user set  plugin="mysql_native_password";
# Query OK, 1 row affected (0.00 sec)
# Rows matched: 4  Changed: 1  Warnings: 0

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

mysql> quit;
# Bye

更改成功

  1. 重启mysql服务
root@hecs-x-xlarge-2-linux-20200630100243:~# service mysql restart;
  1. 使用mysql登录
root@hecs-x-xlarge-2-linux-20200630100243:~# mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
  1. 使用用户名和密码登录
root@hecs-x-xlarge-2-linux-20200630100243:~# mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.30-0ubuntu0.18.04.1 (Ubuntu)

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

大功告成!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值