0

0

Mysql的主从复制和级联_MySQL

php中文网

php中文网

发布时间:2016-06-01 13:30:22

|

1193人浏览过

|

来源于php中文网

原创

bitsCN.com

mysql的主从复制和级联

 

记录一下mysql的主主复制和级联复制。 

一、环境

主机:192.25.10.71

主机:192.25.10.73

从机:192.25.10.76

DB:Mysql 5.5.31

OS:CentOS 6.3

二、架构  

 

Mysql的主从复制和级联_MySQL

 

三、实施步骤 

mysql的安装略 

1.master端创建测试DB

[root@proxy1 ~]# mysqlWelcome to the MySQL monitor.  Commands end with ; or /g.Your MySQL connection id is 1959Server version: 5.5.31-log Source distributionCopyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '/h' for help. Type '/c' to clear the current input statement.mysql> create database db_kenyon;Query OK, 1 row affected (0.03 sec)mysql> show databases;+--------------------+| Database           |+--------------------+| information_schema || db_kenyon          || mysql              || performance_schema || test               |+--------------------+5 rows in set (0.00 sec)mysql> use db_kenyon;Database changedmysql> create table t_kenyon(id int);Query OK, 0 rows affected (0.07 sec)mysql> insert into t_kenyon values(1),(2),(3);Query OK, 3 rows affected (0.01 sec)Records: 3  Duplicates: 0  Warnings: 0mysql> select * from t_kenyon;+------+| id   |+------+|    1 ||    2 ||    3 |+------+3 rows in set (0.00 sec)mysql> show master status;Empty set (0.00 sec)[root@localhost ~]#2.修改配置/etc/my.cnf[mysqld]log-bin=mysql-binserver-id    =1 binlog-do-db=db_kenyoninnodb_data_home_dir = /usr/local/mysql/data/innodb_log_group_home_dir = /usr/local/mysql/data/innodb_buffer_pool_size = 256Minnodb_additional_mem_pool_size = 20Minnodb_log_file_size = 64Minnodb_log_buffer_size = 8Minnodb_flush_log_at_trx_commit = 1innodb_lock_wait_timeout = 503.创建slave端连到master端的用户,重启master[root@localhost ~]# /etc/init.d/mysqld restartShutting down MySQL.. SUCCESS! Starting MySQL............ SUCCESS![root@localhost ~]# mysqlmysql>grant replication slave on *.* to repl@'192.25.10.73' identified by '123456';mysql> flush privileges;[root@localhost ~]#4.备份master端数据,并拷贝至slave端mysql> flush tables with read lock;Query OK, 0 rows affected (0.00 sec)mysql>  show master status;+------------------+----------+---------------+--------------------------------------------------+-------------------+| File             | Position | Binlog_Do_DB  | Binlog_Ignore_DB                                 | Executed_Gtid_Set |+------------------+----------+---------------+--------------------------------------------------+-------------------+| mysql-bin.000002 |      120 | db_kenyon |  |                   |+------------------+----------+---------------+--------------------------------------------------+-------------------+1 row in set (0.00 sec)mysql> 另外一个session:[root@localhost log]# cd /usr/local/mysql/data[root@localhost data]# tar -zcvf backup.tar.gz db_kenyon[root@localhost ]# scp backup.tar.gz root@192.25.10.73:/usr/local/mysql/data/ --回到之前的session,从机搭完以后解锁,保证数据一致mysql> unlock tables;Query OK, 0 rows affected (0.00 sec)5.修改slave端的数据[mysqld]server-id  = 2master-host     =   192.25.10.71   #5.5以上废弃master-user     =   repl            #5.5以上废弃master-password =   123456           #5.5以上废弃master-port     =   3306                  #5.5以上废弃master-connect-retry=60           #重试时间60秒, #5.5以上废弃replicate-do-db=db_kenyon         #需要同步的库log-slave-updates=1               #启用从库日志,这样可以设置链式复制read-only = 1   #0表示可读写,1表示只读,但是只针对普通用户,超级用户和同步用户不受此限制6.重启slave,并指定主机检查mysql> change master to master_host='192.25.10.71',master_user='repl',master_password='123456',master_log_file='mysql-bin.000002',master_log_pos=120mysql> show slave status/G*************************** 1. row ***************************               Slave_IO_State:                  Master_Host: 192.25.10.71                  Master_User: repl                  Master_Port: 3306                Connect_Retry: 60              Master_Log_File: mysql-bin.000002          Read_Master_Log_Pos: 120               Relay_Log_File: localhost-relay-bin.000001                Relay_Log_Pos: 4        Relay_Master_Log_File: mysql-bin.000002             Slave_IO_Running: No            Slave_SQL_Running: No              Replicate_Do_DB: db_kenyon          Replicate_Ignore_DB:           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: 120              Relay_Log_Space: 120              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: NULLMaster_SSL_Verify_Server_Cert: No                Last_IO_Errno: 0                Last_IO_Error:               Last_SQL_Errno: 0               Last_SQL_Error:  Replicate_Ignore_Server_Ids:             Master_Server_Id: 0                  Master_UUID:             Master_Info_File: /usr/local/mysql/data/master.info                    SQL_Delay: 0          SQL_Remaining_Delay: NULL      Slave_SQL_Running_State:           Master_Retry_Count: 86400                  Master_Bind:      Last_IO_Error_Timestamp:     Last_SQL_Error_Timestamp:               Master_SSL_Crl:           Master_SSL_Crlpath:           Retrieved_Gtid_Set:            Executed_Gtid_Set:                Auto_Position: 01 row in set (0.00 sec)

 

调整网络和mysql参数等,使两个参数:Slave_IO_Running, Slave_SQL_Running的状态为YES 

 

