Its a process of creating duplicate versions of a the DB. Replication process is not only copies a database, but also synchronizes changes from master to one of the slaves. But this is does not means that slave databases are identical copy of the master, because replication can be configured that only a schema of tables or columns or rows will be replicated, i.e. a partial replication. The replication ensures that those specific configured objects are kept in sync between the different databases.
### Mariadb Replication Concepts ###
**Backups** : Replication can be used for DB backups. For example, you have master -> slave replication. If master is lost (hdd fails, for example) you can restore your db from master.
**Scaling** : You can use master -> slave replication for scaling solution. For example, if you have a few big and have SQL query, using replcation you can separate this queries for each replcations nodes. Write SQL should be performed only on master, for read-only queries slave server can be used.
**Spreading solution** : You can use replication for distribution. For example, you can distribute different sales data to different databases.
**Failover solution** : For example you have, master -> slave(1) -> slave(2) -> slave(3) replication. You can write script for master monitoring , if master fails, script can quickly change slave(1) new for master master -> slave(1) -> slave(2) and your application will continue working whit out downtime
### Simple diagrammatic demonstration of replication ###
Before you start good know what is **binary log** and Ibdata1. The binary log contains a record about all changes in the db, data and structure, as well as how long each statement took to execute. Bin log consists set log files and an index. Its means that main SQL statements such as CREATE, ALTER, INSERT, UPDATE and DELETE will be putted to this log, statements, such as SELECT will not be logged. These info can be logged to general query.log file. In simple **Ibdata1** is a file which contains all tables and all info about db.
You'll need to edit the my.cnf file on the Master server to enable binary logging and set the server's id. I will use vi text editor, but use can use any suitable for your such as nano, joe etc.
SOME_ROOT_PASSWORD - password for root user that you have setup
test_repl - name of the data base which will be replicated;
You need to recover mysql dump (full-dump.sql) at slave server. Its needed for replication.
### Slave server configuration ###
All this commands you need to perform at slave server
Lets assume that we have fresh/updated CentOS 7.x server with latest mariaDB server and you can login as root to maria DB server (this was descripbed in first part of the article)
Login to Maria DB console and create DB
mysql -u root -pSOME_ROOT_PASSWORD;
mysql> create database test_repl;
mysql> exit;
Recover data from master at slave server
mysql -u root -pSOME_ROOT_PASSWORD test_repl <full-dump.sql
Where:
full-dump.sql - its DB Dump that you have create at test server.
You can see the data is replicated to slave server. Its mean that replication is working. Hope you enjoyed the article. Let us know if you have any questions.