运维笔记--linux下忘记mysql root密码

补充链接:Windows下忘记密码处理: https://www.cnblogs.com/hellojesson/p/5972950.html

场景描述:

Linux环境下忘记 root 密码,

1. 修改MySQL的登录设置: 
# vim /etc/my.cnf 
在[mysqld]的段中加上一句:skip-grant-tables 
例如: 
[mysqld] 
datadir=/var/lib/mysql 
socket=/var/lib/mysql/mysql.sock 
skip-grant-tables 
保存并且退出vi。

2. 重新启动mysqld 
# service mysqld restart 
Stopping MySQL: [ OK ] 
Starting MySQL: [ OK ]

3. 登录并修改MySQL的root密码 

# mysql 
Welcome to the MySQL monitor. Commands end with ; or \g. 
Your MySQL connection id is 3 to server version: 3.23.56 
Type 'help;' or '\h' for help. Type '\c' to clear the buffer. 
mysql> USE mysql ; 
Database changed 
mysql> UPDATE user SET Password = password ( 'new-password' ) WHERE User = 'root' ; 
Query OK, 0 rows affected (0.00 sec) 
Rows matched: 2 Changed: 0 Warnings: 0 
mysql> flush privileges ; 
Query OK, 0 rows affected (0.01 sec) 
mysql> quit

4.将MySQL的登录设置修改回来 
# vim /etc/my.cnf 
将刚才在[mysqld]的段中加上的skip-grant-tables删除 
保存并且退出vim

5.重新启动mysqld 
# service mysqld restart 
Stopping MySQL: [ OK ] 
Starting MySQL: [ OK ]

************************************************

6. 配置mysql允许远程连接:

允许远程用户登录访问mysql的方法
需要手动增加可以远程访问数据库的用户。

方法一(不推荐) 本地登入mysql,更改 "mysql" 数据库里的 "user" 表里的 "host" 项,将"localhost"改为"%"

#mysql -u root -proot

mysql>use mysql;
mysql>update user set host = '%' where user = 'root';
mysql>select host, user from user;

方法二 直接授权(推荐)

从任何主机上使用root用户,密码:youpassword(你的root密码)连接到mysql服务器:(首先登陆Linux服务器,填写下面代码即可)

[root@localhost software]# mysql -u root -proot 
mysql>GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'youpassword' WITH GRANT OPTION;
mysql>FLUSH PRIVILEGES;

操作完后切记执行以下命令刷新权限

FLUSH PRIVILEGES;

方法三:终极方法

找到mysql.cnf

注释bind-address = 127.0.0.1

改为bind-address = 0.0.0.0

重启服务。

方法四: 给指定用户授权  --不需要刷新,重启数据库

mysql> use mysql;
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
mysql> update user set host = '%' where user = '你的用户';

Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

 

posted @ 2019-05-30 19:44  hello-Jesson  阅读(297)  评论(0编辑  收藏  举报