前段時間安裝mysql5.7后,第一次登陸發(fā)現(xiàn)空密碼怎么都登陸不成功,后來網(wǎng)上查了一下發(fā)現(xiàn),從5.7開始會自動生成一個隨機(jī)密碼了。
- 查看初始密碼
grep 'temporary password' /var/log/mysqld.log
2016-07-08T02:25:46.311098Z 1 [Note] A temporary password is generated for root@localhost: MtPqF0/oN5zo
其中“MtPqF0/oN5zo”就為我們要找的初始密碼
另外一種方法查看默認(rèn)密碼:
cat /root/.mysql_secret
The random password set for the root userat Fri Jan 10 20:00:34 2014 (local time): aJqZsA2m
“aJqZsA2m”就是我們要找的初始密碼
ps:這上面兩種方法都是在網(wǎng)上找的,第一個方法我是在log中沒有找到對應(yīng)的密碼記錄,第二方法找到的密碼也登陸不進(jìn)去,不知道哪里操作錯誤了,最后還是使用的下面的mysql找回密碼大招登陸進(jìn)去的
- mysql密碼找回
- 方法一:
vi /etc/my.cnf
在[mysqld]下加上 skip-grant-tables
,如:
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
skip-grant-tables
重啟mysql
service mysqld restart
登陸mysql后就可以修改密碼了
mysql -u root
update mysql.user set authentication_string=PASSWORD('123456') where User='root';
flush privileges;
然后改回my.cnf重啟mysql。
- 方法2:
先暫停mysql
以不檢查權(quán)限的方式啟動
bin/mysqld_safe --skip-grant-tables &
登陸mysql后就可以修改密碼了
mysql -u root
update mysql.user set authentication_string=PASSWORD('123456') where User='root';
flush privileges;
然后重啟mysql就ok了
service mysqld restart
登錄后任何操作都會有這個提示:
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
還需要刷新一次密碼才行
set password for root@localhost = password('123456');
flush privileges;