一、擁有原來的myql的root的密碼;
mysql -uroot -p
Enter password: 【輸入原來的密碼】(如果安裝完mysql第一次設置root密碼,則直接回車)
mysql>use mysql;
mysql> update user set authentication_string=password("123456") where user='root';
mysql> flush privileges;
mysql> exit;
二、忘記原來的myql的root的密碼;
首先,你必須要有操作系統的root權限了。也就說需要以root的身份登錄到操作系統,然後進行一下操作。
1、編輯MySQL配置文件my.cnf
vi /etc/my.cnf
#編輯文件,找到[mysqld],在下面添加一行skip-grant-tables
[mysqld]
skip-grant-tables
service mysqld restart #重啓MySQL服務
2、進入MySQL控制檯
mysql -uroot -p
#直接按回車,這時不需要輸入root密碼。
3、修改root密碼
update mysql.user set authentication_string=password('123456') where User="root" and Host="localhost";
flush privileges;
grant all on *.* to 'root'@'localhost' identified by '123456' with grant option;
4、取消/etc/my.cnf中的skip-grant-tables
vi /etc/my.cnf
編輯文件,找到[mysqld],刪除skip-grant-tables這一行
:wq! #保存退出
5、重啓mysql
service mysqld restart
重啓mysql,這個時候mysql的root密碼已經修改爲123456
6、進入mysql控制檯
mysql -uroot -p123456