Enable remote access rights for MySQL under Linux and open port 3306 in the firewall

Enable remote access rights for MySQL under Linux and open port 3306 in the firewall

Enable remote access rights for mysql

By default, MySQL users do not have remote access permissions, so when the program and the database are not on the same server, we need to enable remote access permissions for MySQL.

There are two mainstream methods, the table modification method and the authorization method.

Relatively speaking, the table modification method is easier, and I personally prefer this method. Therefore, I will only post the table modification method here.

1. Log in to MySQL

mysql -u root -p

2. Modify the user table of the MySQL database and change the host item from localhost to %. %This means that any host is allowed to access. If only a certain IP is allowed to access, you can change it to the corresponding IP. For example, you can change localhost to 192.168.1.123, which means that only the IP 192.168.1.123 in the local area network is allowed to remotely access MySQL.

mysql> use mysql; 
mysql> update user set host = '%' where user = 'root'; 
mysql> select host, user from user; 
mysql> flush privileges;

Open port 3306 on the firewall

1. Open the firewall configuration file

vi /etc/sysconfig/iptables

2. Add the following line

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

3. Restart the firewall

service iptables restart

Note: The statement to open port 3306 must be placed before icmp-host-prohibited

Attachment: Personal configuration

# Firewall configuration written by system-config-firewall
# Manual customization of this file is not recommended.
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -i eth0 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
-A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT
-A FORWARD -p icmp -j ACCEPT
-A FORWARD -i lo -j ACCEPT
-A FORWARD -i eth0 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Solution to mysql connection blocked by firewall under linux
  • Solution for adding iptables firewall policy to MySQL service

<<:  JavaScript implementation of drop-down list

>>:  foreman ubuntu16 quick installation

Recommend

How to set up Windows Server 2019 (with pictures and text)

1. Windows Server 2019 Installation Install Windo...

Complete steps to achieve high availability with nginx combined with keepalived

Preface In order to meet the high availability of...

Summary of tips for making web pages

Preface This article mainly summarizes some of th...

How to filter out certain libraries during mysql full backup

Use the --all-database parameter when performing ...

MySQL graphical management tool Navicat installation steps

Table of contents Preface 1. Arrange the installa...

js to implement collision detection

This article example shares the specific code of ...

Deploy Varnish cache proxy server based on Centos7

1. Varnish Overview 1. Introduction to Varnish Va...

Troubleshooting MySQL high CPU load issues

High CPU load caused by MySQL This afternoon, I d...

How to view mysql binlog (binary log)

For example, when you create a new table or updat...

Pure HTML+CSS to achieve Element loading effect

This is the effect of the Element UI loading comp...

MySQL common backup commands and shell backup scripts sharing

To back up multiple databases, you can use the fo...

Example of how to implement MySQL cascading replication

The so-called cascading replication is that the m...

Simple example of adding and removing HTML nodes

<br />Simple example of adding and removing ...

Count the list tags in HTML

1. <dl> defines a list, <dt> defines ...