1, Cài đặt từ gói
sudo apt install mysql-server
sudo mysql_secure_installation
sudo nano /etc/mysql/mysql.conf.d/mysql.cnf
[mysqld]
user = mysql
bind-address = 0.0.0.0
mysqlx-bind-address = 0.0.0.0
key_buffer_size = 16M
myisam-recover-options = BACKUP
log_error = /var/log/mysql/error.log
max_binlog_size = 100M2
sudo systemctl restart mysql.service
2, Bật mysql
sudo systemctl start mysql.service
3, Cài đặt instance thứ 2
mkdir -p /opt/second_server_data/
chmod –reference /var/lib/mysql /opt/second_server_data/
chown –reference /var/lib/mysql /opt/second_server_data/
sudo cp /etc/mysql/mysql.conf.d/mysqld.cnf /etc/mysql/my_multi.cnf
cat /etc/mysql/mysql.conf.d/mysqld.cnf >> /etc/mysql/my_multi.cnf
cat /etc/mysql/my_multi.cnf
[mysqld1]
server-id = 1
user = mysql
datadir = /var/lib/mysql
bind-address = 0.0.0.0
mysqlx-bind-address = 0.0.0.0
mysqlx = 0
key_buffer_size = 16M
myisam-recover-options = BACKUP
log_error = /var/log/mysql/error.log
max_binlog_size = 100M
[mysqld2]
server-id = 2
user = mysql
pid-file = /var/run/mysqld/second_server.pid
socket = /var/run/mysqld/second_server.sock
port = 3307
datadir = /opt/second_server_data/
bind-address = 0.0.0.0
mysqlx-bind-address = 0.0.0.0
mysqlx = 0
key_buffer_size = 16M
myisam-recover-options = BACKUP
log_error = /var/log/mysql/second_server_error.log
max_binlog_size = 100M
[mysqld_multi]
mysqld = /usr/bin/mysqld_safe
mysqladmin = /usr/bin/mysqladmin
user = multi_admin
pass = multipass
4, Chỉnh sửa apparmor
sudo nano /etc/apparmor.d/usr.sbin.mysqld
bổ sung:
/opt/second_server_data/ r,
/opt/second_server_data/** rwk,
/var/run/mysqld/second_server.pid rw,
/var/run/mysqld/second_server.sock rw,
/var/run/mysqld/second_server.sock.lock rw,
/run/mysqld/second_server.pid rw,
/run/mysqld/second_server.sock rw,
/run/mysqld/second_server.sock.lock rw,
sudo service apparmor restart
mysqld –initialize –user=mysql –datadir=/opt/second_server_data/
sudo ls -l /opt/second_server_data/
sudo ls -l /opt/second_server_data/
mysql> SHOW VARIABLES LIKE ‘log_error’;
default root password at: /var/log/syslog or at /var/log/mysql/error.log
sudo mysqld_safe –no-defaults –datadir=/opt/second_server_data/ –port=3307 –mysqlx=0 –socket=/var/run/mysqld/second_server.sock
5, Start DB
mysql -h 127.0.0.1 -P 3307 -u root -p
ALTER USER ‘root’@’localhost’ IDENTIFIED BY ‘second_server’;
CREATE USER ‘multi_admin’@’%’ IDENTIFIED BY ‘multipass’;
GRANT SHUTDOWN ON . TO ‘multi_admin’@’%’;
mysql -h 127.0.0.1 -P 3306 -u root -p
CREATE USER ‘multi_admin’@’%’ IDENTIFIED BY ‘multipass’;
GRANT SHUTDOWN ON . TO ‘multi_admin’@’%’;
To start all servers at once run the command below.
sudo mysqld_multi –defaults-file=/etc/mysql/my_multi.cnf –no-log start