This commit is contained in:
parent
1ea25ff238
commit
56ad6595a3
79
README.md
79
README.md
@ -1,29 +1,30 @@
|
|||||||
[![Build Status](https://drone.asperti.com/api/badges/paspo/docker-mariadb-backup-slave/status.svg)](https://drone.asperti.com/paspo/docker-mariadb-backup-slave)
|
|
||||||
|
|
||||||
|
|
||||||
# docker-mariadb-backup-slave
|
# docker-mariadb-backup-slave
|
||||||
|
|
||||||
Setup a mariadb slave server with automysqlbackup
|
[![Build Status](https://drone.asperti.com/api/badges/paspo/docker-mariadb-backup-slave/status.svg)](https://drone.asperti.com/paspo/docker-mariadb-backup-slave)
|
||||||
|
|
||||||
|
Setup a mariadb slave server with automysqlbackup
|
||||||
|
|
||||||
## Parameters
|
## Parameters
|
||||||
|
|
||||||
### SERVER_ID
|
### SERVER_ID
|
||||||
|
|
||||||
This is used to customize slave server ID. The default value is 33. You should use an unique ID for each server.
|
This is used to customize slave server ID. The default value is 33. You should use an unique ID for each server.
|
||||||
|
|
||||||
### READONLY
|
### READONLY
|
||||||
|
|
||||||
When set to **1**, the slave server is set as readonly. This is the default. If you set this variable to something different, the database will be read/write.
|
When set to **1**, the slave server is set as readonly. This is the default. If you set this variable to something different, the database will be read/write.
|
||||||
|
|
||||||
### REPLICATE_DO_DB
|
### REPLICATE_DO_DB
|
||||||
This is used to specify a single database to backup. It is generally advised to set this variable to the database name you want to backup.
|
|
||||||
|
|
||||||
|
This is used to specify a single database to backup. It is generally advised to set this variable to the database name you want to backup.
|
||||||
|
|
||||||
## How to setup a mysql slave for backup
|
## How to setup a mysql slave for backup
|
||||||
|
|
||||||
### Configure the master node
|
### Configure the master node
|
||||||
|
|
||||||
Examine all mariadb config files, they're usually located in **/etc/mysql/**. You must have these instructions:
|
Examine all mariadb config files, they're usually located in **/etc/mysql/**. You must have these instructions:
|
||||||
|
|
||||||
```
|
```ini
|
||||||
[mysqld]
|
[mysqld]
|
||||||
server_id = 1
|
server_id = 1
|
||||||
log_bin
|
log_bin
|
||||||
@ -41,7 +42,7 @@ After these modifications, restart mariadb.
|
|||||||
|
|
||||||
Execute this statement on the master node:
|
Execute this statement on the master node:
|
||||||
|
|
||||||
```
|
```sql
|
||||||
grant replication slave on *.* to repluser@backupslave.host identified by 'aRandomPassword'; flush privileges;
|
grant replication slave on *.* to repluser@backupslave.host identified by 'aRandomPassword'; flush privileges;
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -51,7 +52,7 @@ Replace **repluser**, **backupslave.host**, **aRandomPassword** with something m
|
|||||||
|
|
||||||
We use docker-compose here, but you can do the same with plain docker. Here's a sample **docker-compose.yaml** file:
|
We use docker-compose here, but you can do the same with plain docker. Here's a sample **docker-compose.yaml** file:
|
||||||
|
|
||||||
```
|
```yaml
|
||||||
version: "3"
|
version: "3"
|
||||||
services:
|
services:
|
||||||
|
|
||||||
@ -69,12 +70,14 @@ services:
|
|||||||
```
|
```
|
||||||
|
|
||||||
Start the container:
|
Start the container:
|
||||||
```
|
|
||||||
|
```bash
|
||||||
docker-compose up -d
|
docker-compose up -d
|
||||||
```
|
```
|
||||||
|
|
||||||
Create the database:
|
Create the database:
|
||||||
```
|
|
||||||
|
```bash
|
||||||
docker exec my-container sh -c 'echo "create database aWordpressDatabase;" | exec mysql -uroot -p"$MYSQL_ROOT_PASSWORD"'
|
docker exec my-container sh -c 'echo "create database aWordpressDatabase;" | exec mysql -uroot -p"$MYSQL_ROOT_PASSWORD"'
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -82,7 +85,8 @@ docker exec my-container sh -c 'echo "create database aWordpressDatabase;" | exe
|
|||||||
|
|
||||||
To obtain a consistant slave, you first have to freeze the master, and then transfer the data.
|
To obtain a consistant slave, you first have to freeze the master, and then transfer the data.
|
||||||
Execute this on the master:
|
Execute this on the master:
|
||||||
```
|
|
||||||
|
```sql
|
||||||
FLUSH TABLES WITH READ LOCK;
|
FLUSH TABLES WITH READ LOCK;
|
||||||
show master status;
|
show master status;
|
||||||
```
|
```
|
||||||
@ -92,6 +96,7 @@ show master status;
|
|||||||
Take note of file and position, we'll need these informations later.
|
Take note of file and position, we'll need these informations later.
|
||||||
|
|
||||||
### Import initial data
|
### Import initial data
|
||||||
|
|
||||||
We have two ways to copy the data from the master to the slave: we'll call these "live" and "classic".
|
We have two ways to copy the data from the master to the slave: we'll call these "live" and "classic".
|
||||||
The classic way is the usual mysqldump + file transfer + import.
|
The classic way is the usual mysqldump + file transfer + import.
|
||||||
The live way is dump + import at the same time, without an intermediate file.
|
The live way is dump + import at the same time, without an intermediate file.
|
||||||
@ -99,47 +104,62 @@ The live way is preferred when you have small databases (less than 5Gb) and a go
|
|||||||
The classic way is preferred when you have a big database because it locks the database for less time.
|
The classic way is preferred when you have a big database because it locks the database for less time.
|
||||||
|
|
||||||
### Import initial data (live version)
|
### Import initial data (live version)
|
||||||
|
|
||||||
Now, we need to import the backup from the master, so as we started the slave as readwrite, we can now do this on the slave:
|
Now, we need to import the backup from the master, so as we started the slave as readwrite, we can now do this on the slave:
|
||||||
|
|
||||||
```
|
```bash
|
||||||
docker exec my-container sh -c 'mysqldump -C --lock-tables=false --quick -u repluser -h master.host --password="aRandomPassword" aWordpressDatabase | mysql -u root -p"$MYSQL_ROOT_PASSWORD" aWordpressDatabase'
|
docker exec my-container sh -c 'mysqldump -C --lock-tables=false --quick -u repluser -h master.host --password="aRandomPassword" aWordpressDatabase | mysql -u root -p"$MYSQL_ROOT_PASSWORD" aWordpressDatabase'
|
||||||
```
|
```
|
||||||
|
|
||||||
Replace **my-container**, **repluser**, **master.host**, **aRandomPassword**, **aWordpressDatabase** with the correct values.
|
Replace **my-container**, **repluser**, **master.host**, **aRandomPassword**, **aWordpressDatabase** with the correct values.
|
||||||
|
|
||||||
Now that the slave has got some data, we can release the lock in the master:
|
Now that the slave has got some data, we can release the lock in the master:
|
||||||
```
|
|
||||||
|
```bash
|
||||||
UNLOCK TABLES;
|
UNLOCK TABLES;
|
||||||
```
|
```
|
||||||
|
|
||||||
### Import initial data (classic version)
|
### Import initial data (classic version)
|
||||||
|
|
||||||
In the master we run mysqldump in a shell:
|
In the master we run mysqldump in a shell:
|
||||||
```
|
|
||||||
|
```bash
|
||||||
mysqldump -u root -p aWordpressDatabase | gzip > thedump.sql.gz
|
mysqldump -u root -p aWordpressDatabase | gzip > thedump.sql.gz
|
||||||
```
|
```
|
||||||
|
|
||||||
When the export is finished, we can let the master resume his work, by issuing this command in the previous mysql session:
|
When the export is finished, we can let the master resume his work, by issuing this command in the previous mysql session:
|
||||||
```
|
|
||||||
|
```bash
|
||||||
UNLOCK TABLES;
|
UNLOCK TABLES;
|
||||||
```
|
```
|
||||||
|
|
||||||
We can proceed to transfer the dump file to the slave; use the method you prefer.
|
We can proceed to transfer the dump file to the slave; use the method you prefer.
|
||||||
On the slave, copy the dump file in the running container, like this:
|
On the slave, copy the dump file in the running container, like this:
|
||||||
```
|
|
||||||
|
```bash
|
||||||
docker cp thedump.sql.gz my-container:/thedump.sql.gz
|
docker cp thedump.sql.gz my-container:/thedump.sql.gz
|
||||||
```
|
```
|
||||||
|
|
||||||
Then we import it:
|
Then we import it:
|
||||||
```
|
|
||||||
|
```bash
|
||||||
docker exec my-container sh -c 'zcat /thedump.sql.gz | mysql -u root -p"$MYSQL_ROOT_PASSWORD" aWordpressDatabase'
|
docker exec my-container sh -c 'zcat /thedump.sql.gz | mysql -u root -p"$MYSQL_ROOT_PASSWORD" aWordpressDatabase'
|
||||||
docker exec my-container sh -c 'rm /thedump.sql.gz'
|
docker exec my-container sh -c 'rm /thedump.sql.gz'
|
||||||
```
|
```
|
||||||
|
|
||||||
Replace **my-container**, **aWordpressDatabase** with the correct values.
|
Replace **my-container**, **aWordpressDatabase** with the correct values.
|
||||||
|
|
||||||
### Start the slave
|
### Start the slave
|
||||||
|
|
||||||
Open a mysql console in the slave:
|
Open a mysql console in the slave:
|
||||||
```
|
|
||||||
|
```bash
|
||||||
docker exec -ti my-container sh -c 'mysql -u root -p"$MYSQL_ROOT_PASSWORD" aWordpressDatabase'
|
docker exec -ti my-container sh -c 'mysql -u root -p"$MYSQL_ROOT_PASSWORD" aWordpressDatabase'
|
||||||
```
|
```
|
||||||
|
|
||||||
And configure the replication:
|
And configure the replication:
|
||||||
```
|
|
||||||
|
```sql
|
||||||
STOP SLAVE;
|
STOP SLAVE;
|
||||||
RESET SLAVE;
|
RESET SLAVE;
|
||||||
CHANGE MASTER TO
|
CHANGE MASTER TO
|
||||||
@ -153,12 +173,16 @@ CHANGE MASTER TO
|
|||||||
MASTER_CONNECT_RETRY=10;
|
MASTER_CONNECT_RETRY=10;
|
||||||
START SLAVE;
|
START SLAVE;
|
||||||
```
|
```
|
||||||
|
|
||||||
### Check replication status
|
### Check replication status
|
||||||
```
|
|
||||||
|
```bash
|
||||||
docker exec -ti my-container sh -c 'mysql -u root -p"$MYSQL_ROOT_PASSWORD" aWordpressDatabase -e "SHOW SLAVE STATUS\G;"'
|
docker exec -ti my-container sh -c 'mysql -u root -p"$MYSQL_ROOT_PASSWORD" aWordpressDatabase -e "SHOW SLAVE STATUS\G;"'
|
||||||
```
|
```
|
||||||
|
|
||||||
Example output:
|
Example output:
|
||||||
```
|
|
||||||
|
```sql
|
||||||
*************************** 1. row ***************************
|
*************************** 1. row ***************************
|
||||||
Slave_IO_State: Waiting for master to send event
|
Slave_IO_State: Waiting for master to send event
|
||||||
Master_Host: master.host
|
Master_Host: master.host
|
||||||
@ -209,18 +233,23 @@ Master_SSL_Verify_Server_Cert: No
|
|||||||
Parallel_Mode: conservative
|
Parallel_Mode: conservative
|
||||||
1 row in set (0.00 sec)
|
1 row in set (0.00 sec)
|
||||||
```
|
```
|
||||||
|
|
||||||
In a normal condition, **Slave_IO_Running** and **Slave_SQL_Running** should be **Yes**: this means that the communication between master and slave is ok and no sync error has occourred. **Seconds_Behind_Master** is usually 0, except wen the communication has just been established and the slave has to catch up with the master. This situation usually resolves in some time.
|
In a normal condition, **Slave_IO_Running** and **Slave_SQL_Running** should be **Yes**: this means that the communication between master and slave is ok and no sync error has occourred. **Seconds_Behind_Master** is usually 0, except wen the communication has just been established and the slave has to catch up with the master. This situation usually resolves in some time.
|
||||||
|
|
||||||
### Make the slave read-only
|
### Make the slave read-only
|
||||||
|
|
||||||
For a safety measure, we're enabling read-only mode for the slave: we don't have to run any query on this host, we're just using it for scheduled backups.
|
For a safety measure, we're enabling read-only mode for the slave: we don't have to run any query on this host, we're just using it for scheduled backups.
|
||||||
Reopen **docker-compose.yaml**, change **READONLY=0** to **READONLY=1** and restart the container:
|
Reopen **docker-compose.yaml**, change **READONLY=0** to **READONLY=1** and restart the container:
|
||||||
```
|
|
||||||
|
```bash
|
||||||
docker-compose up -d
|
docker-compose up -d
|
||||||
```
|
```
|
||||||
|
|
||||||
## How to use different mariadb version
|
## How to use different mariadb version
|
||||||
The default is mariadb 10.4.
|
|
||||||
If you want, you can use mariadb 10.3, 10.2 or 10.1 by changing this line in **docker-compose.yaml**:
|
The default is mariadb 10.5.
|
||||||
```
|
If you want, you can use mariadb 10.4, 10.3, or 10.2 by changing this line in **docker-compose.yaml**:
|
||||||
|
|
||||||
|
```yaml
|
||||||
image: docker.asperti.com/paspo/mariadb-backup-slave:maria-10.1
|
image: docker.asperti.com/paspo/mariadb-backup-slave:maria-10.1
|
||||||
```
|
```
|
Loading…
Reference in New Issue
Block a user