7.级联过程 

其他配置如上,注意级联参数log-slave-updates开启,该参数在my.cnf里值应为1,或者

羊驼CMS 企业版3.8
羊驼CMS 企业版3.8

羊驼 v3.8 企业版(仿阿里巴巴单企业界面)方便中小企业建站使用,包含产品、新闻、网上询价等模块。羊驼! 是一个开源的轻量级树状 CMS 系统。 它基于 php + mysql 并以 b2core MVC 为底层架构。 可以方便快速的配置出个人、企业网站。在B2Core 优雅的MVC架构之上你可以轻松定制任意应用型网站。 羊驼 v3.8 企业版更新:修复了后台错误,和前台图片地址问题。 企业版主

下载

mysql> show variables like 'log_slave_%';+-------------------+-------+| Variable_name     | Value |+-------------------+-------+| log_slave_updates | ON   |+-------------------+-------+1 row in set (0.00 sec)

 

8.主主的过程就是主从的基础上再以从机为主机,反向操作一遍即可。简单测试,在71上建表,73、76上都能看到该表即成功。 

 

四、级联的性能损耗测试 

使用mysqlslap

[root@proxy1 bin]# ./mysqlslap   --concurrency=2  --iterations=5 --number-int-cols=10 --number-char-cols=10 -a --auto-generate-sql-guid-primary --number-of-queries=10000 --auto-generate-sql-load-type=writeBenchmark        Average number of seconds to run all queries: 5.324 seconds        Minimum number of seconds to run all queries: 4.106 seconds        Maximum number of seconds to run all queries: 6.113 seconds        Number of clients running queries: 2        Average number of queries per client: 5000

 

每秒处理能力1878

改变concurrency,得到如下结果:

 

并发数 平均时间s(后者开启级联) 每秒处理 损耗

2 5.324 1878 0.6s  11%

2 5.912 1691 0.6s  11%

10 2.521 3966 0.3s  12%

10 2.828 3536 0.3s  12%

50 2.161 4627 0.2s  7%

50 2.331 4290 0.2s  7%

100 2.355 4246 0.3s  11%

100 2.618 3819 0.3s  11%

bitsCN.com

热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

通义千问
通义千问

阿里巴巴推出的全能AI助手

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
c语言中/相关合集
c语言中/相关合集

本专题整合了c语言中/的用法、含义解释。阅读专题下面的文章了解更多详细内容。

0

2026.02.03

漫蛙漫画网页版入口与正版在线阅读 漫蛙MANWA官网访问专题
漫蛙漫画网页版入口与正版在线阅读 漫蛙MANWA官网访问专题

本专题围绕漫蛙漫画(Manwa / Manwa2)官网网页版入口进行整理,涵盖漫蛙漫画官方主页访问方式、网页版在线阅读入口、台版正版漫画浏览说明及基础使用指引,帮助用户快速进入漫蛙漫画官网,稳定在线阅读正版漫画内容,避免误入非官方页面。

0

2026.02.03

Yandex官网入口与俄罗斯搜索引擎访问指南 Yandex中文登录与网页版入口
Yandex官网入口与俄罗斯搜索引擎访问指南 Yandex中文登录与网页版入口

本专题汇总了俄罗斯知名搜索引擎 Yandex 的官网入口、免登录访问地址、中文登录方法与网页版使用指南,帮助用户稳定访问 Yandex 官网,并提供一站式入口汇总。无论是登录入口还是在线搜索,用户都能快速获取最新稳定的访问链接与使用指南。

3

2026.02.03

Java 设计模式与重构实践
Java 设计模式与重构实践

本专题专注讲解 Java 中常用的设计模式,包括单例模式、工厂模式、观察者模式、策略模式等,并结合代码重构实践,帮助学习者掌握 如何运用设计模式优化代码结构,提高代码的可读性、可维护性和扩展性。通过具体示例,展示设计模式如何解决实际开发中的复杂问题。

2

2026.02.03

C# 并发与异步编程
C# 并发与异步编程

本专题系统讲解 C# 异步编程与并发控制,重点介绍 async 和 await 关键字、Task 类、线程池管理、并发数据结构、死锁与线程安全问题。通过多个实战项目,帮助学习者掌握 如何在 C# 中编写高效的异步代码,提升应用的并发性能与响应速度。

0

2026.02.03

Python 强化学习与深度Q网络(DQN)
Python 强化学习与深度Q网络(DQN)

本专题深入讲解 Python 在强化学习(Reinforcement Learning)中的应用,重点介绍 深度Q网络(DQN) 及其实现方法,涵盖 Q-learning 算法、深度学习与神经网络的结合、环境模拟与奖励机制设计、探索与利用的平衡等。通过构建一个简单的游戏AI,帮助学习者掌握 如何使用 Python 训练智能体在动态环境中作出决策。

2

2026.02.03

python end=
python end=

本专题整合了python中end=的相关内容,阅读专题下面的文章了解更多详细内容。

0

2026.02.03

python运算符优先级
python运算符优先级

本专题整合了python运算符优先级排序、用法相关内容,阅读专题下面的文章了解更多详细内容。

1

2026.02.03

python print用法与作用
python print用法与作用

本专题整合了python print的用法、作用、函数功能相关内容,阅读专题下面的文章了解更多详细教程。

1

2026.02.03

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
MySQL 教程
MySQL 教程

共48课时 | 2.1万人学习

MySQL 初学入门(mosh老师)
MySQL 初学入门(mosh老师)

共3课时 | 0.3万人学习

简单聊聊mysql8与网络通信
简单聊聊mysql8与网络通信

共1课时 | 821人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号