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

补充连接:Windows下忘记密码处理: http://www.javashuo.com/article/p-uhhhwgvw-dp.htmlhtml

场景描述:mysql

Linux环境下忘记 root 密码,sql

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 ]vim

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删除 
保存而且退出vimsocket

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

************************************************this

6. 配置mysql容许远程链接:spa

容许远程用户登陆访问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
相关文章
相关标签/搜索