配置Mysql数据库的主从同步教程
更新时间:2024-05-25 02:27:01 阅读量: 综合文库 文档下载
- mysql数据库主从复制推荐度:
- 相关推荐
配置Mysql数据库的主从同步教程
主从同步是实现网站分布式数据处理一个非常常用的方案了,今天我来为各位介绍配置Mysql数据库的主从同步(双主)教程,希望下文能帮助到各位哦.
配置Mysql数据库的主从同步(一主一从).
一、主库开启BINLOG、server-id
[root@Master-Mysql ~]# grep -E \ log-bin = /usr/local/mysql/data/mysql-bin server-id = 1
mysql> show variables like '%log_bin%';
+---------------------------------+---------------------------------------+
| Variable_name | Value +---------------------------------+---------------------------------------+
| log_bin | ON | log_bin_basename | /usr/local/mysql/data/mysql-bin | | log_bin_index | /usr/local/mysql/data/mysql-bin.index | | log_bin_trust_function_creators | OFF | | log_bin_use_v1_row_events | OFF | sql_log_bin | ON +---------------------------------+---------------------------------------+ 6 rows in set (0.01 sec) --phpfensi.com mysql> show variables like '%server_id%'; +----------------+-------+ | Variable_name | Value | +----------------+-------+ | server_id | 1 | | server_id_bits | 32 | +----------------+-------+ 2 rows in set (0.00 sec)
备注:以上两个信息必须在[mysqld]模块下!!!
二、给从库授权
mysql> grant replication slave on *.* to byrd@'192.168.199.%' identified by 'admin'; mysql> flush privileges;
mysql> select user,host from mysql.user; +------+---------------+ | user | host | +------+---------------+ | root | 127.0.0.1 | | byrd | 192.168.199.% |
| | | | | root | ::1 | | root | lamp | | root | localhost | +------+---------------+ 5 rows in set (0.00 sec) 锁表前建立点数据:
mysql> create database hitest; mysql> show databases; +--------------------+
| Database | +--------------------+ | hitest | +--------------------+ 6 rows in set (0.00 sec) mysql> use hitest;
mysql> create table test(
-> id int(4) not null primary key auto_increment, -> name char(20) not null -> );
Query OK, 0 rows affected (1.80 sec) mysql> show tables ; +------------------+ | Tables_in_hitest | +------------------+ | test | +------------------+
mysql> insert into test(id,name) values(1,'zy'); mysql> select * from test; +----+------+ | id | name | +----+------+ | 1 | zy | +----+------+
三、锁表、备份、解锁
mysql> flush table with read lock; #锁表
mysql> show variables like '%timeout%'; #锁表时间 +-----------------------------+----------+
| Variable_name | Value | +-----------------------------+----------+
| interactive_timeout | 28800 | | wait_timeout | 28800 | +-----------------------------+----------+
12 rows in set (0.06 sec)
mysql> show master status; #binlog日志位置
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000004 | 1305 | | | | +------------------+----------+--------------+------------------+-------------------+ 1 row in set (0.03 sec)
[root@Master-Mysql ~]# /usr/local/mysql/bin/mysqldump -uroot -p'' -B -A |gzip >/tmp/all.sql.gz #新窗口备份 Enter password:
mysql> unlock table; #解锁
###############解锁后主库操作如下:############### mysql> use hitest
mysql> insert into test(id,name) values(2,'binghe'); mysql> select * from test; +----+--------+ | id | name | +----+--------+ | 1 | zy | | 2 | binghe | +----+--------+
mysql> create database hxy;
###############解锁后主库操作完成~############### 备注:备份数据需要重新打开新窗口,不然锁表就自动失效.
四、主库导入到从库
################主库操作################ [root@Master-Mysql tmp]# ll
-rw-r--r--. 1 root root 162236 Jul 8 21:30 all.sql.gz [root@Master-Mysql tmp]# gzip -d all.sql.gz [root@Master-Mysql tmp]# ll
-rw-r--r--. 1 root root 590351 Jul 8 21:30 all.sql ################主库完成################
##备注:将主库导出的all.sql通过scp、ssh、sftp等方式拷贝到从库服务器,此处略## [root@Slave-Mysql ~]# grep log-bin /etc/my.cnf #log-bin = /usr/local/mysql/data/mysql-bin
[root@Slave-Mysql ~]# grep server-id /etc/my.cnf server-id = 2
[root@Slave-Mysql ~]# /etc/init.d/mysqld restart
[root@Slave-Mysql tmp]# /usr/local/mysql/bin/mysql -uroot -p'admin'
mysql> use hitest;
mysql> select * from test; +----+------+ | id | name | +----+------+ | 1 | zy | +----+------+
1 row in set (0.00 sec) 六、从库配置信息
mysql> CHANGE MASTER TO
-> MASTER_HOST='192.168.199.177', -> MASTER_PORT=3306, -> MASTER_USER='byrd',
-> MASTER_PASSWORD='admin',
-> MASTER_LOG_FILE='mysql-bin.000004', -> MASTER_LOG_POS=1305;
Query OK, 0 rows affected, 2 warnings (1.96 sec)
[root@Slave-Mysql ~]# ll /usr/local/mysql/data/master.info ##备注:master.info记录MASTER的相关信息! 七、启动从库同步
mysql> start slave;
mysql> show slave status\G
Slave_IO_Running: Yes Slave_SQL_Running: Yes Seconds_Behind_Master: 0 八、结果测试
mysql> use hitest;
mysql> select * from test; +----+--------+ | id | name | +----+--------+ | 1 | zy | | 2 | binghe | +----+--------+
2 rows in set (0.00 sec)
[root@Master-Mysql ~]# /usr/local/mysql/bin/mysql -uroot -p'' -e \ 主库建立了一个zhihu的数据库 Enter password:
[root@Slave-Mysql ~]# /usr/local/mysql/bin/mysql -uroot -p'' -e \Enter password: +------------------+
# | Database (zhihu) | +------------------+ | zhihu | +------------------+
配置Mysql数据库的主从同步(双主)
已经配置好的:
主库:192.168.199.177
从库:192.168.199.178
[root@Master-Mysql ~]# egrep \/etc/my.cnf
log-bin = /usr/local/mysql/data/mysql-bin #必须 server-id = 1 #必须 log-slave-updates #必须
auto_increment_increment = 2 #必须 auto_increment_offset = 1 #必须
slave-skip-errors = 1032,1062,1007 #非必须,建议
########################主库、从库分隔符######################## [root@Slave-Mysql data]# egrep \ #log-bin = /usr/local/mysql/data/mysql-bin server-id = 2 log-slave-updates
log-bin = /usr/local/mysql/data/mysql-bin #read-only #双主,此选项要注释掉 slave-skip-errors = 1032,1062,1007
auto_increment_increment = 2 #ID自增间隔 auto_increment_offset = 2 #ID初始位置 192.168.199.178: mysql> stop slave;
mysql> flush table with read lock; mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000004 | 120 | | | | +------------------+----------+--------------+------------------+-------------------+
mysql> system /usr/local/mywww.shanxiwang.netsql/bin/mysqldump -uroot -p'' -A -B >/tmp/192.168.199.178.sql #如果主、从一致非必须 mysql> unlock tables; #同上 mysql> system ls -l /tmp/
-rw-r--r--. 1 root root 2887406 Jul 12 22:24 192.168.199.178.sql mysql> start slave; 192.168.199.177:
[root@Master-Mysql ~]# /usr/local/mysql/bin/mysql -uroot -p'' < /tmp/192.168.199.178.sql #如果主、从一致非必须
mysql> update mysql.user set password=PASSWORD('admin') where user='root';
[root@Master-Mysql ~]# cat |/usr/local/mysql/bin/mysql -uroot -p'admin' <
> MASTER_HOST='192.168.199.178', > MASTER_PORT=3306, > MASTER_USER='byrd',
> MASTER_PASSWORD='admin',
> MASTER_LOG_FILE='mysql-bin.000004', > MASTER_LOG_POS=120; > EOF
mysql> start slave;
mysql> show slave status\G
*************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.199.178 Master_User: byrd Master_Port: 3306 Connect_Retry: 60
Master_Log_File: mysql-bin.000004 Read_Master_Log_Pos: 938
Relay_Log_File: mysqld-relay-bin.000002 Relay_Log_Pos: 1101
Relay_Master_Log_File: mysql-bin.000004 Slave_IO_Running: Yes Slave_SQL_Running: Yes Last_Errno: 0 Skip_Counter: 0 Exec_Master_Log_Pos: 938 Relay_Log_Space: 1275 Until_Condition: None Until_Log_Pos: 0 Master_SSL_Allowed: No Seconds_Behind_Master: 0 Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 0 Last_SQL_Errno: 0 Master_Server_Id: 2
Master_UUID: 34d672c3-d292-11e3-9ff5-00155dc7834c Master_Info_File: /usr/local/mysql/data/master.info
SQL_Delay: 0 --phpfensi.com SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
Master_Retry_Count: 86400 测试:192.168.199.177:
mysql> use hitest;
mysql> CREATE TABLE `ces` (
-> `REL_ID` bigint(12) NOT NULL auto_increment COMMENT 'id', -> `TITLE` varchar(255) NOT NULL COMMENT 'biaoti', -> PRIMARY KEY (`REL_ID`)
-> ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8; mysql> insert into ces(TITLE) values('test'); mysql> insert into ces(TITLE) values('test'); mysql> insert into ces(TITLE) values('test'); mysql> insert into ces(TITLE) values('test25'); mysql> select * from ces; +--------+-------+ | REL_ID | TITLE | +--------+-------+ | 1 | test | | 3 | test | | 5 | test | | 25 | test25| +--------+--------+
3 rows in set (0.03 sec) 192.168.199.178: mysql> use hitest;
mysql> insert into ces(TITLE) values('test26'); mysql> insert into ces(TITLE) values('test28'); mysql> insert into ces(TITLE) values('test30'); mysql> select * from ces; +--------+--------+ | REL_ID | TITLE | +--------+--------+ | 1 | test | | 3 | test | | 5 | test | | 26 | test26 | | 28 | test28 | | 30 | test30 | +--------+--------+
17 rows in set (0.00 sec)
说明:如果一主、一丛已经做好,只要知道从库位置点(show master status;)、然后之前主库执行(CHANGE MASTER)、之前主库开启slave(start slave)即可,其中数据库备份等步骤可以省略,如果主从有一些数据库不一致则同上操作.
正在阅读:
配置Mysql数据库的主从同步教程05-25
成语式广播广告文案-媒力中国08-05
基于虚拟仪器技术取样积分器设计05-09
最新2018党外中青年干部培训班心得体会-word范文(10页)09-26
员工管理的方法08-14
七年级下册数学三角形相关结论总结与证明05-13
婚姻法论文题目12-10
血液透析室管理制度01-04
晶体结构与晶体中的缺陷习题11-29
控烟工作计划03-13
- 多层物业服务方案
- (审判实务)习惯法与少数民族地区民间纠纷解决问题(孙 潋)
- 人教版新课标六年级下册语文全册教案
- 词语打卡
- photoshop实习报告
- 钢结构设计原理综合测试2
- 2014年期末练习题
- 高中数学中的逆向思维解题方法探讨
- 名师原创 全国通用2014-2015学年高二寒假作业 政治(一)Word版
- 北航《建筑结构检测鉴定与加固》在线作业三
- XX县卫生监督所工程建设项目可行性研究报告
- 小学四年级观察作文经典评语
- 浅谈110KV变电站电气一次设计-程泉焱(1)
- 安全员考试题库
- 国家电网公司变电运维管理规定(试行)
- 义务教育课程标准稿征求意见提纲
- 教学秘书面试技巧
- 钢结构工程施工组织设计
- 水利工程概论论文
- 09届九年级数学第四次模拟试卷
- 主从
- 同步
- 配置
- 数据库
- 教程
- Mysql
- iMC7.1智能管理平台操作手册
- 古老的易学文化,生生不息
- 关于大学生创业的文献综述1
- 红日药业-财务分析报告
- 2014高考数学总复习(人教新课标)配套章末综合检测:第8章 圆锥
- 新高一课程有哪些?如何预习?
- 论我国社会保障基金投资运营存在的问题及对策分析
- 建筑工程九大分部
- 2018年东北三省四市教研联合体高考模拟生物试题(市二模)试题及
- 2018年部编人教版二年级语文下册口语交际-注意说话的语气 教案
- 桥梁施工组织设计
- 浅谈运用互联网进行家校共育的有效途径
- 工程传热复习知识
- 核医学重点归纳
- 甲级单位编制电机机电设备项目可行性报告(立项可研+贷款+用地+2
- 八上第四单元第三、四章复习导纲3个—韩明花
- 天体运动 人教版必修2
- 行政事业单位基本建设支出财务管理若干问题及对策
- 发达国家的对外投资政策及实例分析
- 精选-苏教版五年级上册语文寒假作业答案-文档资料