秒杀MYSQL单双向同步

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
云数据库 RDS MySQL,高可用系列 2核4GB
简介:
简介 :
采用脚本直接配置, 请事先安装好MYSQL,附件中脚本包里有4个文件分别为mysqlsa.py(主程序)、mysqldb(数据库模块)、master(参数)、slave(参数)
程序文件下载地址:http://down.51cto.com/data/229410
Sever info:
  • Master:192.168.1.3
  • Slave:192.168.1.4
脚本使用教程:
Master:192.168.1.3
  1. 1、解压TAR包 
  2. [root@SQL1 down]# tar -zxf mysql-master-slave.tar.gz
  3. 2、进入目录设置权限
  4. [root@SQL1 down]# cd mysql-master-slave 
    [root@SQL1 mysql-master-slave]# chmod 777 mysqlsa.py
  5. 3、打开master文件,设置你需要同步的库
  6. [root@SQL1 mysql-master-slave]# vi master 
    log-bin=/var/log/mysql/mysql-bin 
    binlog-ignore-db=mysql,test 
    binlog-do-db=wgdb 
    log-bin=/var/log/mysql/updatelog
  7. 4、运行脚本程序(这里注意如果你的MYSQL默认安装路经不是‘/usr/local/mysql'在此脚本程序的10~11行把成“export LD_LIBRARY_PATH=your path '你自己路经地址,否则有可能程序里面MySQLdb模块报错
  8. [root@SQL1 mysql-master-slave]# ./mysqlsa.py    
    ---------------------------------------------------------------------- 
                                    1)Installation MySQL Master 

                                    2)Installation MySQL Slave 

                                    Q)Quit Installation 

    ---------------------------------------------------------------------- 
                                    Plase,Enter your  option:
  9. 1:配置安装MYSQL主库
  10. 2:配置安装MYSQL从库
  11. Q:退出

5、我们选择“1”配置MYSQL主库
[root@SQL1 mysql-master-slave]# ./mysqlsa.py    
---------------------------------------------------------------------- 
                                1)Installation MySQL Master 

                                2)Installation MySQL Slave 

                                Q)Quit Installation 

---------------------------------------------------------------------- 
                                 Plase,Enter your option:1
6、提示要求输入SLAVE服务器IP,这步是创建访问帐号用的,我们输入SLAVE的IP:192.168.1.4,注意:千万别输错,不然SLAVE访问就会出错
                             Plase,Enter your  option:1 
Please,input your slave server ip: 192.168.1.4
7、OK,执行完成以后程序不会给你任何提示!我们直接安Q退出,查看/etc/my.cnf的57行下面内容是否成功写入
54 # required unique id between 1  and 2^32 - 1 
55 # defaults  to 1  if master-host  is  not  set 
56 # but will  not  function  as a master  if omitted 
57 server-id             = 1 
58 log-bin=/var/log/mysql/mysql-bin 
59 binlog-ignore-db=mysql,test 
60 binlog-do-db=wgdb 
61 log-bin=/var/log/mysql/updatelog
  1.  8、重启MYSQL 
  2.  
    [root@SQL1 down]# service mysqld restart



