การเข้าถึง Mysql ที่อยู่ Host ต่างกัน
การเข้าถึงฐานข้อมูล MySQL ที่ไม่มีตรวจสอบการรักษาความปลอดภัย แต่ในบางครั้งเราต้องการเข้าถึงข้อมูลสำหรับผู้ใช้งานโดยเฉพาะสำหรับ web server จิงที่ต้องการระบบตรวจสอบ จึงจะขอกล่าวิธีการเข้าถึงแบบ ไม่มีการรักษาความปลอดภัย และระบบที่ได้จัดการระบบความปลอภัย โดยการเข้าจากภายนอกของ host
การเข้าถึง MySQL
คุณใช้คำสั่งจาก commandline เพื่อการติดต่อ MySQL สำหรับการเข้าไปใช้งาน มีขั้นตอนดังนี้
Step : 1 การ Login เข้า SSH ถ้า Server อยู่ภายนอกสถานที่
คำสั่งสำหรับการเข้าถึงข้อมูล โดยทำการ Login ด้วยคำสั่งนี่ :
ssh [email protected] Step # 2: ทำการแก้ไขไฟล์ my.cnf เป็นไฟลที่ควบคุมการทำงาน MySQL ทั้งหมด (ควรสำรองไฟล์ก่อนถ้าไม่ชำนาญ)
การติดต่อเพื่อแก้ไขไฟล์ my.cnf สำหรับคอนฟิกค่าต่างๆ โดยใช้ editor ต่างๆที่มีในระบบปฏิบัติการนั้น ส่วนใหญ่จะ editor ที่ชื่อ vi ในระบบปฏิบัติการตระกูล Linux Unix
- ถ้าใช้ Linux ที่ชื่อ Debian Linux ไฟล์จะอยู่ตำแหน่ง path ที่ /etc/mysql/my.cnf location
- ถ้าเป็น Linux ที่ชื่อ Red Hat Linux/Fedora/Centos Linux ไฟล์จะอยู่ตำแหน่ง path ที่ /etc/my.cnf location
- แล้วถ้าเป็น Unix ที่ชื่อว่า FreeBSD เราต้องทำการสร้างไฟล์ my.cnf ขั้นมา ตำแหน่ง path ที่ /var/db/mysql/my.cnf
แก้ไขไฟล์ /etc/my.cnf, run:# vi /etc/my.cnf
Step # 3: แก้ในส่วน นี้
[mysqld] bind-address=YOUR-SERVER-IP
ตัวอย่าง เช่น IP 192.168.1.10
[mysqld] 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 = 192.168.1.10 # skip-networking .... .. ....
Step# 4 บันทึกไฟล์ที่ทำการแก้ไข
ทำการรีสตาร์ MySQL# /etc/init.d/mysql restart
Step # 5 กำหนดสิทธิการเข้าถึง
ใช้คำสั่งในการเข้าถึง mysql server:$ mysql -u root -p mysql
แล้วพิมพ์คำสั่งดังนี้
Mysql> CREATE DATABASE foo;
mysql> GRANT ALL ON foo.* TO bar@'202.54.10.20' IDENTIFIED BY 'PASSWORD';
mysql> GRANT ALL ON foo.* TO bar@'202.54.10.20' IDENTIFIED BY 'PASSWORD';
หรือแก้ไขสิทธิการเข้าถึงmysql> update db set Host='202.54.10.20' where Db='Datadb';
mysql> update user set Host='202.54.10.20' where user='webadmin';
Step # 5: ออกจาการใช้งาน MySQL
mysql> exit
Step # 6: เปิด port 3306
ด้วยคำสั่ง
/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
Finally save all rules:# service iptables save
A sample FreeBSD / OpenBSD pf rule ( /etc/pf.conf)
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
Step # 7: Test it
From your remote system or your desktop type the following command:$ mysql -u webadmin –h 65.55.55.2 –p
Where,
- -u webadmin: webadmin is MySQL username
- -h IP or hostname: 65.55.55.2 is MySQL server IP address or hostname (FQDN)
- -p : Prompt for password
You can also use telnet to connect to port 3306 for testing purpose:$ telnet 65.55.55.2 3306