设为首页 收藏本站
查看: 476|回复: 0

[经验分享] mysql关于用户密码的设置( 修改、重置、找回)

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2015-1-5 08:41:45 | 显示全部楼层 |阅读模式
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密码


    • 单实例:mysqladmin–u root –p password ‘新密码’

1
2
[iyunv@Mysql ~]# mysqladmin -u root -p password 'jeck123'
        Enter password:           ----->输入旧密码





    • 多实例:mysqladmin –u root –p password ‘新密码’  -S mysql.sock的位置

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



        注意:若刚装完mysql,则root密码为空,直接回车即可
1.2非交互式修改root密码


    • 单实例:mysqladmin –u root –p‘旧密码’  password  ‘新密码’(注意-p后面无空格)

1
[iyunv@Mysql ~]# mysqladmin -uroot -p'jeck123' password '123jeck'





    • 多实例:mysqladmin –u root –p‘旧密码’  password  ‘新密码’  -S mysql.sock的位置

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



    这种用于非交互式修改密码,适用于脚本
1.3在mysql数据库中修改密码
方法一:(只能修改当前登录mysql用户的密码)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[iyunv@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



方法二:(可以修改任意用户的密码,只要有修改权限)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[iyunv@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
1
2
3
[iyunv@Mysql~]# service mysqld stop
Shuttingdown MySQL. SUCCESS!
[iyunv@Mysql ~]# netstat -lnt | grep mysqld



3.2用mysqld_safe启动mysql,需要加—skip-grant-tables(忽略授权验证)
单实例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
[iyunv@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

[iyunv@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



多实例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
[iyunv@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

[iyunv@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(必须重新启动)
1
2
3
4
5
[iyunv@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用新修改的密码就可以登录了
1
2
3
4
5
6
7
8
9
10
[iyunv@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>






运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-40208-1-1.html 上篇帖子: xtrabackup实战备份mysql5.6.21 下篇帖子: mysql单实例的安装和简单配置(5.1.*版本) mysql 密码 用户
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表