1.如果忘记了mysql的root密码,可以用以下方法解决(mysql5.5.47为例)?
1)平滑关闭数据库
[root@desktop5 ~]# /etc/init.d/mysql stop
2)跳过授权表开启数据库
[root@desktop5 ~]# mysqld_safe--skip_grant_tables --skip_networking & [1] 83153 160114 19:50:08 mysqld_safe Logging to'/var/log/mysqld.log'. 160114 19:50:08 mysqld_safe Starting mysqlddaemon with databases from /mysqldata/ --skip_grant_tables 跳过授权表 --skip_networking 跳过网络,防止其他用户对数据进行读写操作,待密码恢复后可正常开启
3)更改管理员root密码
mysql> update mysql.user setpassword=password('redhat123') where user='root' and host='localhost'; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> flush privileges;
4)平滑关闭数据库
[root@desktop5 ~]# mysqladmin -uroot-predhat123 shutdown 160114 19:51:34 mysqld_safe mysqld from pidfile /var/run/mysqld/mysqld.pid ended [1]+ Done mysqld_safe --skip_grant_tables --skip_networking
5).登录验证
[root@desktop5 ~]# /etc/init.d/mysql start Starting MySQL.. [ OK ] [root@desktop5 ~]# mysql -uroot -predhat123 Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 Server version: 5.5.47 MySQL CommunityServer (GPL) Copyright (c) 2000, 2015, Oracle and/or itsaffiliates. All rights reserved. Oracle is a registered trademark of OracleCorporation and/or its affiliates. Other names may be trademarks oftheir respective owners. Type 'help;' or '\h' for help. Type '\c' toclear the current input statement. mysql> show databases;
2.MySQL5.6初始化时修改mysql数据密码
mysql> set password for root@localhost=password('redhat'); Query OK, 0 rows affected (0.00 sec) mysql> update mysql.user set password=password("redhat") where host='localhost' and user='root'; Query OK, 1 row affected (0.00 sec) # mysqladmin -u root -h desktop5.example.com password 'redhat' -p