干货推荐,今天这一期给大家分享下关于mysql重置密码操作
![fca0528d41f26e1d57d318a7e3cda67e.png](https://i-blog.csdnimg.cn/blog_migrate/64be05a96f63ea001be6fa49aedbb02e.jpeg)
废话少说 ,直接进入正题
怎centos 系统下如果忘记mysql密码怎么办?
那我们在指令界面上输入
[root@xxxx ~]# mysql -u root -p
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
出现这个审核大家不要慌,按照下面的操作就可以解决问题
1.首先 检查mysql服务是否还开着,如果还有开着,那么首先停止mysql服务
叫大家一种查看 服务是否开启的方法 ps -ef | grep -i mysql
[root@xxxx ~]# ps -ef | grep -i mysql
如果出现以下这个,那么就是说明服务还在开着
root 22972 1 0 14:18 pts/0 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
mysql 23166 22972 0 14:18 pts/0 00:00:00 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib/mysql/plugin --user=mysql --log-error=/var/log/mysqld.log --pid-file=/var/run/mysqld/mysqld.pid --socket=/var/lib/mysql/mysql.sock
root 23237 21825 0 14:22 pts/0 00:00:00 grep -i mysql
我们首先需要做的就是先把这个服务停止掉
service mysql stop
如果你是使用一键安装的也可以直接使用
lnmp restart
![7619bca8ca88128597a9da77cb88c2e2.png](https://i-blog.csdnimg.cn/blog_migrate/726e10c2acc1ab1c5353722828c780b2.jpeg)
[root@xxxx ~]# service mysql stop
在接着 修改mysql的配置文件my.cnf
这个文件my.cnf配置文件的位置,一般在/etc/my.cnf,有些版本在/etc/mysql/my.cnf
这个我们执行 vi /etc/my.cnf
![fdc862224287dd53cc9ece520a5516df.png](https://i-blog.csdnimg.cn/blog_migrate/48f69edbc9d23b00e6b2aa6271332925.jpeg)
在配置文件中,增加2行代码
找到 [mysqld]
在这个下面添加这条语法
skip-grant-tables
这个的作用是登录mysql的时候跳过密码验证,然后启动mysql服务,并进入mysql
然后我们保存退出,再次启动脚本
[root@xxxx ~]# service mysqld start
[root@xxxx ~]# mysql -u root
可以看到这个是审核提示的是下面这个条语法,他已经绕过密码验证,到这里已经成功了一半。
Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.
这个步骤操作很重要. 修改密码
我们执行
mysql> use mysql;
会提示下面这个指令,说明已经i进入了
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
这个是最关键的也是最重要的操作,修改密码 root_password 这个就是你需要重新设置的密码
mysql> update mysql.user set authentication_string=password('root_password') where user='root';
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 1
上面的提示说已经修改了,这个审时候我们在执行下下面语法 ,刷新下
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
最后退出
mysql> exit
重启mysql服务
先将之前加在配置文件里面的2句代码注释或删除掉,然后重启mysql服务,就可以使用刚刚设置的密码登录了。
上面已经将密码重置成功了,我们这个时候就可以将服务重新开启
[root@xxxx ~]# service mysql start
再次执行连接操作
[root@xxxx ~]# mysql -u root -proot_password
![48c6b48de009bae7ead437a3949003b0.png](https://i-blog.csdnimg.cn/blog_migrate/e9d8b012d84e5274965689c8faa51afe.jpeg)
好了今天分享干货就到这里结束了,喜欢的关注点赞收藏哦!