环境:
mysql版本:Ver 5.7.34 for Linux on x86_64 (MySQL Community Server (GPL))
server OS:centOS 7
-------------------------------------------------------------------------------------------------
服务器断电重启后,mysql无法访问了。
经在网上大量查阅后,发现需要重置mysql,即重新初始化 mysql。
经查看/etc/my.cnf中的datadir值是“datadir=/var/lib/mysql”,
第一步:先将 目录“/var/lib/mysql”下的所有文件都 删除;
第二步:执行命令“systemctl start mysqld”,系统会自动生成目录“/var/lib/mysql”下的文件;
第三步:执行如下命令:
[root@vm-database ~]# mysql -uroot -p
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
输入之前的密码不对,也试了好几个,也没有用,又在网上查阅了下,发现可以在文件“/etc/my.cnf”中的 [mysqld] 后面任意一行添加“skip-grant-tables”用来跳过密码验证的过程,例如下图
然后执行命令
[root@vm-database ~]# systemctl restart mysqld
再执行下面的命令后:
[root@vm-database ~]# mysql -uroot -p
在出现的输入密码的位置直接回车,进行mysql。
接着执行如下操作:
mysql> use mysql
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
经过一番查阅后,执行如下命令解决的命令重置的问题:
mysql> alter user user() identified by '123456';
Query OK, 0 rows affected (0.00 sec)
mysql>
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> quit
Bye
使用上面调协的密码123456重新执行如下命令,成功;
[root@vm-database mysql]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.34 MySQL Community Server (GPL)
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
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;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
下面接着解决远端登录数据库的问题,如下命令:
mysql> grant all privileges on *.* to 'root'@'%' identified by 'ab5Nz#' with grant option;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> quit
Bye
好的,大功造成
--------------------------------------------------------------------------
参考:
1、《重置密码解决MySQL5.7 for Linux错误 ERROR 1045 》;
3、《mysql 重新初始化》