# Creating A Database Replication

1. Install master database and update mysql config. kindly use different unique numbers for server-id.

```
log-bin=mysql-bin
server-id=145
binlog_format     = row
binlog_row_image  = full
expire_logs_days  = 10
innodb_flush_log_at_trx_commit=1
sync_binlog=1
```

2. Create a New User for Replication Services

```
create user 'replica_user'@'%' identified by 'password';
GRANT REPLICATION SLAVE ON . TO 'replica_user'@'%';
FLUSH PRIVILEGES;
```

3. Dump database to file 

```
nohup mysqldump  --single-transaction -u root --password='password' --all-databases --master-data > /root/all-databases.sql &
```

4. Transfer data to replication server

```
scp -i priv_key -rp -P1533 /root/all-databases.sql root@138.201.127.133:/home/backup
```

5. Install mysql on replication server and update mysql config. kindly use different unique numbers for server-id.

```
log-bin=mysql-bin
server-id=145
binlog_format     = row
binlog_row_image  = full
expire_logs_days  = 10
innodb_flush_log_at_trx_commit=1
sync_binlog=1
```

6. Upload mysql dump to replication database 

```
nohup mysql --user root --password='password'   < /home/backup/all-databases.sql &
```

7. Start replication. Cat first 30 lines of mysql dump to get **MASTER_LOG_FILE** and **MASTER_LOG_POS** 
Example:

![fistlines_mysql_dump](Infrastructure/Screenshot_2023-01-02_at_16.22.35.png)

```
stop slave;

CHANGE MASTER TO MASTER_HOST = '<master_host_ip>', MASTER_PORT=1632, MASTER_USER = 'replica_user', MASTER_PASSWORD = "password", MASTER_LOG_FILE = 'mysql-bin.000255', MASTER_LOG_POS = 445911727;

start slave;
```

