mysql数据库一主多从的搭建

搭建环境:mysql-z : 117.34.95.161:3306

                  mysql-c1:117.34.95.161:23306

                  mysql-c2:117.34.95.161:13306

                  均为CentOS 6.6、mysql5.6

一、安装数据库(略)

二、配置主数据库

   1、  编辑/etc/my.cnf 添加以下两行    

    server_id=1

    log_bin=mysql_bin

    binlog-do-db=maidong    #需要同步的数据库名,如果有多个数据库,可重复此参数,每个数据库一行

    binlog-ignore-db=mysql   #不同步mysql系统数据库

    (以上两行不加时为同步全部数据库)

    重启服务:service mysqld  restart 

   2、进入mysql命令控制台

   show variables like 'server_id';  #查看server_id是否是1

        mysql> show variables like 'server_id';  

+---------------+-------+

| Variable_name | Value |

+---------------+-------+

| server_id     | 1     |

+---------------+-------+

1 row in set (0.00 sec)

 

mysql> show master status;   #查看主服务器信息,记录file跟pos

+------------------+----------+--------------+------------------+-------------------+

| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |

+------------------+----------+--------------+------------------+-------------------+

| mysql_bin.000002 |      304 |              |                  |                   |

+------------------+----------+--------------+------------------+-------------------+

 

1 row in set (0.00 sec)

3、配置权限

     create database maidong;

     create user maidong identified by '123456';

     grant all privileges on maidong.* to maidong@'%' identified by '123456';

     grant all on *.* to 'maidong'@'10.57.96.54' identified by '111111' with grant option;   #授权用户maidong从10.57.96.54完全访问数据库,注意:这里的10.57.96.54是要连接数据库Web服务器IP

     grant replication slave on *.* to 'maidong'@'10.57.96.54' identified by '111111' with grant option;   #授权用户maidong只能从10.57.96.54这个IP访问主服务器117.34.95.161上面的数据库,并且只具有数据库备份的权限

     flush privileges;

4、把主数据库上的数据库导入从数据库(略 )

三、配置从数据库

1、编辑配置文件

    vim /etc/my.cnf   

    server-id=2   #设置服务器id,修改其值为2,表示为从数据库,与主数据库不同即可

    log-bin=mysql-bin  #启动MySQ二进制日志系统,注意:如果原来的配置文件中已经有这一行,就不用再添加了。

    replicate-do-db=maidong   #需要同步的数据库名,如果有多个数据库,可重复此参数,每个数据库一行

     replicate-ignore-db=mysql   #不同步mysql系统数据库

     read_only  #设置数据库只读

    service mysqld restart;

2、进入mysql命令行

    show variables like 'server_id';

           mysql> show variables like 'server_id';

           +---------------+-------+

            | Variable_name | Value |

           +---------------+-------+

            | server_id     | 2     |

            +---------------+-------+

           1 row in set (0.00 sec)

      stop slave;

      change master to master_host='117.34.95.161', master_port=3306, master_user='maidong', master_password='111111', master_log_file='mysql_bin.000002',master_log_pos=304;    #执行同步语句

      start slave;    #开启slave同步进程

 

show slave status\G;  #查看slave同步信息

mysql> show slave status\G;

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

               Slave_IO_State: Waiting for master to send event

                  Master_Host: 192.168.1.110

                  Master_User: wanmeng

                  Master_Port: 3306

                Connect_Retry: 60

              Master_Log_File: mysql_bin.000002

          Read_Master_Log_Pos: 304

               Relay_Log_File: mysql-c1-relay-bin.000002

                Relay_Log_Pos: 467

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

              Relay_Log_Space: 643

              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: 

  Replicate_Ignore_Server_Ids: 

             Master_Server_Id: 1

                  Master_UUID: e9a5acfc-ca98-11e5-b6a0-005056931c66

             Master_Info_File: /usr/local/mysql/data/master.info

                    SQL_Delay: 0

          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

                  Master_Bind: 

      Last_IO_Error_Timestamp: 

     Last_SQL_Error_Timestamp: 

               Master_SSL_Crl: 

           Master_SSL_Crlpath: 

           Retrieved_Gtid_Set: 

            Executed_Gtid_Set: 

                Auto_Position: 0

1 row in set (0.00 sec)

注意查看:

            Slave_IO_Running: Yes       #连接到主库,并读取主库的日志到本地,生成本地日志文件

            Slave_SQL_Running: Yes   #读取本地日志文件,并执行日志里的SQL命令。

            两个参数均为yes说明配置成功

另一台从数据库做同样操作,server_id不同即可

 

猜你喜欢

转载自u013120869.iteye.com/blog/2275796