标签:
MySQL 主从复制搭建
1. 搭建
1.1 Master
Master配置(my.cnf)
[mysqld] log-bin=/usr/local/mysql-5.6.16/log/mysql-bin server_id=1
启动Master
./bin/mysqld_safe --defaults-file=/usr/local/mysql-5.6.16/my.cnf --user=mysql &
创建用于复制的用户
mysql > grant replication slave on *.* to ‘rep1‘@‘192.168.80.102‘ identified by ‘rep1‘;
备份数据并还原到从服务器
-- 设置读锁,数据无法更新
mysql > flush tables with read lock;
-- 查看开始复制的位置
mysql > show master status; +------------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+-------------------+ | mysql-bin.000001 | 120 | | | | +------------------+----------+--------------+------------------+-------------------+
-- 传输数据
tar -czf - data/ | ssh 192.168.80.102 "tar -xzf - -C /usr/local/mysql-5.6.16"
-- 解除锁定
mysql > unlock tables;
1.2 Slave
Slave配置(my.cnf)
[mysqld] server_id=2 # Master和Slave之间唯一 skip-slave-start # 启动时不启动slave的复制进程
启动Slave
./bin/mysqld_safe --defaults-file=/usr/local/mysql-5.6.16/my.cnf --user=mysql &
配置复制参数
mysql > change master to -> master_host=‘192.168.80.101‘, -> master_port=3306, -> master_user=‘rep1‘, -> master_password=‘rep1‘, -> master_log_file=‘mysql-bin.000001‘, -> master_log_pos=120;
启动Slave的IO和SQL线程
mysql > start slave;
检查
mysql > show processlist \G
应出现以下信息:
State: Waiting for master to send event
State: Slave has read all relay log; waiting for the slave I/O thread to update it
或
mysql > show slave status \G
应出现以下信息:
Slave_IO_State: Waiting for master to send event
Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
2. 测试
2.1 Master
mysql > use test; mysql > show tables; mysql > create table t1 as select * from mysql.user;
2.2 Slave
mysql > show tables; mysql > select * from t1;
3. 附加
3.1 主要复制启动选项
(1) log-slave-updates
该参数需要和log-bin一起使用,配置从服务器上的更新操作是否写binlog。默认关闭。如果搭建一个链式复制,从服务器同时也是其他服务器的主服务器,则需要打开这个选项,以让它的从服务器获得它的binlog进行同步。
(2) read-only
on: 从服务器只接受超级用户的更新操作
off: 从服务器接受普通用户的更新操作
标签:
原文地址:http://www.cnblogs.com/tjm-1990/p/4295845.html