Slave:192.168.1.4
  1. 1、解压TAR包
  2. [root@SQL2 down]# tar -zxf mysql-master-slave.tar.gz
  3. 2、进入目录设置权限
  4. [root@SQL2 down]# cd mysql-master-slave 
    [root@SQL2 mysql-master-slave]#
  5. 3、打开slave文件,设置你需要同步的库和master的IP
  6. master-host = 192.168.1.3 
    master-user = replication 
    master-password = *.asDICf.1KD 
    master-port = 3306
    master-connect-retry = 60 
    replicate-ignore-db = mysql,test 
    replicate-do-db = wgdb 
    relay-log=/var/log/mysql/slave-relay-bin 
    slave-skip-errors=all
  7. 4、运行脚本程序(这里注意如果你的MYSQL默认安装路经不是‘/usr/local/mysql'在此脚本程序的10~11行把成“export LD_LIBRARY_PATH=your path '你自己路经地址,否则有可能程序里面MySQLdb模块报错
  8. [root@SQL2 mysql-master-slave]# ./mysqlsa.py    
    ---------------------------------------------------------------------- 
                                    1)Installation MySQL Master 

                                    2)Installation MySQL Slave 

                                    Q)Quit Installation 

    ---------------------------------------------------------------------- 
                                    Plase,Enter your option:
  9. 5、我们选择‘2’安装从库
  10. [root@SQL2 mysql-master-slave]# ./mysqlsa.py    
    ---------------------------------------------------------------------- 
                                    1)Installation MySQL Master 

                                    2)Installation MySQL Slave 

                                    Q)Quit Installation 

    ---------------------------------------------------------------------- 
                                    Plase,Enter your  option:2
  11. 6、检查下‘/etc/my.cnf' 57行内容是否写入
  12. # required unique id between 1  and 2^32 - 1 
    # defaults  to 1  if master-host  is  not  set 
    # but will  not  function  as a master  if omitted 
    server-id             = 2 
    master-host = 192.168.1.3 
    master-user = replication 
    master-password = *.asDICf.1KD 
    master-port = 3307 
    master-connect-retry = 60 
    replicate-ignore-db = mysql,test 
    replicate-do-db = wgdb 
    relay-log=/var/log/mysql/slave-relay-bin 
    slave-skip-errors=all
  13. 7、重起MYSQL
  14. [root@SQL2 mysql-master-slave]# service mysqld restart
  1. 最后一步
  2. 1、进入Master 调整参数
  3. [root@SQL1 down]# mysql -uroot -p 
    Enter password:    
    Welcome  to the MySQL monitor.    Commands  end  with ;  or \g. 
    Your MySQL connection id  is 2 
    Server version: 5.1.57-log Source distribution 

    Copyright (c) 2000, 2010, Oracle  and/ or its affiliates. All rights reserved. 
    This software comes  with ABSOLUTELY NO WARRANTY. This  is free software, 
    and you are welcome  to modify  and redistribute it under the GPL v2 license 

    Type  'help;' or '\h' for help. Type '\c' to clear the current input statement. 

    mysql> slave stop; 
    Query OK, 0 rows affected, 1 warning (0.00 sec) 

    mysql>     show master status; 
    +------------------+----------+--------------+------------------+ 
    | File                         | Position | Binlog_Do_DB | Binlog_Ignore_DB | 
    +------------------+----------+--------------+------------------+ 
    | updatelog.000001 |            106 | wgdb                 | mysql,test             | 
    +------------------+----------+--------------+------------------+ 
    1 row  in  set (0.00 sec)
  1. 2、进入slave 调整参数
  2. [root@SQL2 mysql-master-slave]# mysql -uroot -p 
    Enter password:    
    Welcome  to the MySQL monitor.    Commands  end  with ;  or \g. 
    Your MySQL connection id  is 4 
    Server version: 5.1.57-log Source distribution 

    Copyright (c) 2000, 2010, Oracle  and/ or its affiliates. All rights reserved. 
    This software comes  with ABSOLUTELY NO WARRANTY. This  is free software, 
    and you are welcome  to modify  and redistribute it under the GPL v2 license 

    Type  'help;' or '\h' for help. Type '\c' to clear the current input statement. 

    mysql> slave  stop
    Query OK, 0 rows affected (0.00 sec) 

    mysql> CHANGE MASTER  TO MASTER_LOG_FILE= 'updatelog.000001',MASTER_LOG_POS=106; 
    Query OK, 0 rows affected (0.02 sec) 

    mysql> slave start; 
    Query OK, 0 rows affected (0.01 sec) 
  3. 3、检查是否成功
  4. Slave_IO_Running:    Yes 
    Slave_SQL_Running: Yes
