简单的主从复制
配置文件说明
master:
cat /etc/my.cnf
[client]
port = 3306
socket = /data/soft/mysql/mysql.sock
[mysqld]
user = mysql
port = 3306
socket = /data/soft/mysql/mysql.sock
basedir = /data/soft/mysql
datadir = /data/soft/mysql/data
log-bin = /data/soft/mysql/mysql-bin
server-id = 106 # server id 不能相同 可以选择使用主机ip最后三位
skip_name_resolve = 0 # 跳过域名解析参数
[mysqld_safe]
log-error=/data/soft/mysql/mysql.log
pid-file=/data/soft/mysql/mysqld.pid
slave:
cat /etc/my.cnf
[client]
port = 3306
socket = /data/soft/mysql/mysql.sock
[mysqld]
user = mysql
port = 3306
socket = /data/soft/mysql/mysql.sock
basedir = /data/soft/mysql
datadir = /data/soft/mysql/data
log-bin = /data/soft/mysql/mysql-bin
server-id = 107 # server id 不能相同 可以选择使用主机ip最后三位
skip_name_resolve = 0 # 跳过域名解析参数
read_only = 1 # 从库只读 (非root用户 )
[mysqld_safe]
log-error=/data/soft/mysql/mysql.log
pid-file=/data/soft/mysql/mysqld.pid
创建主从复制用户
master
mysql > grant replication slave on *.* to repl@'%' identified by '123456';
初始化从库数据
master:
备份主库当前数据
mysqldump -uroot -p123456 -A -B -F --master-data=2 >/tmp/full.sql
slave:
导入数据
mysql > set sql_log_bin=0;
mysql > source /tmp/full.sql;
开启从库复制
master:
查看当前使用的文件以及POS号
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000012 | 120 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
slave:
配置主从复制
mysql > CHANGE MASTER TO
MASTER_HOST='10.0.0.52',
MASTER_USER='repl',
MASTER_PASSWORD='123456',
MASTER_PORT=3306,
MASTER_LOG_FILE='mysql-bin.000012',
MASTER_LOG_POS=120;
启动主从复制
mysql > start slave;
测试主从同步
查看slave状态:主要查看Slave_IO_Running 与 Slave_SQL_Running 是否都为Yes
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.0.0.52
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000010
Read_Master_Log_Pos: 842
Relay_Log_File: 3307-relay-bin.000018
Relay_Log_Pos: 283
Relay_Master_Log_File: mysql-bin.000010
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 842
Relay_Log_Space: 455
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 6
Master_UUID: 4f344556-e0ab-11e7-9138-000c29d60ab3
Master_Info_File: /data/3307/data/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
1 row in set (0.00 sec)
主库创建新的库
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.00 sec)
mysql > create database clsn;
查看从库
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| clsn |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.00 sec)
到此mysql主从复制就搭建完成
半同步复制配置
加载插件
master
mysql > INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_master.so';
slave
mysql > INSTALL PLUGIN rpl_semi_sync_slave SONAME 'semisync_slave.so';
检查插件是否加载成功
mysql > show plugins;
启动半同步复制
master
mysql > SET GLOBAL rpl_semi_sync_master_enabled = 1;
slave
SET GLOBAL rpl_semi_sync_slave_enabled = 1;
查看半同步是否在运行
master
mysql > show status like 'Rpl_semi_sync_master_status';
slave
mysql < show status like 'Rpl_semi_sync_slave_status';
重启从库上的IO线程
slave
mysql > STOP SLAVE IO_THREAD;
mysql > START SLAVE IO_THREAD;
测试半同步复制
slave
查看延迟时间:rpl_semi_sync_slave_trace_level默认好像是32,可以通过CHANGE MASTER TO MASTER_DELAY = 30;设置
mysql> show variables like '%rpl_semi_sync%';
+---------------------------------+-------+
| Variable_name | Value |
+---------------------------------+-------+
| rpl_semi_sync_slave_enabled | ON |
| rpl_semi_sync_slave_trace_level | 32 |
+---------------------------------+-------+
2 rows in set (0.00 sec)
停止从库复制
stop slave;
master
创建新的库查,如果创建库的等待时间为设置的时间就证明是成功的
create database clsn; # 此处创建应该需要等待32秒才返回成功
标签:主从复制,slave,soft,简单,Master,mysql,master,data
From: https://www.cnblogs.com/bkhb/p/17195550.html