1.配置MySQL复制基本操作
MySQL的原理:点击打开链接
一.master 1.启动二进制日志。 log-bin = master-bin log-bin-index = master-bin.index
2.选择一个唯一的server.id server.id = (0-2^32)
3.创建具有复制权限的用户 REPLTCATION SLAVE REPLICATION CLIENT
二.slave 1.启用中继日志 relay-log = relay-log relay-log-index = slave-bin.index
2.选择一个唯一的server-id server-id = (0-2^32)
3.连接至主服务器,并开始复制数据。 change master to master_host = '',master_port='',master_log_file=‘’,master_log_pos='',master_user='',master_password='';
start slave;
主服务器的dump线程将数据给从服务器的I/O线程,SQL线程从中继日志读一个数据在MySQL中写。
主服务器的配置。 修改/etc/my.cnf (mysql的主配置文件)
设置二进制日志。注意server-id必须要和从服务器区分开来。
最好再加上一句在配置文件中加上,不过不是很重要。
innodb_file_per_table = 1
建立用户账号:
mysql> grant replication slave on *.* to 'master'@'192.168.%.%' identified by '123';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
查看主服务器的二进制日志内容。
mysql> show master status;
+-------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| master-bin.000001 | 333 | | |
+-------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
mysql> show binlog events in 'master-bin.000001';
+-------------------+-----+-------------+-----------+-------------+------------------------------------------------------------------------------+
| Log_name | Pos | Event_type | Server_id | End_log_pos | Info |
+-------------------+-----+-------------+-----------+-------------+------------------------------------------------------------------------------+
| master-bin.000001 | 4 | Format_desc | 1 | 107 | Server ver: 5.5.12-log, Binlog ver: 4 |
| master-bin.000001 | 107 | Query | 1 | 258 | grant replication slave on *.* to 'master'@'192.168.%.%' identified by '123' |
| master-bin.000001 | 258 | Query | 1 | 333 | flush privileges |
+-------------------+-----+-------------+-----------+-------------+------------------------------------------------------------------------------+
3 rows in set (0.02 sec)
修改/etc/my.cnf
注意修改server-id 号。
连接至主服务器。
mysql> change master to
-> master_host='192.168.217.11',
-> master_user='master',
-> master_password='123',
-> master_log_file='master-bin.000001',
-> master_log_pos=333;
启动slave。
mysql> start slave;
Query OK, 0 rows affected (0.02 sec)
查看slave的状态。
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.217.11
Master_User: master
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-bin.000001
Read_Master_Log_Pos: 333
Relay_Log_File: relay-log.000002
Relay_Log_Pos: 254
Relay_Master_Log_File: master-bin.000001
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: 333
Relay_Log_Space: 404
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: 1
1 row in set (0.00 sec)
mysql>
因为从服务器要求不能执行写操作。
所以在从服务器的/etc/my.cnf 里填了read-only = true;
mysql> show global variables like 'read%';
+----------------------+---------+
| Variable_name | Value |
+----------------------+---------+
| read_buffer_size | 1048576 |
| read_only | ON |
| read_rnd_buffer_size | 4194304 |
+----------------------+---------+
3 rows in set (0.00 sec)
发现成功复制。
另外最好配置主服务器同步二进制日志。
sync-binlog=on 在主服务器上设定。用于事务安全。
MySQL 5.5 semi-sync 半同步复制。
主服务器端:
mysql> install plugin rpl_semi_sync_master soname 'semisync_master.so'
-> ;
Query OK, 0 rows affected (0.04 sec)
mysql> show global variables like '%rpl%';
+------------------------------------+-------+
| Variable_name | Value |
+------------------------------------+-------+
| rpl_recovery_rank | 0 |
| rpl_semi_sync_master_enabled | OFF |#未启用
| rpl_semi_sync_master_timeout | 10000 |#超时时间
| rpl_semi_sync_master_trace_level | 32 |#追踪级别
| rpl_semi_sync_master_wait_no_slave | ON |
+------------------------------------+------
从服务器端:
mysql> install plugin rpl_semi_sync_slave soname 'semisync_slave.so';
Query OK, 0 rows affected (0.08 sec)
主服务器端启动:
mysql> set global rpl_semi_sync_master_enabled = 1;
Query OK, 0 rows affected (0.00 sec)
从服务器端启动:
mysql> set global rpl_semi_sync_slave_enabled = 1;
Query OK, 0 rows affected (0.00 sec)
从服务器从新启动IO_THREAD
mysql> stop slave IO_THREAD;
Query OK, 0 rows affected (0.00 sec)
mysql> start slave IO_THREAD;
Query OK, 0 rows affected (0.00 sec)
mysql> show global status like '%rpl%';
+----------------------------+-------------+
| Variable_name | Value |
+----------------------------+-------------+
| Rpl_semi_sync_slave_status | ON |
| Rpl_status | AUTH_MASTER |
+----------------------------+-------------+
2 rows in set (0.00 sec)
当然也可以只复制某个库或者某张表。
在slave 的 /etc/my.cnf 下
写上
replicate-do-table=wishrp.sku_map #库名.表名
最好在slave上执行。因为在master上执行会造成二进制日志不全的情况