mysql关于用户密码的设置( 修改、重置、找回)

时间:2015-01-05 07:11:09   收藏:0   阅读:310


1.登录mysql

1.1单实例登录

1) mysql     刚装完mysql无密码情况下登录

2) mysql–u root   刚装完mysql无密码情况下登录 

3) mysql–u root –p   标准的dba登录

3) mysql–u root –p ‘密码’  无交互登录。一般不用,容易泄漏密码

登录成功后 提示:mysql>

1.2 多实例登录

        mysql –u root –S 指定mysql.sock文件的位置

        提示:和单实例唯一的区别是多实例需要指定mysql.sock的位置

2.修改用户密码

1.1交互式修改root密码

[root@Mysql ~]# mysqladmin -u root -p password ‘jeck123‘
        Enter password:           ----->输入旧密码
[root@Mysql ~]# mysqladmin -u root -p password ‘jeck123‘ -S/usr/local/mysql/tmp/mysql.sock 
    Enter password:          ----->输入旧密码

        注意:若刚装完mysql,则root密码为空,直接回车即可

1.2非交互式修改root密码

[root@Mysql ~]# mysqladmin -uroot -p‘jeck123‘ password ‘123jeck‘
[root@Mysql ~]# mysqladmin -uroot -p‘jeck123‘ password ‘123jeck‘-S /usr/local/mysql/tmp/mysql.sock

    这种用于非交互式修改密码,适用于脚本

1.3在mysql数据库中修改密码

方法一:(只能修改当前登录mysql用户的密码)

[root@Mysql ~]# mysql -uroot -p123jeck
Welcometo the MySQL monitor.  Commands end with; or \g.
YourMySQL connection id is 4
Serverversion: 5.1.56-log Source distribution
Copyright(c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
Thissoftware comes with ABSOLUTELY NO WARRANTY. This is free software,
andyou are welcome to modify and redistribute it under the GPL v2 license
Type‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.

mysql> setpassword=password(‘456jeck‘); 
Query OK, 0 rows affected (0.00 sec)
mysql> flushprivileges;

Query OK, 0 rows affected (0.00 sec)
mysql> exit

方法二:(可以修改任意用户的密码,只要有修改权限)

[root@Mysql ~]# mysql -uroot -p456jeck
Welcometo the MySQL monitor.  Commands end with; or \g.
YourMySQL connection id is 6
Serverversion: 5.1.56-log Source distribution
Copyright(c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
Thissoftware comes with ABSOLUTELY NO WARRANTY. This is free software,
andyou are welcome to modify and redistribute it under the GPL v2 license
Type‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.

mysql> updatemysql.user set password=PASSWORD(‘789jeck‘) where user=‘root‘;
Query OK, 3 rows affected (0.00 sec)
Rows matched: 3  Changed:3  Warnings: 0

mysql> flushprivileges;
Query OK, 0 rows affected (0.00 sec)
mysql>

3.找回用户密码

3.1关闭mysql

[root@Mysql~]# service mysqld stop
Shuttingdown MySQL. SUCCESS!
[root@Mysql ~]# netstat -lnt | grep mysqld

3.2用mysqld_safe启动mysql,需要加—skip-grant-tables(忽略授权验证)

单实例:

[root@Mysql~]# /usr/local/mysql/bin/mysqld_safe--skip-grant-tables &   --->加&,使进程在后台运行
[1]28643
15010315:56:25 mysqld_safe Logging to ‘/usr/local/mysql-5.1.56/data/Mysql.err‘.
15010315:56:25 mysqld_safe Starting mysqld daemon with databases from/usr/local/mysql-5.1.56/data

[root@Mysql ~]# mysql -uroot -p
Enter password:              ---->直接回车,因为已经忽略验证

Welcometo the MySQL monitor.  Commands end with; or \g.
YourMySQL connection id is 1
Serverversion: 5.1.56-log Source distribution
Copyright(c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
Thissoftware comes with ABSOLUTELY NO WARRANTY. This is free software,
andyou are welcome to modify and redistribute it under the GPL v2 license
Type‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.

mysql> setpassword=password(‘jeck123‘);            --->此方法不行
ERROR 1290 (HY000): The MySQL server is running with the--skip-grant-tables option so it cannot execute this statement

mysql> updatemysql.user set password=PASSWORD(‘jeck123‘) where user=‘root‘;   
Query OK, 3 rows affected (0.00 sec)
Rows matched: 3  Changed:3  Warnings: 0

mysql> flushprivileges;
Query OK, 0 rows affected (0.00 sec)

mysql> exit
Bye

多实例:

[root@Mysql~]# /usr/local/mysql/bin/mysqld_safe--defaults-file=/etc/my.cnf --skip-grant-tables &
[1]28643
15010315:56:25 mysqld_safe Logging to ‘/usr/local/mysql-5.1.56/data/Mysql.err‘.
15010315:56:25 mysqld_safe Starting mysqld daemon with databases from/usr/local/mysql-5.1.56/data

[root@Mysql ~]# mysql -uroot -p   -S/usr/local/mysql/tmp/mysql.sock  
Enter password:              ---->直接回车,因为已经忽略验证

Welcometo the MySQL monitor.  Commands end with; or \g.
YourMySQL connection id is 1
Serverversion: 5.1.56-log Source distribution
Copyright(c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
Thissoftware comes with ABSOLUTELY NO WARRANTY. This is free software,
andyou are welcome to modify and redistribute it under the GPL v2 license
Type‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.

mysql> updatemysql.user set password=PASSWORD(‘jeck123‘) where user=‘root‘;               ---->修改root密码   
Query OK, 3 rows affected (0.00 sec)
Rows matched: 3  Changed:3  Warnings: 0

mysql> flushprivileges;
Query OK, 0 rows affected (0.00 sec)

mysql> exit
Bye

3.3重新启动mysql(必须重新启动

[root@Mysql~]# service mysqld restart
Shuttingdown MySQL.150103 16:07:17 mysqld_safe mysqld from pid file/usr/local/mysql-5.1.56/data/Mysql.pid ended
 SUCCESS! 
StartingMySQL. SUCCESS! 
[1]+  Done                   /usr/local/mysql/bin/mysqld_safe --skip-grant-tables

3.4用新修改的密码就可以登录了

[root@Mysql~]# mysql -uroot -pjeck123

Welcometo the MySQL monitor.  Commands end with; or \g.
YourMySQL connection id is 1
Serverversion: 5.1.56-log Source distribution
Copyright(c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
Thissoftware comes with ABSOLUTELY NO WARRANTY. This is free software,
andyou are welcome to modify and redistribute it under the GPL v2 license
Type‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.
mysql>


本文出自 “Study-Everyday” 博客,请务必保留此出处http://studys.blog.51cto.com/9736817/1599104

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!