[問題解決]2003 Can not connect to Mysql Server on "xxxx.xxxx.xxxx.xxxx"

[問題解決]2003 Can not connect to Mysql Server on “xxxx.xxxx.xxxx.xxxx”

一般這種問題是因爲沒有授予遠程主機訪問本地Mysql權限的原因。
解決方式:

  • 第一步,在Mysql數據庫的user表中添加一條記錄:host字段爲%,用戶爲root(自己修改),後面的權限什麼的自己看着給。可以使用Mysql GUI修改,比如navicat之類的,也可通過命令行修改,命令如下:

    create user user@’%’ identified by ‘password’;
    grant all on . to user@’%’;
    flush privileges;

    注:user,password自己替換成自己數據庫配置的。* .*是所有庫,你也可以指定。

  • 第二步,修要修改Mysql的配置文件:

    對於5.6版本及以下的:
    修改配置/etc/mysql/my.cnf:bind-address = xxx.xxx.xxx.xxx(你本地的IP地址,不是127.0.0.1)

    5.7版本及以上
    修改配置/etc/mysql/mysql.conf.d/mysqld.cnf:bind-address = xxx.xxx.xxx.xxx(你本地的IP地址,不是127.0.0.1)
    或者你不想指定IP地址可以再添加一條配置:bind-address = 0.0.0.0

  • 第三步,重啓你的Mysql,這樣應該就可以了。
    sudo /etc/init.d/mysql restart

  • 如果你第一步沒有做:你可以按照下面的步驟完成剩餘的任務:

    Then stop and restart MySQL with the new my.cnf entry. Once running go to the terminal and enter the following command.

    lsof -i -P | grep :3306

    That should come back something like this with your actual IP in the xxx’s

    mysqld 1046 mysql 10u IPv4 5203 0t0 TCP xxx.xxx.xxx.xxx:3306 (LISTEN)

    If the above statement returns correctly you will then be able to accept remote users. However for a remote user to connect with the correct priveleges you need to have that user created in both the localhost and ‘%’ as in.

    CREATE USER ‘myuser’@’localhost’ IDENTIFIED BY ‘mypass’;
    CREATE USER ‘myuser’@’%’ IDENTIFIED BY ‘mypass’;

    then,

    GRANT ALL ON . TO ‘myuser’@’localhost’;
    GRANT ALL ON . TO ‘myuser’@’%’;

    and finally,

    FLUSH PRIVILEGES;
    EXIT;

    If you don’t have the same user created as above, when you logon locally you may inherit base localhost privileges and have access issues. If you want to restrict the access myuser has then you would need to read up on the GRANT statement syntax HERE If you get through all this and still have issues post some additional error output and the my.cnf appropriate lines.

    NOTE: If lsof does not return or is not found you can install it HERE based on your Linux distribution. You do not need lsof to make things work, but it is extremely handy when things are not working as expected.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章