出現這個問題的原因之一是權限的問題,也就是說你的電腦可能沒有權限訪問mysql數據庫。
講道理這種情況其實基本上不該遇到,因為我們在安裝mysql之后,root其實是有最高權限的,而且很少會有人去修改root的權限。
這個問題的解決方法就是授權。授權命令大概是這樣的:
grant all privileges on *.* to 'root'@'我電腦的ip地址' identified by '密碼';
使用mysql -u root -p進入mysql
grant all privileges on *.* to 'root'@'192.168.0.103' identified by '123456';
如果你是本地登錄的,那么:
grant all privileges on *.* to 'root'@'localhost' identified by '123456';
當然你也可以直接改成這樣:
grant all privileges on *.* to 'root'@'%' identified by '123456';
就可以給所有ip都設定root登陸了。
如果授權成功,會有Query OK的提示。
然后:
flush privileges;
這個是刷新授權的意思,如果沒有這句話,授權可能無法立刻生效。
exit;