Mysql 数据库管理员密码管理
Mysql数据在维护中经常需要修改管理员密码,在多次的修改中自己也可能忘记。另外在岗位交接中也可能忘了交接数据库的密码。在这种情况下,掌握在不知道或忘了mysql的root密码的情况下修改mysql数据库的密码就变得很有必要。
不多废话,说正事:
上述情况Mysql数据库root密码修改有两种方法:
1、 改配置文件:
(1)模拟错误,修改管理员密码:
[root@localhost ~]# mysqladmin -uroot password "123123"
[root@localhost ~]# mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
(2)修改mysql的配置文件:/etc/my.cnf
[root@localhost ~]# vi /etc/my.cnf
[mysqld] #在配置文件中找到mysqld模块添加
skip-grant-tables #该项必须在mysqld模块内,保存推出
(3)重启mysql并修改密码:
[root@localhost ~]# service mysqld restart
[root@localhost ~]# mysql #登录mysql,不需要其他
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.0.77-log Source distribution
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql>
mysql> use mysql; #使用mysql数据库
Database changed
mysql> update user set password=password("123") where user="root"; #修改root密码,注意password必须这么写,123为密码是自己定的。
Query OK, 3 rows affected (0.00 sec)
Rows matched: 3 Changed: 3 Warnings: 0
mysql> flush privileges; #刷新权限
Query OK, 0 rows affected (0.00 sec)
mysql> exit #退出
Bye
(4)取消跳过验证模式:
[root@localhost ~]# vi /etc/my.cnf
[mysqld]
#skip-grant-tables #这里可以注释掉、也可以直接删除
[root@localhost ~]# service mysqld restart #重启服务
(5)登录验证:
[root@localhost ~]# mysql -uroot -p123 #使用root和123密码登录数据库
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.0.77-log Source distribution
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> #登录成功,很爽!
2、 使用命令:
(1) 停止服务,以跳过验证的模式启动mysql数据库:
[root@localhost ~]# service mysqld stop
停止 MySQL: [确定]
[root@localhost ~]# mysqld_safe --skip-grant-tables & #跳过用户名验证模式启动
[1] 3465 #此PID号需要记住、改完密码后需要杀死它
(2) 修改密码:
[root@localhost ~]# mysql #登录mysql,不需要其他
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.0.77-log Source distribution
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql>
mysql> use mysql; #使用mysql数据库
Database changed
mysql> update user set password=password("12345") where user="root"; #修改root密码,注意password必须这么写,12345为密码是自己定的。
Query OK, 3 rows affected (0.00 sec)
Rows matched: 3 Changed: 3 Warnings: 0
mysql> flush privileges; #刷新权限
Query OK, 0 rows affected (0.00 sec)
mysql> exit #退出
Bye
(3) 终止mysql的safe模式、正常启动mysql:
[root@localhost ~]# kill -9 3465 #杀死skip模式的mysql进程
[root@localhost ~]# killall mysqld #如果没有杀干净,使用改命令杀死所有的mysqld程序,不然mysqld启动不了
[root@localhost ~]# service mysqld start
(4) 验证:
[root@localhost ~]# mysql #验证没有用户名及密码的登录
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
[root@localhost ~]# mysql -uroot -p12345 #新密码
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.0.77-log Source distribution
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql>