Linux中Mysql配置主从同步

来源:互联网 发布:手机屏幕跑出蜘蛛软件 编辑:程序博客网 时间:2024/06/10 20:27

声明:初次搭建,以跑起来为目的,高深的东西以后再说。


1.两台Linux服务器,192.168.99.110,192.168.99.165,各部署一个mysql,版本5.1。

2.确定主从,192.168.99.110为主,192.168.99.165为从。

3.从库会登录主库来同步,所以在主库创建从库登录用的用户

  mysql>insert into mysql.user(Host,User,Password) values("192.168.99.165","forslave",password("82993341"));

  mysql>grant all privileges on *.* to forslave@192.168.99.165 identified by '82993341';

  mysql>flush privileges;


4.配置主库,主库配置文件在/etc/my.cnf中,[mysqld]下面加上。

#数据库ID号, 为1时表示为Master,其中master_id必须为1到232–1之间的一个正整数值;   

server-id=1

#启用二进制日志;  

log-bin=mysql-bin

#需要同步的二进制数据库名;  

binlog-do-db=homesite

#不同步的二进制数据库名,如果不设置可以将其注释掉;  

binlog-ignore-db=information_schema

binlog-ignore-db=mysql

binlog-ignore-db=test

#设定生成的log文件名;  

#log-bin=/home/shaoyangdd/logs/mysql-master-bin.log  

#把更新的记录写到二进制文件中;  

log-slave-updates


5.保存上面的配置文件重启数据库。

6.配置从库,同上

#如果需要增加Slave库则,此id往后顺延;  

server-id=2

log-bin=mysql-bin

#主库host  

master-host=192.168.99.110

#在主数据库服务器中建立的用于该从服务器备份使用的用户  

master-user=root

master-password=82993341

master-port=3306

#如果发现主服务器断线,重新连接的时间差;  

master-connect-retry=60

#不需要备份的数据库;   

replicate-ignore-db=mysql

replicate-ignore-db=information_schema

replicate-ignore-db=test

#需要备份的数据库  

replicate-do-db=homesite

log-slave-update


7.保存上面的配置文件重启数据库。


8.进入主数据库服务器,授权replication slave,super和reload。

  mysql>grant replication slave,super,reload on *.* to forslave@192.168.99.165 identified by '82993341'; 


9.进入从库服务器启动slave。

  mysql>slave start;


10.查看状态。

mysql> show slave status\G;

*************************** 1. row ***************************

               Slave_IO_State: Waiting for master to send event

                  Master_Host: 192.168.99.110

                  Master_User: forslave

                  Master_Port: 3306

                Connect_Retry: 60

              Master_Log_File: mysql-bin.000003

          Read_Master_Log_Pos: 351

               Relay_Log_File: mysqld-relay-bin.000007

                Relay_Log_Pos: 496

        Relay_Master_Log_File: mysql-bin.000003

             Slave_IO_Running: Yes

            Slave_SQL_Running: Yes

              Replicate_Do_DB: homesite

          Replicate_Ignore_DB: mysql,information_schema,test

           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: 351

              Relay_Log_Space: 1634

              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: 0

Master_SSL_Verify_Server_Cert: No

                Last_IO_Errno: 0

                Last_IO_Error: 

               Last_SQL_Errno: 0

               Last_SQL_Error: 

1 row in set (0.00 sec)


后缀为error的字段的值为0说明配置成功,如果不为0会报其它错,具体原因查看日志,/var/log/mysqld.log


10.验证,在主库插入一条数据,然后去从库查看,简单,不再说明。