以上配置已经全部完成,很多朋友可能会问,这只是单向同步吗?
答:只要把两个操作反向做下就OK
脚本程序问题在哪里?
答:http://down.51cto.com/data/229410

以下是主程序的代码
#!/usr/bin/python 

#This  is a mysql reversed sync     and data pool script. 
import os,MySQLdb 
import sys 
import re 
#Check that Mysql exists 
if os.path.isfile( '/etc/my.cnf'): 
        b=os.system( 'python mysqldb.py') 
        profile = open( '/etc/profile','r+') 
         if re.search( 'export LD_LIBRARY_PATH=/usr/local/mysql/lib/mysql/',profile.read()) is None: 
                profile.write( 'export LD_LIBRARY_PATH=/usr/local/mysql/lib/mysql/') 
        profile.close 
                 
else
        print  'Your mysql not installiation' 
        sys. exit(1) 

class msa: 
        def sql(self): 
                try:    
                        try: 
                                conn = MySQLdb.connect(host =  'localhost',user='root',passwd='') 
                        except: 
                                pw = raw_input( 'Please,input your amdin of mysql passwd:') 
                                conn = MySQLdb.connect(host =  'localhost',user='root',passwd='%s')%pw 
                        IP = raw_input( 'Please,input your slave server ip:') 
                        cursor = conn.cursor() 
                        sql =  "GRANT REPLICATION SLAVE,REPLICATION CLIENT,RELOAD,SUPER ON *.* TO    'replication'@'%s' identified by '*.asDICf.1KD'; Flush privileges; "%(IP) 
                        cursor. execute(sql) 
                except Exception,e: 
                        print e 
                        sys. exit() 
        def mk(self): 
                var= '/var/log/mysql' 
                 if  not os.path.isdir(var): 
                        os.makedirs(var) 
                        os.chmod(var,448|56) 
                        os.chown(var,500,500) 
        def master(self): 
                my_conf = open( '/etc/my.cnf','r') 
                my_conf_read = my_conf.readlines() 
                my_conf.close 
                 if re.search( 'binlog-do-db',str(my_conf_read)) is None: 
                        master_file = open( 'master','r') 
                        read = master_file.read() 
                        my_conf_read.insert(57,read) 
                        my_conf = open( '/etc/my.cnf','w') 
                        my_conf.writelines(my_conf_read) 
                        master_file.close 
                        my_conf.close 
        def slave(self): 
                my_conf = open( '/etc/my.cnf','r') 
                my_conf_read = my_conf.readlines() 
                my_conf.close 
                 if re.search( 'replicate-do-db',str(my_conf_read)) is None: 
                        master_file = open( 'slave','r') 
                        read = master_file.read() 
                        my_conf_read.insert(57,read) 
                        my_conf = open( '/etc/my.cnf','w') 
                        my_conf.writelines(my_conf_read) 
                        master_file.close 
        def re(self): 
                my_conf = open( '/etc/my.cnf','r') 
                #my_conf_read = my_conf.readlines() 
                my_conf.close 
                 if info    ==  '1': 
    = re. sub()    = re. sub( 'log-bin=mysql-bin','#log-bin=mysql-bin',my_conf.read()) 
                 else
                         if re.search( 'binlog-do-db',my_conf.read()) is None: 
                                my_conf.seek(0) 
    = re. sub()    = re. sub( 'server-id\t= 1\n','server-id\t= 2\n',my_conf.read()) 
                         else
                                my_conf.seek(0) 
