mysql密码操作
在装完mysql软件后默认有一个root用户,但是没有密码,直接在shell敲mysql就可以进入数据库。
[root@zhaoyun ~]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.0.77 Source distribution
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.0.77 Source distribution
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql>
1、现在为root用户设置一个密码:
[root@zhaoyun ~]# mysqladmin -u root password "123456" #这个命令为root用户设置一个123456的密码。
再次登陆就需要密码来登陆了。
不输入密码出现报错:
[root@zhaoyun ~]# mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
输入密码就正常登陆了:
[root@zhaoyun ~]# mysql -uroot -p123456
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.0.77 Source distribution
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.0.77 Source distribution
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql>
mysql> set password=password("654321");
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
3、重新设置密码:用mysqladmin命令设置,需要知道原有密码
[root@zhaoyun ~]# mysqladmin -u root -p password "123456"
Enter password: 654321
Enter password: 654321
密码忘记处理方法:
1、修改/etc/my.cnf文件
添加俩行:
[root@zhaoyun ~]# vim /etc/my.cnf
[mysqld]
skip-grant-table #绕过授权表验证
skip-networking #绕过网络服务验证,如果本地操作的话,就不用这一行了。
skip-grant-table #绕过授权表验证
skip-networking #绕过网络服务验证,如果本地操作的话,就不用这一行了。
2、重启mysqld服务
[root@zhaoyun ~]# service mysqld restart
Stopping MySQL: [ OK ]
Starting MySQL: [ OK ]
Stopping MySQL: [ OK ]
Starting MySQL: [ OK ]
这个时候登陆就不需要密码了。
[root@zhaoyun ~]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.0.77 Source distribution
[root@zhaoyun ~]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.0.77 Source distribution
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql>
3、登陆数据库后,就可以重新设置密码了。
mysql> update mysql.user set password=password("123456") where user="root";
Query OK, 2 rows affected (0.00 sec)
Rows matched: 3 Changed: 2 Warnings: 0
Query OK, 2 rows affected (0.00 sec)
Rows matched: 3 Changed: 2 Warnings: 0
mysql> flush privileges ; #刷新权限
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
4、将/etc/my.cnf文件中添加的俩行去掉,重启mysqld服务。
[root@zhaoyun ~]# service mysqld restart
Stopping MySQL: [ OK ]
Starting MySQL: [ OK ]
[root@zhaoyun ~]#
Stopping MySQL: [ OK ]
Starting MySQL: [ OK ]
[root@zhaoyun ~]#
完成后就可以用设置的密码登陆数据库了。
[root@zhaoyun ~]# mysql -uroot -p123456
[root@zhaoyun ~]# mysql -uroot -p123456
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.0.77 Source distribution
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql>
本文转自zhaoyun00 51CTO博客,原文链接:http://blog.51cto.com/zhaoyun/727061