MYSQL--root密码忘记找回

下午把数据库关闭了,准备修改通过skip-granted-tables修改下root的密码,结果关闭之后,重启不成功。

[root@iZ2zehmkcpexiu333gfymtZ ~]# service mysqld start

MySQL Daemon failed to start.

Starting mysqld:                                           [FAILED]

mysql关闭之后无法正常启动,提示启动失败。

查看mysql启动日志:

tail -20  /var/log/mysqld.log

[php]  view plain  copy
  1. 180516 18:33:07 [ERROR] /usr/libexec/mysqld: Can't create/write to file '/var/run/mysqld/mysqld.pid' (Errcode: 13)  
  2. 180516 18:33:07 [ERROR] Can't start server: can't create PID file: Permission denied  
  3. 180516 18:33:07 mysqld_safe mysqld from pid file /var/run/mysqld/mysqld.pid ended  
  4. 180516 18:33:20 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql  
  5. 180516 18:33:20  InnoDB: Initializing buffer pool, size = 8.0M  
  6. 180516 18:33:20  InnoDB: Completed initialization of buffer pool  
  7. 180516 18:33:20  InnoDB: Started; log sequence number 0 1455648  
  8. 180516 18:33:20 [ERROR] /usr/libexec/mysqld: Can't create/write to file '/var/run/mysqld/mysqld.pid' (Errcode: 13)  
  9. 180516 18:33:20 [ERROR] Can't start server: can't create PID file: Permission denied  
  10. 180516 18:33:20 mysqld_safe mysqld from pid file /var/run/mysqld/mysqld.pid ended  

提示不能启动服务,不能创建PID文件,权限不足

[php]  view plain  copy
  1. Can't create/write to file '/var/run/mysqld/mysqld.pid' 提示不能创建pid文件,目录为 /var/run/mysqld/ 看了下目录权限为nobody   

而mysql使用的是mysql用户运行的,把权限修改 chmod 777 /var/run/mysqld -R

#############################################################################################

重置密码步骤:

    1、先查看mysql是否启动,找到mysqld_safe的路径

        

[php]  view plain  copy
  1. [root@iZ2zehmkcpexiu333gfymtZ ~]# ps -ef | grep mysqld  
  2. root      8702     1  0 18:35 ?        00:00:00 /bin/sh /usr/bin/mysqld_safe --datadir=/var/lib/mysql --socket=/var/lib/mysql/mysql.sock --pid-file=/var/run/mysqld/mysqld.pid --basedir=/usr --user=mysql  
  3. mysql     8804  8702  0 18:35 ?        00:00:00 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --user=mysql --log-error=/var/log/mysqld.log --pid-file=/var/run/mysqld/mysqld.pid --socket=/var/lib/mysql/mysql.sock  
  4. root      9106  9069  0 18:45 pts/4    00:00:00 grep mysqld  

  2、关闭mysqld

[php]  view plain  copy
  1. [root@iZ2zehmkcpexiu333gfymtZ ~]# service mysqld stop  
  2. Stopping mysqld:                                           [  OK  ]  

 3、  -–skip-grant-tables 跳过授权表启动mysql , 这个地方重启mysql的时候 需要注意,这个时候mysql只能在 本服务器进行连接不需要用户名密码,直接mysql 就可以进行命令行,可以在这个命令之后在价格 & 表示这个这个进程挂到后台运行

[php]  view plain  copy
  1. [root@iZ2zehmkcpexiu333gfymtZ ~]# /usr/bin/mysqld_safe --datadir=/var/lib/mysql --socket=/var/lib/mysql/mysql.sock --pid-file=/var/run/mysqld/mysqld.pid --basedir=/usr --user=mysql<span style="color:#ff6666;"> --skip-grant-tables </span>  
  2. 180516 18:47:14 mysqld_safe Logging to '/var/log/mysqld.log'.  
  3. 180516 18:47:14 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql  

4、进入mysql,直接就可以操作了

