linux下MySQL的登陆与设置密码(本文基于centos6.4、mysql5.7.3),mysql安装在/usr/local/mysql目录下:
mysql的安装请参考:http://note.t4x.org/system/centos-nginx-mysql-php/
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
[root@lnmp ~]# /usr/local/mysql/bin/mysqladmin -uroot password 'admin123' #修改mysql密码为admin123 [root@lnmp ~]# /usr/local/mysql/bin/mysql #如果是空密码可以这样进入mysql [root@lnmp ~]# /usr/local/mysql/bin/mysql -uroot -p #建议此种方式,后面的不建议/usr/local/mysql/bin/mysql -uroot -p'admin123' [root@lnmp ~]# /usr/local/mysql/bin/mysqladmin -uroot -p'admin123' password 'admin456' mysqladmin: [Warning] Using a password on the command line interface can be insecure. [root@lnmp ~]# /usr/local/mysql/bin/mysql -uroot -p'admin123' mysql: [Warning] Using a password on the command line interface can be insecure. ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES) [root@lnmp ~]# /usr/local/mysql/bin/mysql -uroot -p'admin456' mysql> mysql> update mysql.user set password=PASSWORD('admin123') where user='root'; #升级用户表中的密码 Query OK, 4 rows affected (0.03 sec) Rows matched: 4 Changed: 4 Warnings: 0 mysql> flush privileges; #如果不更新,则新密码不生效 mysql> set password=password('admin123'); #以上方法适用于已知密码或者已经登入到msyql>的情况下!!! Query OK, 0 rows affected (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) |
忘记mysql的root密码:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
[root@lnmp ~]# service mysqld stop #关闭mysql Shutting down MySQL... SUCCESS! [root@lnmp ~]# /usr/local/mysql/bin/mysqld_safe --skip-grant-tables & #跳过授权表 [1] 3692 [root@lnmp ~]# 140502 15:40:48 mysqld_safe Logging to '/byrd/server/mysql-5.7.3/data/lnmp.err'. 140502 15:40:48 mysqld_safe Starting mysqld daemon with databases from /byrd/server/mysql-5.7.3/data [root@lnmp ~]# /etc/init.d/mysqld start #跳过授权表后,重新启动mysql Starting MySQL SUCCESS! [root@lnmp ~]# /usr/local/mysql/bin/mysql #直接进入mysql Welcome to the MySQL monitor. Commands end with ; or \g. mysql> update mysql.user set password=PASSWORD('admin123') where user='root'; #修改需要的mysql的root密码 mysql> flush privileges; #刷新权限,密码修改为admin123 mysql>quit [root@lnmp ~]# /etc/init.d/mysqld stop [root@lnmp ~]# /etc/init.d/mysqld start [root@lnmp ~]# /usr/local/mysql/bin/mysql -uroot -p'admin123' #密码被修改为了admin123 |
备注:你可以将/usr/local/mysql/bin加入PATH中。这样可以直接输入mysql或者mysqladmin,不用输入全路径。
申明:本文由BYRD原创(基于Centos6.4 X64、mysql5.7.3),未经许可禁止转载! SourceByrd's Weblog-https://note.t4x.org/database/mysql-login-setpassword/
申明:除非注明Byrd's Blog内容均为原创,未经许可禁止转载!详情请阅读版权申明!