We want to set up MySQL server replication. We will use VMWare Workstation to set up two virtual machines – master and slave.
On Distributing Operating Systems Course we will use RS1 (as master) and RS2 (as slave).
We want to set up MySQL server replication. We will use VMWare Workstation to set up two virtual machines – master and slave.
On Distributing Operating Systems Course we will use RS1 (as master) and RS2 (as slave).
We will use Linux Debian Jessie distribution.
We need some preparation:
1 2 3 4 5 6 7 8 9 10 |
root@lsa-template:~# apt-get update Ign http://ftp.pl.debian.org jessie InRelease Get:1 http://ftp.pl.debian.org jessie-updates InRelease [142 kB] (....) Get:8 http://security.debian.org jessie/updates/main Translation-en [143 kB] Fetched 804 kB in 7s (106 kB/s) Reading package lists... Done root@lsa-template:~# |
1 2 3 4 5 6 7 8 |
root@lsa-template:~# apt-get upgrade Reading package lists... Done Building dependency tree Reading state information... Done (...) root@lsa-template:~# |
1 2 3 4 5 6 7 |
root@lsa-template:~# apt-get dist-upgrade Reading package lists... Done Building dependency tree (...) root@lsa-template:~# |
We’re now ready to install mysql.
Install mysql-server package on both machines DB1 and DB2 (in the box below we present installation on DB1).
Note:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
root@DB1:~# apt-get install mysql-server Reading package lists... Done Building dependency tree Reading state information... Done The following extra packages will be installed: libaio1 libdbd-mysql-perl libdbi-perl libhtml-template-perl libmysqlclient18 libterm-readkey-perl mysql-client-5.5 mysql-common mysql-server-5.5 mysql-server-core-5.5 Suggested packages: libclone-perl libmldbm-perl libnet-daemon-perl libsql-statement-perl libipc-sharedcache-perl tinyca The following NEW packages will be installed: libaio1 libdbd-mysql-perl libdbi-perl libhtml-template-perl libmysqlclient18 libterm-readkey-perl mysql-client-5.5 mysql-common mysql-server mysql-server-5.5 mysql-server-core-5.5 0 upgraded, 11 newly installed, 0 to remove and 0 not upgraded. Need to get 8,725 kB of archives. After this operation, 96.3 MB of additional disk space will be used. Do you want to continue? [Y/n] Y Get:1 http://ftp.pl.debian.org/debian/ jessie/main libaio1 amd64 0.3.110-1 [9,312 B] Get:2 http://ftp.pl.debian.org/debian/ jessie/main mysql-common all 5.5.49-0+deb8u1 [81.1 kB] Get:3 http://ftp.pl.debian.org/debian/ jessie/main libmysqlclient18 amd64 5.5.49-0+deb8u1 [675 kB] Get:4 http://ftp.pl.debian.org/debian/ jessie/main libdbi-perl amd64 1.631-3+b1 [816 kB] Get:5 http://ftp.pl.debian.org/debian/ jessie/main libdbd-mysql-perl amd64 4.028-2+b1 [119 kB] Get:6 http://ftp.pl.debian.org/debian/ jessie/main libterm-readkey-perl amd64 2.32-1+b1 [28.0 kB] Get:7 http://ftp.pl.debian.org/debian/ jessie/main mysql-client-5.5 amd64 5.5.49-0+deb8u1 [1,667 kB] Get:8 http://ftp.pl.debian.org/debian/ jessie/main mysql-server-core-5.5 amd64 5.5.49-0+deb8u1 [3,415 kB] Get:9 http://ftp.pl.debian.org/debian/ jessie/main mysql-server-5.5 amd64 5.5.49-0+deb8u1 [1,769 kB] (...) Setting up libdbi-perl (1.631-3+b1) ... Setting up libdbd-mysql-perl (4.028-2+b1) ... Setting up libterm-readkey-perl (2.32-1+b1) ... Setting up mysql-client-5.5 (5.5.49-0+deb8u1) ... Setting up mysql-server-core-5.5 (5.5.49-0+deb8u1) ... Setting up mysql-server-5.5 (5.5.49-0+deb8u1) ... 160614 0:17:04 [Warning] Using unique option prefix key_buffer instead of key_buffer_size is deprecated and will be removed in a future release. Please use the full name instead. 160614 0:17:04 [Note] /usr/sbin/mysqld (mysqld 5.5.49-0+deb8u1) starting as process 22565 ... Setting up libhtml-template-perl (2.95-1) ... Setting up mysql-server (5.5.49-0+deb8u1) ... Processing triggers for libc-bin (2.19-18+deb8u4) ... Processing triggers for systemd (215-17+deb8u4) ... root@DB1:~# |
We can now set up master server on DB1.
Default configuration of mysql needs some modifications to become a master.
What we will do and why:
Edit /etc/mysql/my.cnf and add the following lines in [mysqld] section:
1 2 |
log-bin=mysql-bin server-id=1 |
Note:
1 2 |
bind-address = 0.0.0.0 #skip-networking |
Finally we need to restart the mysql service for the changes to be applied:
1 2 |
root@DB1:~# service mysql restart root@DB1:~# |
You need to login to mysql and create user, grant privileges and apply the config:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
root@DB1:~# mysql -u root -p mysql Enter password: Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 37 Server version: 5.5.49-0+deb8u1-log (Debian) Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved. 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> CREATE USER 'repl'@'%' IDENTIFIED BY 'qwerty'; Query OK, 0 rows affected (0.00 sec) mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%'; Query OK, 0 rows affected (0.00 sec) mysql> FLUSH PRIVILEGES; Query OK, 0 rows affected (0.00 sec) mysql> Bye root@DB1:~# |
It’s good to have a test database and test table on which we will test our replication cluster:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
root@DB1:~# mysql -u root -p mysql Enter password: Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 38 Server version: 5.5.49-0+deb8u1-log (Debian) Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved. 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> create database testdb; Query OK, 1 row affected (0.00 sec) mysql> use testdb; Database changed mysql> create table testtable (i int); Query OK, 0 rows affected (0.01 sec) mysql> flush tables; Query OK, 0 rows affected (0.01 sec) mysql> Bye root@DB1:~# |
We’ll also need a username and password for mysql connections (we could use root, but the GRANT query is necessary).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
root@DB1:~# mysql -u root -p mysql Enter password: Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 38 Server version: 5.5.49-0+deb8u1-log (Debian) Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved. 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> CREATE USER 'testuser'@'%' IDENTIFIED BY 'qwerty'; Query OK, 1 row affected (0.00 sec) mysql> GRANT ALL PRIVILEGES ON testdb.* TO 'testuser'@'%'; Query OK, 0 rows affected (0.01 sec) mysql> FLUSH PRIVILEGES; Query OK, 0 rows affected (0.01 sec) mysql> Bye root@DB1:~# |
Note:
We need to run commands listed above on both RS1 and RS2.
We need to set the binary log position and name of the binary log file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
root@DB1:~# mysql -u root -p mysql Enter password: Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 37 Server version: 5.5.49-0+deb8u1-log (Debian) Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved. 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> FLUSH TABLES WITH READ LOCK; Query OK, 0 rows affected (0.00 sec) mysql> SHOW MASTER STATUS; +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000004 | 107 | | | +------------------+----------+--------------+------------------+ 1 row in set (0.00 sec) mysql> Bye root@DB1:~# |
In our case:
We need to get databases dump, which will be later inserted into slaves before slave will sync up with master.
We will use mysqldump command:
1 2 3 4 |
root@DB1:~# mysqldump --all-databases --master-data -u root -p > dbdump.db Enter password: -- Warning: Skipping the data of table mysql.event. Specify the --events option explicitly. root@DB1:~# |
We will use scp file transfer protocol:
1 2 3 4 5 6 7 8 |
root@DB1:~# scp dbdump.db kalkos@192.168.100.20: The authenticity of host '192.168.100.20 (192.168.100.20)' can't be established. ECDSA key fingerprint is 37:1d:ab:b9:d8:12:cc:44:b6:9a:a4:8b:be:83:35:48. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '192.168.100.20' (ECDSA) to the list of known hosts. kalkos@192.168.100.20's password: dbdump.db 100% 540KB 540.0KB/s 00:00 root@DB1:~# |
Edit /etc/mysql/my.cnf and add the following lines in [mysqld] section:
1 |
server-id=2 |
Note:
1 2 |
bind-address = 0.0.0.0 #skip-networking |
Finally, we restart the service for the configuration to be applied:
1 2 |
root@DB2:~# service mysql restart root@DB2:~# |
We need to login to mysql slave server and set the master location and position.
We gathered from master in the previous steps that location in master is mysql-bin.000004 and positions is 107.
Change this values to the ones you got from your master (otherwise it won’t work).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
root@DB2:~# mysql -u root -p mysql Enter password: Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 38 Server version: 5.5.49-0+deb8u1 (Debian) Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved. 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> CHANGE MASTER TO MASTER_HOST='192.168.154.128', MASTER_USER='repl', MASTER_PASSWORD='qwerty', MASTER_LOG_FILE='mysql-bin.000004', MASTER_LOG_POS=107; Query OK, 0 rows affected (0.02 sec) mysql> Bye root@DB2:~# |
We’re now almost ready to start slave, but first we need to restore the dump of our database.
We use the mysql command line client, which can read standard input (the file location depends on the destination we used with scp command above, for Distributed Operating Systems it’s usually /home/kalkos/dbdump.db):
1 2 3 |
root@DB2:~# mysql -u root -p < /home/kalkos/dbdump.db Enter password: root@DB2:~# |
We can now start slave on DB2:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
root@DB2:~# mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 41 Server version: 5.5.49-0+deb8u1 (Debian) Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved. 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> START SLAVE; Query OK, 0 rows affected (0.00 sec) |
Let’s see if slave works fine (\G set the more human friendly output):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
mysql> show slave status \G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.154.128 Master_User: repl Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000004 Read_Master_Log_Pos: 107 Relay_Log_File: mysqld-relay-bin.000002 Relay_Log_Pos: 253 Relay_Master_Log_File: mysql-bin.000004 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0 Last_Error: Skip_Counter: 0 Exec_Master_Log_Pos: 107 Relay_Log_Space: 410 Until_Condition: None Until_Log_File: Until_Log_Pos: 0 Master_SSL_Allowed: No Master_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: 0 Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 0 Last_IO_Error: Last_SQL_Errno: 0 Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 1 1 row in set (0.00 sec) mysql> |
Finally we can select from testtable in testdb:
1 2 3 4 5 6 7 |
mysql> use testdb; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> select * from testtable; Empty set (0.00 sec) |
First, on slave we’ll check what’s the content of testtable (DB2):
1 2 3 4 5 6 7 |
mysql> use testdb; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> select * from testtable; Empty set (0.00 sec) |
On master, we’ll insert the first value to testtable in testdb (DB1:):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
root@DB1:~# mysql -u root -p testdb Enter password: Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 45 Server version: 5.5.49-0+deb8u1-log (Debian) Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved. 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> select * from testtable; Empty set (0.00 sec) mysql> insert into testtable values ('1'); Query OK, 1 row affected (0.01 sec) mysql> select * from testtable; +------+ | i | +------+ | 1 | +------+ 1 row in set (0.00 sec) mysql> |
Finally we’ll check if the same result was applied to slave server (DB2):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
mysql> use testdb; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> select * from testtable; Empty set (0.00 sec) mysql> select * from testtable; +------+ | i | +------+ | 1 | +------+ 1 row in set (0.00 sec) mysql> |
Which completes the configuration.
Check: