Mysql密码我们如果安装时没有设置也可以在命令模式下进行设置了,下面来为各位介绍linux中Mysql的登陆与设置密码步骤吧,希望例子对各位有帮助.
linux下Mysql的登陆与设置密码(本文基于centos6.4、mysql5.7.3),mysql安装在/usr/local/mysql目录下:
- [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密码:
- [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 --Vevb.com
- 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,不用输入全路径.
新闻热点
疑难解答