配置Mysql远程连接

一.赋予某个用户权限

  1.赋予权限格式:grant 权限 on 数据库对象 to 用户@IP(或者相应正则)

    注:可以赋予select,delete,update,insert,index等权限精确到某一个数据库某一个表。

    GRANT ALL PRIVILEGES ON *.* TO '用户名'@'%' IDENTIFIED BY '密码' WITH GRANT OPTION;

    这里表示赋予该用户所有数据库所有表(*.*表示所有表),%表示所有IP地址。

 

  2.刷新权限:FLUSH PRIVILEGES;

  3.查看权限:select user,host from mysql.user;

  

 

     注:mysql是自带的数据库名,里面的表是Mysql相关配置信息端口,事件等等,其中user存放用户信息

      

 

  3.按理就可以在其他IP地址连接了(使用mysql -u用户名 -h服务器ip地址 -P端口号 -p)

二.意外

  1.配置文件种指定了blind-address:

    查看Mysql配置文件种(一般是/etc/my.cnf种)是否指定了blind-address,这表示只能是某个或某几个ip能连接。如果有就将它注释了,前面加#号注释。然后从启mysql。

    重启mysql:service mysqld restart,如果安装的是Mariadb(我的就是),则需要使用systemctl restart mariadb.service

  2.防火墙的原因:

    可能会报:ERROR 2003 (HY000): Can't connect to MySQL server on '你要连接的IP' (111)。

    原因:1.可能是Mysql端口不对(默认是3306),只需加参数 -P 你的端口指定就行;

查看mysql服务端口 

       2.还有可能是有防火墙阻止,可以通过telnet来测试(可以直接关闭防火墙)。

          防火墙相关命令:

            (1)查看防火墙状态:service  iptables status或者systemctl status firewalld或者firewall-cmd --state

            (2)暂时关闭防火墙:systemctl stop firewalld或者service  iptables stop或者systemctl stop firewalld.service

            (3)永久关闭防火墙:systemctl disable firewalld或者chkconfig iptables off或者systemctl disable firewalld.service

            (4)重启防火墙:systemctl enable firewalld或者service iptables restart  或者systemctl restart firewalld.service

              (5)永久关闭后重启:chkconfig iptables on

  

  3.端口未开启:(我遇到的就是这个原因)

      Mysql:ERROR 2003 (HY000) 110(连接超时)

      查看你的服务器是否把对应端口打开,未打开启动就行了。    

posted @ 2019-05-14 09:03  biu嘟  阅读(81921)  评论(0编辑  收藏  举报