CentOs7强制修改MySQL中root密码

centos7强制修改MySQL默认密码

前言

最近需要搭建zabbix,需要在centos7上搭建MySQL服务器;这个很简单,直接命令:
获取MySQL源:

1
sudo rpm -Uvh https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm

安装:

1
sudo yum install mysql-* --skip-broken

问题

安装过程,由于公司网简直惨不忍睹,中间一度断掉了,都不知道哪边安装出错了。。。好不容易看着安装成功之后,查找默认的root密码:

1
grep 'temporary password' /var/log/mysqld.log

然后就悲剧了,这个日志文件,压根就是空的,root密码跑哪玩去了!!!
然后试了试,貌似好像可以起服务。。见鬼

1
2
systemctl start mysqld
systemctl status mysqld

不过这样还是进不了控制台啊,没密码啊,没办法,只有强制修改root密码。

强制修改密码

首先强制修改配置文件,/etc/my.cnf
添加:

1
2
[mysqld]
skip-grant-tables=1

添加skip-grant-tables=1这行,然后重启MySQL服务。

1
systemctl restart mysqld

修改密码

进入MySQL控制台:

1
mysql

打印:

1
2
3
4
5
6
7
8
9
10
11
12
13
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.21 MySQL Community Server (GPL)

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>

修改密码

1
2
3
use mysql;
update user set authentication_string = password("xxxx") where user="root";
flush privileges;

然后将/etc/my.cnfskip-grant-tables=1注释掉,重启MySQL服务。

后续

使用修改完成的root密码登录MySQL控制台之后,可以进入控制台:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.21

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>

但是有问题:

1
2
mysql> show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

按照提示,我们在修改root密码时候,应该使用的是alter而不是update来更新密码,所以,重新修改密码:

1
2
alter user 'root'@'localhost' identified by 'QWEqwe+342';
flush privileges;

再次重启服务即可。

文章目录
  1. 前言
  2. 问题
  3. 强制修改密码
    1. 修改密码
    2. 修改密码
  4. 后续
|