1、登錄進入mysql,在mysql-5.7.18/bin目錄下執行命令:
./mysql -uroot -p -S /usr/local/mysql-5.7.18/data/3307/mysql.sock
其中 -p 是指定密碼,如果沒有密碼則可以不寫 -p,-S是指定sock文件,mysql.sock文件是服務器與本機客戶端進行通信的ip與端口文件;
或者使用用端口、主機登錄 ./mysql -uroot -p -P3307 -h127.0.0.1 登錄進入MySQL
2、修改mysql的密碼,執行:
alter user 'root'@'localhost' identified by '123456';(其中123456是我們設置的密碼)
3、授權遠程訪問,執行命令:(這樣遠程客戶端才能訪問)
grant all privileges on *.* to root@'%' identified by '123456';
其中*.* 的第一個*表示所有數據庫名,第二個*表示所有的數據庫表;
root@'%' 中的root表示用戶名,%表示ip地址,%也可以指定具體的ip地址,比如root@localhost,root@192.168.10.129
4、執行以下如下命令刷新權限:
flush privileges;
以上步驟一次性執行:
alter user 'root'@'localhost' identified by '123456';
grant all privileges on *.* to root@'%' identified by '123456';
flush privileges;