= my_conf.read() = my_conf.read() 
                my_conf = open( '/etc/my.cnf','w') 
                my_conf.writelines( sub
                my_conf.close 

                 

m = msa() 
while 1 > b: 
        #os.system( 'clear') 
        print  '--' * 35 
        print  '\t\t1)Installation MySQL Master\n' 
        print  '\t\t2)Installation MySQL Slave\n' 
        print  '\t\tQ)Quit Installation\n' 
        print  '--' * 35 
         if ( 'err' in dir()) is True: 
                print err 
                del err 
        info = raw_input( '\t\tPlase,Enter your option:') 
         if info.lower() ==  'q': 
                sys. exit() 
        elif info ==  '1': 
                m.sql() 
                m.mk() 
                m.master() 
                m.re() 
        elif info ==  '2': 
                m.mk() 
                m.slave() 
                m.re() 
         else
                err = '\t\t***You enter the option error***' 


















本文转自 ibelieveme 51CTO博客,原文链接:http://blog.51cto.com/skyson/627833,如需转载请自行联系原作者
相关实践学习
如何快速连接云数据库RDS MySQL
本场景介绍如何通过阿里云数据管理服务DMS快速连接云数据库RDS MySQL,然后进行数据表的CRUD操作。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
打赏
0
0
0
0
235
分享
相关文章
如何将数据从MySQL同步到其他系统
【10月更文挑战第17天】如何将数据从MySQL同步到其他系统
942 0
MySQL 备份 Shell 脚本:支持远程同步与阿里云 OSS 备份
一款自动化 MySQL 备份 Shell 脚本,支持本地存储、远程服务器同步(SSH+rsync)、阿里云 OSS 备份,并自动清理过期备份。适用于数据库管理员和开发者,帮助确保数据安全。
MySQL原理简介—12.MySQL主从同步
本文介绍了四种为MySQL搭建主从复制架构的方法:异步复制、半同步复制、GTID复制和并行复制。异步复制通过配置主库和从库实现简单的主从架构,但存在数据丢失风险;半同步复制确保日志复制到从库后再提交事务,提高了数据安全性;GTID复制简化了配置过程,增强了复制的可靠性和管理性;并行复制通过多线程技术降低主从同步延迟,保证数据一致性。此外,还讨论了如何使用工具监控主从延迟及应对策略,如强制读主库以确保即时读取最新数据。
MySQL原理简介—12.MySQL主从同步
Flink CDC MySQL同步MySQL错误记录
在使用Flink CDC同步MySQL数据时,常见的错误包括连接错误、权限错误、表结构变化、数据类型不匹配、主键冲突和
259 17
Maxwell:binlog 解析器,轻松同步 MySQL 数据
Maxwell:binlog 解析器,轻松同步 MySQL 数据
666 11
Mysql主从同步 清理二进制日志的技巧
Mysql主从同步 清理二进制日志的技巧
62 1
mysql 主从同步 实现增量备份
【8月更文挑战第28天】mysql 主从同步 实现增量备份
126 3
实时计算 Flink版产品使用问题之使用CTAS同步MySQL到Hologres时出现的时区差异,该如何解决
实时计算Flink版作为一种强大的流处理和批处理统一的计算框架,广泛应用于各种需要实时数据处理和分析的场景。实时计算Flink版通常结合SQL接口、DataStream API、以及与上下游数据源和存储系统的丰富连接器,提供了一套全面的解决方案,以应对各种实时计算需求。其低延迟、高吞吐、容错性强的特点,使其成为众多企业和组织实时数据处理首选的技术平台。以下是实时计算Flink版的一些典型使用合集。
MySQL主从同步如何操作?
随着业务增长,单台MySQL服务器难以应对高并发访问和潜在的故障风险。主从同步(Master-Slave)通过读写分离提升数据库处理能力,具备多项优势:读写分离减轻主数据库压力、支持一主多从增强扩展性与高可用性、以及数据备份确保容灾恢复。MySQL利用binlog实现主从数据同步,记录所有写操作,不包含查询。binlog有三种格式:Statement(基于SQL语句)、Row(基于行更改)、Mixed(结合前两者优点)。主从复制涉及三个关键线程:主库的binlog dump thread和从库的I/O thread与SQL thread。
310 0
MySQL主从同步如何操作?
MySQL主从同步如何保证数据一致性?
MySQL主从同步如何保证数据一致性?
616 0
MySQL主从同步如何保证数据一致性?
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等