linux下mysql主从复制,实现数据库同步
运行环境:
查看linux版本命令:lsb_release -a
主服务器:centos release 6.5 mysql 5.6.10-log IP:172.17.200.25
从服务器:centos release 6.5 mysql 5.6.10-log IP:172.17.200.26
主服务器dashi数据库
mysql默认配置文件,如不特殊指定默认为/etc/my.cnf
mysql配置文件查找顺序:/etc/my.cnf /etc/mysql/my.cnf ~/.my.cnf
我是默认设置 /etc/my.cnf
进入主服务 msyql -uroot -p
一:主服务
1.1、创建一个复制用户dashi,具有replication slave 权限。
mysql>grant replication slave on *.* to 'dashi'@'172.17.200.26' identified by 'dashi';
mysql>flush privileges;
1.2、编辑my.cnf文件
vim /etc/my.cnf
增加 server-id=107
log-bin=bin.log 文件路径自己定(推荐绝对路径)
1.3 重启mysql
service mysqld restart
1.4、设置读锁
mysql>flush tables with read lock;
1.5、得到binlog日志文件名和偏移量(此处记住File名称和Position值,后面slave服务器配置时需要用到)
mysql> show master status;
+------------+------------+--------------+--------------------------------------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------+------------+--------------+--------------------------------------------------+-------------------+
| bin.000031 | 1011406487 | | information_schema,mysql,performance_schema,test | |
+------------+------------+--------------+--------------------------------------------------+-------------------+
1 row in set (0.00 sec)
1.6、备份要同步的数据库
mysqldump -uroot -p test>test.sql
1.7解锁
mysql>unlock tables;
二:从服务器(172.17.200.26)
2.1、编辑my.cnf文件
vim /etc/my.cnf
增加 server-id=2
2.2 重启mysql
service mysqld restart
2.3、对从数据库进行相应设置
此处要注意logfile的名称和position的值,其余host、user和password为主数据库设置的账号和密码
mysql> stop slave;
Query OK, 0 rows affected (0.00 sec)
mysql> change master to
-> master_host='172.17.200.25',
-> master_user='dashi',
-> master_password='dashi',
-> master_log_file='bin.log.000001',
-> master_log_pos=713;
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status\G;
在这里主要是看:
Slave_IO_Running=Yes
Slave_SQL_Running=Yes
三、测试:
上述项配置完以后可查看master和slave上线程的状态。在master上,你可以看到slave的I/O线程创建的连接:在master上输入show processlist\G;
mysql> show processlist \G;
*************************** 1. row ***************************
Id: 10865
User: dashi
Host: 172.17.200.25:37369
db: dashi
Command: Sleep
Time: 7
State:
Info: NULL
*************************** 2. row ***************************
Id: 10866
User: dashi
Host: 172.17.200.25:37370
db: dashi
Command: Sleep
Time: 7
State:
Info: NULL
*************************** 3. row ***************************
Id: 10873
User: dashi
Host: 172.17.200.26:37928
db: dashi
Command: Execute
Time: 1
State: Sending data
Info: select count(1) from trade_click where link=?
*************************** 4. row ***************************
Id: 10874
User: dashi
Host: 172.17.200.26:37929
db: dashi
Command: Sleep
Time: 9
State:
Info: NULL
*************************** 5. row ***************************
Id: 10882
User: dashi
Host: 172.17.200.26:37962
db: dashi
Command: Sleep
Time: 78
State:
Info: NULL
ERROR:
No query specified
3.1、在主数据库:192.168.0.107上添加新数据
3.2、在从库查找记录,是否存在
四:总结
主服务器master记录数据库操作日志到Binary log,从服务器开启i/o线程将二进制日志记录的操作同步到relay log(存在从服务器的缓存中),另外sql线程将relay log日志记录的操作在从服务器执行,从而达到主从复制。
最新文章
- 学习笔记:java线程安全
- yum源使用(转)
- Oracle EBS Form 发布到Server端的注意事项
- 将文件路径以";\";隔开
- BZOJ4610——[Wf2016]Ceiling Functi
- (转)C#序列化和反序列化小例子
- [原]AngularJS iframe打开不同域的内容时报错误
- Http,Https (SSL)的Url绝对路径,相对路径解决方案Security Switch 4.2 英文帮助文档 分类: ASP.NET 2014-10-28 10:50 147人阅读 评论(1) 收藏
- IP Editor IP控件(对比一下封装IP控件)
- linux/Windows系统如何安装PHP-openssl扩展
- pyhton 关于 configparser 配置 模块 实践使用中碰到的坑
- AppDir【创建缓存目录】
- Fescar(Seata)-Springcloud流程分析-2阶段
- hash 位运算 练习
- HDU 4802 &;&; HDU 4803 贪心,高精 &;&; HDU 4804 轮廓线dp &;&; HDU 4805 计算几何 &;&; HDU 4811 (13南京区域赛现场赛 题目重演A,B,C,D,J)
- Cannot find config.m4. Make sure that you run '/usr/local/php/bin/phpize' in the top level source directory of the module的 解决方法
- XMPP资源绑定(Resource Binding)与单台设备登录控制
- Python3:文件读写
- git学习——远程仓库操作
- GitKraken使用教程-基础部分(2)