全部博文(470)
分类: Mysql/postgreSQL
2009-10-02 16:01:45
By default, MySQL database server remote access disabled for security reasons. However, some time you need to provide the remote access to database server from home or from web server.
You need type the following commands which will allow remote connections:
First, login over ssh to remote MySQL database server
Once connected you need edit the mysql configuration file my.cfg using text editor such as vi.
# vi /etc/my.cnf
[mysqld]
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]
Where,
user = mysql
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
port = 3306
basedir = /usr
datadir = /var/lib/mysql
tmpdir = /tmp
language = /usr/share/mysql/English
bind-address = 65.55.55.2
# skip-networking
....
..
....
Restart your mysql service to take change in effect:# /etc/init.d/mysql restart
# mysql -u root -p mysql
Grant access to new database
If you want to add new database called foo for user bar and remote IP
202.54.10.20 then you need to type following commands at mysql>
prompt:mysql> CREATE DATABASE foo;
mysql> GRANT ALL ON foo.* TO bar@'202.54.10.20' IDENTIFIED BY 'PASSWORD';
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:mysql> update db set Host='202.54.10.20' where Db='webdb';
mysql> update user set Host='202.54.10.20' where user='webadmin';
Type exit command to logout mysql:mysql> exit
You need to open port 3306 using iptables or BSD pf firewall.
/sbin/iptables -A INPUT -i eth0 -p tcp --destination-port 3306 -j ACCEPT
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
OR only allow remote connection from your lan subnet 192.168.1.0/24:
/sbin/iptables -A INPUT -i eth0 -s 192.168.1.0/24 -p tcp --destination-port 3306 -j ACCEPT
pass in on $ext_if proto tcp from any to any port 3306
OR allow only access from your web server located at 10.5.1.3:
pass in on $ext_if proto tcp from 10.5.1.3 to any port 3306 flags S/SA synproxy state
From remote system or your desktop type the command:
$ mysql -u webadmin –h 65.55.55.2 –p
Where,
You can also use telnet to connect to port 3306 for testing purpose:$ telnet 65.55.55.2 3306