MySQL初始化以及更改密碼


MySQL初始化,不修改配置文件(作為隨筆筆記)

執行命令:

mysql_secure_installation

 

MySQL更改密碼:

使用 mysqladmin 來更改 root 用戶密碼

[root@localhost ~ ]# mysqladmin -uroot(username) -p123456(old_password) password test(new_password)

Warning: Using a password on the command line interface can be insecure.

會提示不安全,可以采用如下命令:

[root@localhost ~ ]# mysqladmin -uroot -p password
Enter password:
New password:
Confirm new password:

這樣比較安全。

 

進入MySQL終端更改密碼:

mysql -uroot -p

use mysql;

update user set password=password("new_password") where user='username' and host='localhost';

flush privileges;

password()函數用於加密,不寫的話代表使用明文密碼。

簡單介紹兩種常用方法,不再介紹過多方法。

 

忘記MySQL root 密碼:

skip-grant-tables的解法

首先,關閉實例

這里,只能通過kill mysqld進程的方式。

注意:不是mysqld_safe進程,也切忌使用kill -9。

ps -ef |grep mysqld

kill mysqld_ps

mysqld_safe --defaults-file=my.cnf --skip-grant-tables  --skip-networking &

mysql -S /var/lib/mysql/mysql.sock

mysql> update mysql.user set password=password('123456') where host='localhost' and user='root';
Query OK, 0 rows affected, 1 warning (0.00 sec)
Rows matched: 1 Changed: 0 Warnings: 1

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

注意:
這里的update語句針對的是MySQL 5.6的操作,如果是在5.7版本,修改的應該是authentication_string字段,而不是password。

 

更優雅的解法(本人測試並未成功):

創建init.sql文件,關閉mysqld

vim init.sql
alter user 'root'@'localhost' identified by '123456';

重新啟動服務

mysqld_safe --defaults-file=/etc/my.cnf --init-file=/root/init.sql &

借鑒了這位大佬的方法,請查看原文,挺詳細的:

https://www.cnblogs.com/ivictor/p/9243259.html

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM