[MySQL] 复制(2)- 创建主备复制(主备库都为空)
2013-11-27 15:10
239 查看
本文适用于新安装的主库和备库,假定主备库为空,如果你是从已存在的主库复制,请转到《[MySQL] 复制(3)- 创建主备复制(从另一个服务器开始复制)》
和主库类似,备库也要指定一个唯一的server id,还有备库的二进制日志最好和主库的命名一致,方便以后切换。
此外,relay_log用于配置中继日志,log_slave_updates=1表示允许备库将其重放的事件也记录在其自身的二进制日志中,read_only=1表示备库为只读。
在主库上可以看到由备库I/O线程向主库发起的连接:
主库的配置
主库需要打开二进制日志,并制定一个唯一的server id,my.cnf文件中增加或修改如下内容:server_id=60 log-bin = /data/mysql/log/mysql-bin
备库的配置
备库my.cnf的配置如下:server_id=61 read_only=1 log_bin = /data/mysql/log/mysql-bin log_slave_updates = 1 relay_log = /data/mysql/log/mysqld-relay-bin
和主库类似,备库也要指定一个唯一的server id,还有备库的二进制日志最好和主库的命名一致,方便以后切换。
此外,relay_log用于配置中继日志,log_slave_updates=1表示允许备库将其重放的事件也记录在其自身的二进制日志中,read_only=1表示备库为只读。
创建复制账号
我们需要在主库创建一个用户,备库的I/O线程将以该用户连接到主库读取二进制日志。GRANT REPLICATION SLAVE ON *.* TO repluser@'192.168.1.%' IDENTIFIED BY 'replpwd';最好在备库里也创建这样一个用户,方便以后切换。
启动复制
首先,我们要获得主库当前的二进制日志和其偏移量:show master status; +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000003 | 245 | | | +------------------+----------+--------------+------------------+然后,在备库执行如下语句连接主库获取二进制日志:
change master to master_host='192.168.1.60', master_user='repluser', master_password='replpwd', master_log_file='mysql-bin.000003', master_log_pos=245;接着,在备库执行如下语句启动复制:
start slave下面是备库的状态:
show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.1.60 Master_User: repluser Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000003 Read_Master_Log_Pos: 647 Relay_Log_File: mysqld-relay-bin.000002 Relay_Log_Pos: 655 Relay_Master_Log_File: mysql-bin.000003 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: 647 Relay_Log_Space: 812 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: 60我们通过Slave_IO_Running和Slave_SQL_Running来判断备库是否和主库在同步。
在主库上可以看到由备库I/O线程向主库发起的连接:
show processlist\G Id: 9 User: repluser Host: 192.168.1.61:36722 db: NULL Command: Binlog Dump Time: 379 State: Master has sent all binlog to slave; waiting for binlog to be updated Info: NULL Rows_sent: 0 Rows_examined: 0 Rows_read: 2在备库上可以看到两个线程,分别是I/O线程和SQL线程:
show processlist\G Id: 10 User: system user Host: db: NULL Command: Connect Time: 615 State: Waiting for master to send event Info: NULL Rows_sent: 0 Rows_examined: 0 Rows_read: 1 *************************** 3. row *************************** Id: 11 User: system user Host: db: NULL Command: Connect Time: 907 State: Slave has read all relay log; waiting for the slave I/O thread to update it Info: NULL Rows_sent: 0 Rows_examined: 0 Rows_read: 1 3 rows in set (0.00 sec)
相关文章推荐
- [MySQL] 复制(3)- 创建主备复制(从另一个服务器开始复制)
- mysql ---复制表结构---创建新表
- mysql主备复制I/O线程不能同步问题
- MySQL入门(6)- 创建表、表的复制、增/删/改 表结构
- Windows环境下创建MySQL主从复制
- MYSQL主备复制结构搭建与切换
- MySQL建立双向主备复制服务器配置方法
- MySQL表结构复制、表数据迁移以及临时表、视图创建
- mysql 复制表结构或创建新表
- mysql 主备复制延迟
- mysql一些复制表、增删改索引、建存储过程、创建函数、创建触发器的一些命令
- MySQL建立双向主备复制server配置方法
- mysql如何新创建一个mysql只复制结构,或复制自己想要的内容
- MySQL主备复制数据不一致的情况
- MySQL入门之创建、更新、修改、复制、查看表
- 【MySQL】主备复制
- MySQL表结构复制、表数据迁移以及临时表、视图创建
- mysql主备复制-预习
- mysql复制创建表
- MySQL复制旧表结构创建新表