一、mysql复制的原理
mysq复制的工作机制其实就是三个进程彼此间的工作,但前提是要开启主服务器的二进制日志,因为主服务器的修改操作都被记录在了二进制日志中。
- #fdisk /dev/sda
- #partprobe /dev/sda
- #pvcreate /dev/sda5
- #vgcreate myvg /dev/sda5
- #lvcreate -n mylv -L 10G myvg
- #mke2fs -j /dev/myvg/mylv
- #mkdir /mydata //创建共享目录
- #vim /etc/fstab //开机自动挂载
- /dev/myvg/mydata /mydata ext3 defaults 0 0
- #groupadd -g 306 -r mysql
- #useradd -g mysql -r -u 306 mysql
- #chown mysql:mysql /mydata //修改共享目录的属主属组为mysql
- #tar xf mysql-5.5.20-linux2.6-i686 -C /usr/local
- # cd /usr/local #ln -sv mysql-5.5.20-linux2.6-i686 mysql //创建连接
- #chown -R mysql:mysql .
- #cp support-files/my-large.cnf /etc/my.cnf //配置文件
- #vim /etc/my.cnf
- 在[mysqld]处 添加datadir=/mydata //添加数据存放位置
- #scripts/mysql_install_db --user=mysql --datadir=/mydata //初始化Mysql
- #chown -R root .
- #cp support-files/mysql.server /etc/rc.d/init.d/mysqld //提供mysql脚本
- #servie mysqld start
- #/usr/local/mysql/bin/mysql //启动并测试mysql是否可用
- #service mysqld stop //必须关闭,否则在节点2上无法启动
- #scp /etc/my.cnf mynode2:/etc //把mysql的配置文件复制到node2上
- #scp /etc/rc.d/init.d/mysqld mynode2:/etc/rc.d/init.d/mysqld //复制服务脚本
- #vim /etc/my.cnf
- binlog_format=mixed //启用二进制日志
- server-id = 1 //设置server-id
- 创建具有复制权限的用户
- mysql>grant replication slave,replication client on *.* to repluser@'172.16.26.%' identified by'1234';
- mysql>flush privileges;
- mysql>show master status
- #vim /etc/my.cnf
- #log-bin=mysql-bin //禁用二进制日志
- relay-log=mysql-relay //启用中继日志
- server-id =12 //设置server id
- #service mysqld restart //重启mysql使配置生效
- mysql>show global variables like "%log%";
- mysql>show global variables like "%server_id%";
- #service mysqld restart
- mysql>change master to
- MASTER_HOST = '172.16.200.1' //主服务的ip地址
- MASTER_USER = 'repluser' //主服务器的建立的用户
- MASTER_PASSWORD = '123' //主服务器的用户密码
- MASTER_LOG_FILE = 'mysql-bin.000001' //主服务器的日志文件
- MASTER_LOG_POS = 352 //日志文件的位置
- MASTER_PORT = 3306 //主服务器所在的端口
- mysql>start slave //启动slave的复制功能
- mysql>create databases testdb;
- mysql>create table tb1;
- mysql>show global variables like 'read_only' //查看只读状态
- mysql>set global read_only=1; //设置为只能只读
- mysql>flush tables with read lock; //施加锁
- #lvcreate -L 50M -s -p r -n mysqldata-snap /dev/myvg/mylv 创建快照卷
- mysql>show master status //记录二进制日志文件的位置
- mysql>unlock tables //释放锁
- mount /dev/myvg/mysqldata-snap /mnt 挂载快照卷
- cd /mnt
- find . | cpio -o -H newc --quiet | gzip > /root/alldatabase.gz //打包压缩
- #umount /mnt //卸载
- #lvremove mysqldata-snap //删除快照卷
- #scp alldatabse.gz node2:/mydata
- #cd /mydata
- #gzip -d alldatabases.gz
- #cpio -id < alldatabases
- # On Master
- mysql> INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_master.so';
- mysql> SET GLOBAL rpl_semi_sync_master_enabled = 1;
- mysql> SET GLOBAL rpl_semi_sync_master_timeout = 1000;
- # On Slave
- mysql> INSTALL PLUGIN rpl_semi_sync_slave SONAME 'semisync_slave.so';
- mysql> SET GLOBAL rpl_semi_sync_slave_enabled = 1;
- mysql> STOP SLAVE IO_THREAD; START SLAVE IO_THREAD;
- # On Master
- [mysqld]
- rpl_semi_sync_master_enabled=1
- rpl_semi_sync_master_timeout=1000 # 1 second
- # On Slave
- [mysqld]
- rpl_semi_sync_slave_enabled=1
- mysql> SHOW GLOBAL STATUS LIKE 'rpl_semi%';
- mysql> SHOW GLOBAL STATUS LIKE 'rpl_semi%';