MySQL/MariaDB Server: Bind To Multiple IP Address

September 8, 2019

I have MySQL/MariaDB database system running on a FreeBSD/Linux server. The server has multiple ip address. The mysqld used by many websites hosted on two other CentOS Linux based servers. I would like to bind MySQL sever running on a Linux or Unix-like server to more than one IP address such as,, and How can I bind mysqld to multiple ips?

[donotprint][/donotprint] Short answer – No. You can not bind to multiple IP address under MySQL database server either running on FreeBSD or Linux/Unix-like operating systems.

Long answer

From the mysql documentation:

The MySQL server listens on a single network socket for TCP/IP connections. This socket is bound to a single address, but it is possible for an address to map onto multiple network interfaces. The default address is To specify an address explicitly, use the –bind-address=addr option at server startup, where addr is an IPv4 address or a host name. If addr is a host name, the server resolves the name to an IPv4 address and binds to that address. The server treats different types of addresses as follows:

  1. If the address is, the server accepts TCP/IP connections on all server host IPv4 interfaces.
  2. If the address is a “regular” IPv4 address (such as, the server accepts TCP/IP connections only for that particular IPv4 address.

Using a firewall

Consider the following setup:

|                   +==========Server_IP1       |
|  +--------+       |                                      |
|  | mysqld +-------+==========Server_IP2      +-------> LAN/WAN ---->
|  +--------+       |                                      |
|  Server_IP0       +==========Server_IP3 with |
|                                              |
           UNIX/Linux Box called


  1. Mysqld server will bind to all IPs on all interfaces.
  2. Use the firewall to control access to the mysqld running on this server. Make sure you only allow connections to 202.54.1.{2,10,15} tcp port # 3306


You can set bind-address directive in my.cnf. Edit /etc/my.cnf or /usr/local/etc/my.cnf, run:
# vi /usr/local/etc/my.cnf
# vi /etc/my.cnf
Set the address to

bind-address    =

Make sure you delete the following line or comment out the following line:


Save and close the file. Next setup the firewall and allows connection from or to select IPs only.

Linux iptables rules

Block all incomming connections to mysqld TCP port # 3306 except for,, and

 ### Now, allow,, and ###
/sbin/iptables -A INPUT -p tcp -s --dport 3306 -j ACCEPT
/sbin/iptables -A INPUT -p tcp -s --dport 3306 -j ACCEPT
/sbin/iptables -A INPUT -p tcp -s --dport 3306 -j ACCEPT
## Block all connections to 3306 ##
/sbin/iptables -A INPUT -p tcp --dport 3306 -j DROP


## Block all connections to 3306 except for three ips##
/sbin/iptables -A INPUT -p tcp --dport 3306 ! -s -j DROP
/sbin/iptables -A INPUT -p tcp --dport 3306 ! -s -j DROP
/sbin/iptables -A INPUT -p tcp --dport 3306 ! -s -j DROP

Save the settings:
# service iptables save
Type the following command to verify new settings:

echo -e "target     prot opt source               destinationn$(iptables -L INPUT -n | grep 3306)"

Sample outputs:

Fig.01: MySQL Server: Iptables negate a range in Iptables

pf firewall rules

Use the following syntax in your /etc/pf.conf:

 ## our interface ##
## do not block mysqld on ##
mysqld_ip="{ !, !, ! }"
## Block everything for tcp port number 3306 except $mysqld_ip  ###
block in on $ext_if proto tcp from any to  $mysqld_ip port 3306

Load updated firewall rules using the pf.conf file:
# pfctl -f /etc/pf.conf
Show the current ruleset, enter:
# pfctl -sr
# pfctl -sr | grep 3306

Sample outputs:

Fig.02: BSD PF Firewall Block All IPs Except A few IPS to MySQL Port

Add/update mysql sever user and permissions settings

Let us assume that you are always making connection from the remote IP called for existing mysqld database called foo for user bar, To grant access to this IP address type the following command at mysql server
mysql> update db set Host='' where Db='foo';
mysql> update user set Host='' where user='bar';

Restart / reload the mysql server

Type the following command to restart the mysqld. If you are on Red Hat Enterprise Linux and friends:
# service mysqld restart
Debian Linux and friends use the following command:
# service mysql restart
FreeBSD unix user type the following command to restart the mysql server:
# /usr/local/etc/rc.d/mysql-server stop && /usr/local/etc/rc.d/mysql-server start

How do I test my settings?

Type the following command from client ( to connect to, enter:
[[email protected] ]$ mysql -u foo -h -P 3306 -p bar

  1. -u foo : The MySQL user name to use when connecting to the server.
  2. -h : Connect to the MySQL server on the given host/ip address.
  3. -P 3306 : The TCP/IP port number to use for the connection.
  4. bar : The database name.

Posted by: SXI ADMIN

The author is the creator of SXI LLC and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

Article Tags:
Article Categories:
How To

Comments are closed.