美文网首页
阿里云服务器设置MySQL远程连接

阿里云服务器设置MySQL远程连接

作者: 在牛魔角上狂码 | 来源:发表于2018-11-01 00:38 被阅读0次

    第一种、改表法

    登录MySQL:
    mysql -uroot -p ,输入密码,登陆进去

    image.png
    选择mysql表,将host只能本地访问的 localhost 改为 %
    mysql>use mysql;   
    
    mysql>update user set host="%" where user="chase";  
    
    mysql> select host,user from user;
    +-----------+------------------+
    | host      | user             |
    +-----------+------------------+
    | %         | chase            |
    | localhost | debian-sys-maint |
    | localhost | mysql.session    |
    | localhost | mysql.sys        |
    | localhost | root             |
    +-----------+------------------+
    5 rows in set (0.00 sec)
    mysql>flush privileges;  //刷新权限
    
    mysql>exit;
    

    第二种、授权法

    登录mysql: mysql -uroot -p密码

    mysql>use mysql;
    
    //grant 权限 on 数据库.表 to '用户名'@"访问地址(%任意主机,localhost本地主机)" identified by '密码';
    mysql>grant all privileges  on 数据库.* to 'czhd'@"%" identified by '123456';
    
    mysql> select host,user from user;
    +-----------+------------------+
    | host      | user             |
    +-----------+------------------+
    | %         | czhd            |
    | %         | chase            |
    | localhost | debian-sys-maint |
    | localhost | mysql.session    |
    | localhost | mysql.sys        |
    | localhost | root             |
    +-----------+------------------+
    5 rows in set (0.00 sec)
    
    mysql>flush privileges;//刷新权限
    
    mysql>exit;
    

    注意:除了授权的数据库可以被创建外,其他数据库是不能被创建出来的,因为没有权限


    接下来:

    centos系统

    查看防火墙是否已授权 3306远程访问,没有的话就添加授权

    service iptables status
    

    编辑 iptables

    vim /etc/sysconfig/iptables
    

    添加 3306

    -A INPUT -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPT
    

    重启 iptables

    service iptables restart
    

    重启 mysql

    service mysql restart
    

    ubuntu系统

    查看 3306

    root@iZwz956snfyrvah6yq8sa4Z:~# netstat -an | grep 3306
    tcp        0      0 127.0.0.1:3306          0.0.0.0:*               LISTEN
    

    注m意:现在3306端口绑定的IP地址是本地的127.0.0.1

    修改mysql配置文件,我这里的文件路径是/etc/mysql/mysql.conf.d/mysqld.cnf

    vim /etc/mysql/mysql.conf.d/mysqld.cnf
    
    image.png

    找到

    bind-address      = 127.0.0.1
    

    在前面加上 #号,将它注释掉

    重启MySQL

    service mysql restart
    

    再次查看3306端口

    root@iZwz956snfyrvah6yq8sa4Z:~# netstat -an | grep 3306
    tcp6       0      0 :::3306                 :::*                    LISTEN 
    

    最后登录阿里云,在服务器的云服务器的安全组中添加3306规则

    网络和安全组 ==》安全组配置


    image.png

    配置规则


    image.png
    选择一条已有的规则,点击克隆
    image.png

    使用MySQL可视化工具来连接

    image.png

    登录成功

    数据库的权限参考:Mysql数据库权限详细解释

    相关文章

      网友评论

          本文标题:阿里云服务器设置MySQL远程连接

          本文链接:https://www.haomeiwen.com/subject/ajaztqtx.html