real-time MySQL mirror on the same server but stored on another disk partition MySQL replication (master ā slave)
Architecture
Server
ā
āāā MySQL Instance 1 (MASTER)
ā port: 3306
ā datadir: /var/lib/mysql
ā
āāā MySQL Instance 2 (SLAVE)
port: 3307
datadir: /mnt/mysql_mirror
Your second partition might be something like:
/dev/sdb1 ā /mnt/mysql_mirror
Create the Mirror Data Directory
sudo mkdir -p /mnt/mysql_mirror
sudo chown -R mysql:mysql /mnt/mysql_mirror
Initialize the database:
mysqld --initialize-insecure --datadir=/mnt/mysql_mirror
Create Second MySQL Configuration
Create a new config file:
/etc/mysql/my-slave.cnf
Example:
[mysqld]
server-id=2
port=3307
datadir=/mnt/mysql_mirror
socket=/var/run/mysqld/mysqld2.sock
relay-log=relay-bin
log_bin=mysql-bin
read_only=1
Start the Second MySQL Instance
mysqld_safe --defaults-file=/etc/mysql/my-slave.cnf &
Check:
mysql -u root -S /var/run/mysqld/mysqld2.sock
Configure Master (Main MySQL)
Edit main config (my.cnf):
[mysqld]
server-id=1
log_bin=mysql-bin
Restart MySQL.
Create replication user:
CREATE USER 'replica'@'127.0.0.1' IDENTIFIED BY 'password';
GRANT REPLICATION SLAVE ON . TO 'replica'@'127.0.0.1';
FLUSH PRIVILEGES;
Get Master Log Position
On the master:
SHOW MASTER STATUS;
Example:
File: mysql-bin.000001
Position: 120
Configure Slave
Connect to the second instance:
mysql -u root -S /var/run/mysqld/mysqld2.sock
Run:
CHANGE MASTER TO
MASTER_HOST='127.0.0.1',
MASTER_PORT=3306,
MASTER_USER='replica',
MASTER_PASSWORD='password',
MASTER_LOG_FILE='mysql-bin.000001',
MASTER_LOG_POS=120;
Start replication:
START SLAVE;
Check status:
SHOW SLAVE STATUS\G
You should see:
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Result
Partition 1 (/var/lib/mysql)
ā
MySQL MASTER (3306)
ā replication
MySQL SLAVE (3307)
ā
Partition 2 (/mnt/mysql_mirror)
Advantages
ā
Real-time mirror
ā
Safe (transaction-based)
ā
No file corruption
ā
Can promote mirror if main DB fails