Mysql remote connect on CentOS

Fixed access IP

  1. check /etc/my.cnf.d/mariadb-server.cnf

  2. vi mariadb-server.cnf

    #
    # Allow server to accept connections on all interfaces.
    #
    #bind-address=127.0.0.1
    bind-address=0.0.0.0
    
  3. restart mysqld
    systemctl restart mysqld
    

change mysql host

mysql -uroot -p

use mysql;
update user set Host = '%' where Host = 'localhost' and User='root';
flush privileges;
select host,user,password from user;
MariaDB [mysql]> select host,user,password from user;
+-----------+---------+-------------------------------------------+
| host      | user    | password                                  |
+-----------+---------+-------------------------------------------+
| %         | root    | *6E74AC414148116F98D67247A60A8A2297EC0653 |
| 127.0.0.1 | root    | *6E74AC414148116F98D67247A60A8A2297EC0653 |
| ::1       | root    | *6E74AC414148116F98D67247A60A8A2297EC0653 |
| localhost | joelwei | *0EC1B82C7A7485CF36D0C56502ACCFA15D2582AE |
| localhost | django  | *6B204722E06FE593EB6F7285A64602450BBE955C |
+-----------+---------+-------------------------------------------+
5 rows in set (0.000 sec)

Firewall Setup

 firewall-cmd --list-all
cd /usr/lib/firewalld/services
vi mysql.xml
<?xml version="1.0" encoding="utf-8"?>
<service>
  <short>MySQL</short>
  <description>MySQL Database Server</description>
  <port protocol="tcp" port="3306"/>
</service>
firewall-cmd  --permanent --zone=public --add-service=mysql
firewall-cmd --reload
getsebool httpd_can_network_connect_db
httpd_can_network_connect_db --> on

if off and turn on

setsebool httpd_can_network_connect_db on

Test it

mysql -u root -h 192.168.0.XX -p

success!

發佈留言