How To EnableRemoteAccessToMySQLServer (Scribe)
How To EnableRemoteAccessToMySQLServer (Scribe)
Step # 1: Login Using SSH (if server is outside your data center)
First, login over ssh to remote MySQL database server:
ssh [email protected]
If you are using Debian Linux file is located at /etc/mysql/my.cnf location If you are using Red Hat Linux/Fedora/Centos Linux file is located at /etc/my.cnf location If you are using FreeBSD you need to create a file /var/db/mysql/my.cnf
Make sure line skip-networking is commented (or remove line) and add following line
bind-address=YOUR-SERVER-IP
For example, if your MySQL server IP is 65.55.55.2 then entire block should be look like as follows:
[mysqld]
Page 2 of 4
user = pid-file = socket = port = basedir = datadir = tmpdir = language = bind-address = # skip-networking .... .. .... mysql /var/run/mysqld/mysqld.pid /var/run/mysqld/mysqld.sock 3306 /usr /var/lib/mysql /tmp /usr/share/mysql/English 65.55.55.2
Where,
bind-address : IP address to bind to. skip-networking : Dont listen for TCP/IP connections at all. All interaction with mysqld must be made via Unix sockets. This option is highly recommended for systems where only local requests are allowed. Since you need to allow remote connection this line should be removed from my.cnf or put it in comment state.
If you are using RHEL / CentOS / Fedora / Scientific Linux, type the following command to restart the mysql server:
# /etc/init.d/mysqld restart
If you are using FreeBSD, 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
OR
# /usr/local/etc/rc.d/mysql-server restart
Grant access to a new database If you want to add a new database called foo for user bar and remote IP 202.54.10.20 then you need to type the following commands at mysql> prompt:mysql> CREATE
DATABASE foo; mysql> GRANT ALL ON foo.* TO bar@'202.54.10.20' IDENTIFIED BY 'PASSWORD';
Page 3 of 4
Let us assume that you are always making connection from remote IP called 202.54.10.20 for database called webdb for user webadmin, To grant access to this IP address type the following command At mysql> prompt for existing database, enter:
mysql> update db set Host='202.54.10.20' where Db='webdb'; mysql> update user set Host='202.54.10.20' where user='webadmin';
OR only allow remote connection from your web server located at 10.5.1.3:
/sbin/iptables -A INPUT -i eth0 -s 10.5.1.3 -p tcp --destination-port 3306 -j ACCEPT
Step # 8: Test it
From your remote system or your desktop type the following command:
$ mysql -u webadmin h 65.55.55.2 p
Where,
Page 4 of 4
-h IP or hostname: 65.55.55.2 is MySQL server IP address or hostname (FQDN) -p : Prompt for password
You can also use the telnet or nc command to connect to port 3306 for testing purpose:
$ echo X | telnet -e X 65.55.55.2 3306
OR
$ nc -z -w1 65.55.55.2 3306
Sample outputs:
Connection to 65.55.55.2 3306 port [tcp/mysql] succeeded!