主从复制是mysql数据库热备份的一个常用的做法,这样可以保存数据库任何一方出现问题可及时更换上,下面小编来分享一篇关于从线上环境配置主从的mysql主从复制安装配置实例.
主机172.16.0.21,172.16.0.22,centos6.2,分别使用yum安装mysql
给21上的mysql新建库:
CREATE DATABASE `replytest1` /*!40100 DEFAULT CHARACTER SET utf8 */;
建表:
- CREATE TABLE `replytest1`.`test1` (
- `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
- `test1col` VARCHAR(45) NULL,
- PRIMARY KEY (`id`));
插入数据:
- INSERT INTO `replytest1`.`test1` (`test1col`) VALUES (’1′);
- INSERT INTO `replytest1`.`test1` (`test1col`) VALUES (’2′);
修改主库配置文件:
server-id = 1 //数据库ID号,为1时表示为Master,其中master_id必须为1到232–1之间的一个正整数值;
log-bin=mysql-bin //启用二进制日志;
binlog-do-db=data //需要同步的二进制数据库名;可以写多行
binlog-ignore-db=mysql //不同步的二进制数据库名;这个同步后听说很麻烦
log-slave-updates //把更新的记录写到二进制文件中;
slave-skip-errors
创建负责复制的用户:
- ###GRANT ALL PRIVILEGES ON *.* TO reply@172.16.0.1 IDENTIFIED BY ‘reply’ WITH GRANT OPTION;
- grant replication slave on *.* to test@192.168.2.119 identified by ‘********’
- flush privileges;
锁定主库:FLUSH TABLES WITH READ LOCK;
导出主表数据:解锁并检查当前的master状态及复制到哪里.
UNLOCK TABLES;Show master status;
从库设置,修改配置文件,代码如下:
- #从库设置
- server-id=2
- master-host=172.16.0.1
- master-user=reply
- master-password=reply
- master-connect-retry=600 #如果发现主服务器断线,重新连接的时间差;
- replicate-ignore-db=mysql
- replicate-ignore-db=test
- log-slave-update
- slave-skip-errors
- #从库设置结束
重启mysql,在从服务器上执行stop slave,reset master命令,重置成主数据库,代码如下:
- mysql> stop slave;
- Query OK, 0 rows affected, 1 warning (0.00 sec)
- mysql> reset master;
- change master to master_host=’172.16.0.21′,master_user=’reply’, master_password=’reply’, master_log_file=’mysql-bin.000001′, master_log_pos=106;
- Slave start;
- mysql> show slave statusG
- *************************** 1. row ***************************
- Slave_IO_State: Waiting for master to send event
- Master_Host: 172.16.0.21
- Master_User: reply
- Master_Port: 3306
- Connect_Retry: 600
- Master_Log_File: mysql-bin.000001
- Read_Master_Log_Pos: 106
- Relay_Log_File: mysqld-relay-bin.000002
- Relay_Log_Pos: 251
- Relay_Master_Log_File: mysql-bin.000001
- Slave_IO_Running: Yes --Vevb.com
- Slave_SQL_Running: Yes
- Replicate_Do_DB:
- Replicate_Ignore_DB: mysql,test
- 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: 106
- Relay_Log_Space: 407
- 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:
- 1 row in set (0.00 sec)
新闻热点
疑难解答