[php]  view plain  copy
  1. [root@iZ2zehmkcpexiu333gfymtZ ~]# mysql  
  2. Welcome to the MySQL monitor.  Commands end with ; or \g.  
  3. Your MySQL connection id is 1  
  4. Server version: 5.1.73 Source distribution  
  5.   
  6. Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.  
  7.   
  8. Oracle is a registered trademark of Oracle Corporation and/or its  
  9. affiliates. Other names may be trademarks of their respective  
  10. owners.  
  11.   
  12. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.  
  13.   
  14. mysql>   

5、查询下user表,其中Host对应的 % 表示允许任意主机访问,如果要限制具体的ip访问,修改为指定的ip就行

[php]  view plain  copy
  1. mysql> select * from user \G;  
  2. *************************** 1. row ***************************  
  3.                  Host: %  
  4.                  User: root  
  5.              Password: *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9  
  6.           Select_priv: Y  
  7.           Insert_priv: Y  
  8.           Update_priv: Y  
  9.           Delete_priv: Y  
  10.           Create_priv: Y  
  11.             Drop_priv: Y  
  12.           Reload_priv: Y  
  13.         Shutdown_priv: Y  
  14.          Process_priv: Y  
  15.             File_priv: Y  
  16.            Grant_priv: Y  
  17.       References_priv: Y  
  18.            Index_priv: Y  
  19.            Alter_priv: Y  
  20.          Show_db_priv: Y  
  21.            Super_priv: Y  
  22. Create_tmp_table_priv: Y  
  23.      Lock_tables_priv: Y  
  24.          Execute_priv: Y  
  25.       Repl_slave_priv: Y  
  26.      Repl_client_priv: Y  
  27.      Create_view_priv: Y  
  28.        Show_view_priv: Y  
  29.   Create_routine_priv: Y  
  30.    Alter_routine_priv: Y  
  31.      Create_user_priv: Y  
  32.            Event_priv: Y  
  33.          Trigger_priv: Y  
  34.              ssl_type:   
  35.            ssl_cipher:   
  36.           x509_issuer:   
  37.          x509_subject:   
  38.         max_questions: 0  
  39.           max_updates: 0  
  40.       max_connections: 0  
  41.  max_user_connections: 0  
  42. 1 row in set (0.00 sec)  
6、修改root的密码,刷新下权限,MYSQL不同版本修改密码方式不同 ( ps:每次修改完记得刷新下权限 )

[php]  view plain  copy
  1. mysql> update user set Password=password('123456');  
  2. Query OK, 0 rows affected (0.00 sec)  
  3. Rows matched: 1  Changed: 0  Warnings: 0  
  4.   
  5. mysql> <span style="color:#ff0000;">flush  privileges;</span>  
  6. Query OK, 0 rows affected (0.00 sec)  
  7.   
  8. mysql>   

7、关闭mysql,重新启动mysql

[php]  view plain  copy
  1. [root@iZ2zehmkcpexiu333gfymtZ ~]# ps -ef | grep mysql  
  2. root      9314  9052  0 18:47 pts/3    00:00:00 /bin/sh /usr/bin/mysqld_safe --datadir=/var/lib/mysql --socket=/var/lib/mysql/mysql.sock --pid-file=/var/run/mysqld/mysqld.pid --basedir=/usr --user=mysql --skip-grant-tables  
  3. mysql     9416  9314  0 18:47 pts/3    00:00:00 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --user=mysql --skip-grant-tables --log-error=/var/log/mysqld.log --pid-file=/var/run/mysqld/mysqld.pid --socket=/var/lib/mysql/mysql.sock  
  4. root      9798  9069  0 19:02 pts/4    00:00:00 grep mysql  
  5. [root@iZ2zehmkcpexiu333gfymtZ ~]# killall mysqld_safe   
  6. [root@iZ2zehmkcpexiu333gfymtZ ~]# kill -7 9314  
  7. [root@iZ2zehmkcpexiu333gfymtZ ~]# service mysqld start  
  8. Starting mysqld:                                           [  OK  ]  
  9. [root@iZ2zehmkcpexiu333gfymtZ ~]#   
8、大功告成,测试一下~
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值