Skip to content

Instantly share code, notes, and snippets.

@ozgurgul
Created July 14, 2020 09:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ozgurgul/7aadf3ae4ee2a69c864f038fffa4b68c to your computer and use it in GitHub Desktop.
Save ozgurgul/7aadf3ae4ee2a69c864f038fffa4b68c to your computer and use it in GitHub Desktop.

ON MASTER

$ yum install mariadb-server
$ systemctl restart mariadb
$ systemctl enable mariadb
$ mysql_secure_installation

On /etc/my.cnf, add

server_id=1
log-basename=master
log-bin
$ systemctl restart mariadb
$ mysql -u root -p
MariaDB [(none)]> STOP SLAVE;
MariaDB [(none)]> GRANT REPLICATION SLAVE ON *.* TO 'slave'@'%' IDENTIFIED BY 'mypassword';
MariaDB [(none)]> FLUSH PRIVILEGES;
MariaDB [(none)]> FLUSH TABLES WITH READ LOCK;
MariaDB [(none)]> SHOW MASTER STATUS;
+--------------------+----------+--------------+------------------+
| File               | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+--------------------+----------+--------------+------------------+
| mariadb-bin.000001 | 460      |              |                  |
+--------------------+----------+--------------+------------------+

Keep session on, don’t logout. Open new terminal and dump the db to file:

$ mysqldump --all-databases --user=root --password --master-data > masterdatabase.sql

Back on the previous terminal:

MariaDB [(none)]> UNLOCK TABLES;

Copy the dump file to slave server

$ scp masterdatabase.sql root@slave.server.com:/root/

ON SLAVE

$ yum install mariadb-server
$ systemctl restart mariadb
$ systemctl enable mariadb
$ mysql_secure_installation

On /etc/my.cnf, put server_id=2. Then import the copy from the master database.

$ mysql -u root -p < masterdatabase.sql
$ systemctl restart mariadb
$ mysql -u root -p
MariaDB [(none)]> STOP SLAVE;
MariaDB [(none)]> 
CHANGE MASTER TO
  MASTER_HOST='master.server.com',
  MASTER_USER='slave',
  MASTER_PASSWORD='mypassword',
  MASTER_PORT=3306,
  MASTER_LOG_FILE='mariadb-bin.000001',
  MASTER_LOG_POS=460,
  MASTER_CONNECT_RETRY=10;

MariaDB [(none)]> SLAVE START;
MariaDB [(none)]> SHOW SLAVE STATUS\G;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment