วันจันทร์ที่ 1 กุมภาพันธ์ พ.ศ. 2564

ติดตั้ง mysql server บน ubuntu 16.04 server

 

ติดตั้ง mysql server บน ubuntu 16.04 server

บันทึกนี้ปรับปรุงล่าสุดเมื่อวันที่ 13-06-2560
ดูแลโดย WIBOON

ก่อนอื่นให้เพิ่ม locale ด้วยคำสั่ง
mama@ubuntu:~$ sudo locale-gen en_US en_US.UTF-8 th_TH th_TH.UTF-8
mama@ubuntu:~$ sudo dpkg-reconfigure locales
กด Enter เพื่อใช้ค่าที่เตรียมนี้

1. ติดตั้ง MySQL server ลงใน host server
1.1 ติดตั้งโปรแกรม

mama@ubuntu:~$ sudo apt install mysql-server
The following NEW packages will be installed:
  libaio1 ........
  ........ mysql-server-core-5.7
After this operation, 162 MB of additional disk space will be used.
Do you want to continue? [Y/n] ตอบ Y
Configuring mysql-server-5.7
 New password for the MySQL "root" user: 

ตั้งรหัสผ่าน

ไฟล์สำหรับการตั้งค่า mysql server อยู่ที่ไหนกันบ้าง

mama@ubuntu:~$ ls -l /etc/mysql/mysql.conf.d/mysqld.cnf
-rw-r--r-- 1 root root 3028 Jul 14 23:09 /etc/mysql/mysql.conf.d/mysqld.cnf


และอันนี้เป็นไฟล์สำหรับการตั้งค่า mysql client ที่ได้มาด้วย
ตรวจสอบดูพาธของไฟล์ my.cnf และ mysql.cnf คืออันเดียวกัน

mama@ubuntu:~$ ls -l /etc/mysql/my.cnf /etc/mysql/mysql.cnf
lrwxrwxrwx 1 root root  24 Aug 23 10:27 /etc/mysql/my.cnf -> /etc/alternatives/my.cnf
-rw-r--r-- 1 root root 682 Jul 11 20:36 /etc/mysql/mysql.cnf


my.cnf เป็น symbolic link file ไปยังไฟล์จริงที่ mysql.cnf

mama@ubuntu:~$ ls -l /etc/alternatives/my.cnf
lrwxrwxrwx 1 root root 20 Aug 23 10:27 /etc/alternatives/my.cnf -> /etc/mysql/mysql.cnf
mama@ubuntu:~$


1.2 ปรับแต่งค่า bind address ที่ mysqld.cnf ให้ mysql client IP จากภายนอก สามารถ connect เข้ามายัง database ได้ ไม่เพียงแค่ localhost เท่านั้น
ถ้าตั้งค่า bind-address = IP ของ network interface จะหมายถึงอนุญาตเฉพาะ network นั้นเท่านั้น
ถ้าตั้งค่า bind-address = :: จะหมายถึงอนุญาตทั้ง IPv4 และ IPv6 ทุกเครื่อง (แต่ควรจะไปกำหนด host firewall rule ในการอนุญาตเฉพาะเครื่องที่จะให้เข้าถึง เช่น ใช้ ufw หรือ shorewall เป็นต้น)
mama@ubuntu:~$ sudo vi /etc/mysql/mysql.conf.d/mysqld.cnf
เดิม
bind-address = 127.0.0.1
เปลี่ยนเป็น
bind-address = ::

1.3 ต้อง restart mysql ด้วยเมื่อมีการแก้ไขค่า config
mama@ubuntu:~$ sudo systemctl restart mysql.service
และก็ควรตรวจสอบ status ด้วยว่า restart สำเร็จหรือไม่
mama@ubuntu:~$ sudo systemctl status mysql.service
ผลลัพธ์

- mysql.service - MySQL Community Server
   Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: en
   Active: active (running) since Mon 2016-08-29 15:38:14 ICT; 8s ago
