ติดตั้ง MySQL บน Ubuntu 20.04

ติดตั้ง MySQL

sudo apt update
sudo apt install mysql-server

ตรวจสอบสถานะ

systemctl status mysql

ลองใช้งาน

$ sudo mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.32-0ubuntu0.20.04.2 (Ubuntu)

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

สร้าง user

CREATE USER 'jack'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON * . * TO 'jack'@'localhost';
FLUSH PRIVILEGES;
SELECT user,authentication_string,plugin,host FROM mysql.user;

ทดสอบที่ local จะเข้าได้ละ

$ sudo mysql -u jack  -p

เข้าจากเครื่องอื่น

1 Access mysqld.cnf File

sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf

2 Change Bind-Address IP

You now have access to the MySQL server configuration file. Scroll down to the bind-address line and change the IP address. The current default IP is set to 127.0.0.1. This IP limits MySQL connections to the local machine.

The new IP should match the address of the machine that needs to access the MySQL server remotely. For example, if you bind MySQL to 0.0.0.0, then any machine that reaches the MySQL server can also connect with it.

bind-address            = 0.0.0.0
sudo systemctl restart mysql

สร้าง remote user โดย remote ไปจากเครื่อง 192.168.1.124

CREATE USER 'jack'@'192.168.1.124' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON * . * TO 'jack'@'192.168.1.124';
FLUSH PRIVILEGES;