ติดตั้ง Mysql & MariaDB บน Ubuntu


MariaDB

  • Install
$ sudo apt-get update
$ sudo apt-get install mariadb-server -y
$ mysql -V
  • Set secure
$ sudo mysql_secure_installation
...
- Enter current password for root (enter for none): Just press the Enter
- Set root password? [Y/n]: Y
- New password: Enter password
- Re-enter new password: Repeat password
- Remove anonymous users? [Y/n]: Y
- Disallow root login remotely? [Y/n]: Y
- Remove test database and access to it? [Y/n]:  Y
- Reload privilege tables now? [Y/n]:  Y
  • Allow All host
$ sudo vi /etc/mysql/mariadb.conf.d/50-server.cnf

edit or add bind-address 127.0.0.1 to 0.0.0.0

  • Restart
$ sudo systemctl restart mariadb.service
  • Check Port 3306 Allow
$ sudo netstat -anp | grep 3306

Mysql

  • Install
$ sudo apt-get update
$ sudo apt-get install mysql-server -y
$ mysql -V
  • Set secure
$ sudo mysql_secure_installation
...
- Enter current password for root (enter for none): Just press the Enter
- Set root password? [Y/n]: Y
- New password: Enter password
- Re-enter new password: Repeat password
- Remove anonymous users? [Y/n]: Y
- Disallow root login remotely? [Y/n]: Y
- Remove test database and access to it? [Y/n]:  Y
- Reload privilege tables now? [Y/n]:  Y
  • Allow All host
$ sudo vi /etc/mysql/mysql.conf.d/mysqld.cnf
...
bind-address = <ip address>
  • Restart
$ sudo systemctl restart mysql.service
  • Check Port 3306 Allow
$ sudo netstat -anp | grep 3306

[Both] How to access from Remote Clients

$mysql -u root 
...

CREATE USER 'xxx'@'localhost' IDENTIFIED BY 'xxxx';

GRANT ALL PRIVILEGES ON *.* TO 'xxx'@'localhost' WITH GRANT OPTION;

CREATE USER 'xxx'@'%' IDENTIFIED BY 'xxxx';

GRANT ALL PRIVILEGES ON *.* TO 'xxx'@'%' WITH GRANT OPTION;

FLUSH PRIVILEGES;

EXIT;
  • Try to connect
$ mysql -u database_user -p database_user_password -h database_server

Not suggest because not allow all IP addresses not secure