Aug 29 15:38:13 ubuntu systemd[1]: Stopped MySQL Community Server.
Aug 29 15:38:13 ubuntu systemd[1]: Starting MySQL Community Server...
Aug 29 15:38:14 ubuntu systemd[1]: Started MySQL Community Server.


2. ตัวอย่างการสร้าง database ด้วย mysql client เป็นต้น
ชื่อ database คือ mydb
ชื่อ user คือ myuser
รหัสผ่านของ myuser คือ mypassword
'myuser'@'localhost' คือ myuser ที่ให้สิทธิเข้าถึง database ได้จาก client localhost เท่านั้น
'myuser'@'%' คือ myuser ที่ให้สิทธิเข้าถึง database ได้จาก client ใด ๆ ก็ได้
การใช้ myuser ทั้งสองแบบนี้ ถือว่าเป็นคนละยูสเซอร์ ดังนั้นรหัสผ่านจึงเป็นคนละอันกัน

เขียนคำสั่ง (แบบรอป้อน password ดังนี้)

mama@ubuntu:~$ mysql -u root -p
Enter password: 

แล้วสร้าง database, user ดังนี้

Welcome to the MySQL monitor.  Commands end with ; or \g.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> CREATE DATABASE mydb CHARACTER SET 'UTF8';
Query OK, 1 row affected (0.00 sec)

mysql> CREATE USER 'myuser'@'%' IDENTIFIED BY 'mypassword';
Query OK, 0 rows affected (0.00 sec)

mysql> GRANT ALL PRIVILEGES ON mydb.* TO 'myuser'@'%';
Query OK, 0 rows affected (0.00 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

mysql> exit
Bye


3. การแบ็กอัพ database
สมมติว่า user ชื่อ root และ password คือ 123456
ใช้คำสั่งดังนี้

mama@ubuntu:~$ mysqldump -u root -p123456 mydb > mydb.sql

หรือเขียนคำสั่งแบบรอป้อน password ดังนี้

mama@ubuntu:~$ mysqldump -u root -p mydb > mydb.sql
Enter password:


4. การรีสโตร์ database
ขั้นตอน
1. เมื่อเราทำเครื่อง server ใหม่ ให้เราสร้าง database ก่อนให้เหมือนกับที่สำรองเก็บไว้จากเครื่องเดิม
2. แล้ว import ข้อมูลจากไฟล์ mydb.sql กลับเข้าใน database ด้วยคำสั่ง

mama@ubuntu:~$ mysql -uroot -p123456 mydb < mydb.sql

หรือเขียนคำสั่งแบบรอป้อน password ดังนี้

mama@ubuntu:~$ mysql -uroot -p mydb < mydb.sql


5. ไม่ต้องป้อน password ทุกครั้ง และไม่เปิดเผย password ไว้ในบรรทัดคำสั่ง mysqldump
นำ password ไปเขียนไว้ในไฟล์ mypw.cnf เป็นวิธีการที่ใช้ในการ backup ทุกวันด้วย cron
สร้างไฟล์ mypw.cnf ด้วยคำสั่ง

mama@ubuntu:~$ sudo vi mypw.cnf

ภายในไฟล์จะมีข้อมูล username และ password เช่น

[mysqldump]
user=myuser
password=mypassword

ทดสอบอีกครั้ง

mama@ubuntu:~$ mysqldump --defaults-extra-file=mypw.cnf mydb > mydb.sql
https://opensource.cc.psu.ac.th/%E0%B8%95%E0%B8%B4%E0%B8%94%E0%B8%95%E0%B8%B1%E0%B9%89%E0%B8%87_mysql_server_%E0%B8%9A%E0%B8%99_ubuntu_16.04_server
https://www.hackthesec.co.in/2016/01/how-to-install-mysql-57-or-56-or-55-on.html

ไม่มีความคิดเห็น:

แสดงความคิดเห็น