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
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,不用輸入全路徑