LAMP MySQL 设置数据同步

来源:互联网 发布:ddos攻击网络 编辑:程序博客网 时间:2024/05/22 13:31
由于服务器主机的数据库的越来越大,而没有测试环境,我为了搭建这个测试的环境也费了不少的周折。
最主要的问题就是数据同步了,之前用MySQL并不多,而现在由于系统是由LAMP组合,不得不对MySQL做深入的研究了,首当其冲的就是数据库备份、还原和同步了。 关于数据库备份和还原网上随便Google一下,Baidu一下,估计也分不出来菜鸟和高手了。而数据同步却让我"蛋疼"了两三天。

在网上都详细的MySQL数据同步步骤说明,我也是参考这些资源做的,但别人的成功不一定是你的成功,别人的经验更不一定是你的经验。其实不单单这次的经历是这样的感受,很多次遇到不同的问题,网上都有不同的解决方案,择其优者而用之,但也不尽其然,因为每个工作环境 、服务器环境、版本信息等的不同都会导致形象各异的错误来。



根据这两天关于对MySQL 的数据同配置学习,发现其实有些步骤是不需要过多考虑的,如果你只想为了简单的数据同步,也就是让 Slave主机只读 Master主机的数据(即单向同步)。步骤如下:


一、配置Master MySQL  
   配置前请注意各服务器的 版本信息是否一致,为了达更好的效果最好是保持版本信息一致。
    1.修改 my.cnf 文件,在
# Replication Master Server (default)
# binary logging is required for replicatio    n
添加如下内容:
log-bin=log-bin=mysql-bin    一般这个都已经被注释了,如果不需要个性配置,直接打开被注释的就可以了
server-id = 1
binlog-do-db=test
binlog-ignore-db=mysql
   2.重启MySQL 
   3.添加专用同步数据库的用户及权限
      GRANT REPLICATION SLAVE,RELOAD,SUPER, ON *.* TO back@% IDENTIFIED BY 'back' ;
      FLUSH PRIVILEGES ; 
    4.查看 master 信息
      show master status;
     如:
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000002 |     7934 | test         | mysql            |
+------------------+----------+--------------+------------------+
      
 

二、配置Slave MySQL
    Slave 服务器不需要配置my.cnf  
    因为之前我是参考网上的步骤写的,结果mySQL一直启不来了,我看了一下日志都是 master-host ,master-user 等变量无法识别,最后我将my.cnf还原,只要了最后一步 执行的SQL语就可以数据同步了。
    待Master 配置完成后,在Slave Server 执行如下SQL:
    change master to master_host='192.168.1.2',master_user='back',master_password='back',master_log_file='mysql-bin.000002' ,master_log_pos= 7934 ;
    注意:这里的master_log_file 和 master_log_pos  的值是从Master Server 里的 show master status 配置信息取的。

   查看Slave 信息:
    show slave status\G;

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.1.2
                  Master_User: back
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000002
          Read_Master_Log_Pos: 7934
               Relay_Log_File: server201-relay-bin.000005
                Relay_Log_Pos: 8080
        Relay_Master_Log_File: mysql-bin.000002
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          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: 7934
              Relay_Log_Space: 8240
              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 MySQL中的Test中新表、添加数据,查看Slave 是否也同步更新了~~