mysql主从复制及双主结构

  1. 环境准备

14.04.1-Ubuntu x86_64

准备两台机器,ip分别为192.168.1.200192.168.1.201

分别为两台机器安装mysql

 

 2. 从http://dev.mysql.com/downloads/mysql/#downloads下载

                mysql-5.6.27-linux-glibc2.5-x86_64.tar.gz安装包

 

 3. 安装mysql依赖

apt-get install libaio1

 

 4. 安装mysql

安装目录为/usr/local

 4.1 添加用户和组

groupadd mysql

useradd –r –g mysql mysql

 

 4.2 解压mysql-5.6.27-linux-glibc2.5-x86_64.tar.gz

tar –zxvf mysql-5.6.27-linux-glibc2.5-x86_64.tar.gz –C /usr/local

 

 4.3 重命名文件

        cd /usr/local

 mv mysql-5.6.27-linux-glibc2.5-x86_64/ mysql

 

 4.4 进入 mysql 目录, 设置 mysql 目录的拥有者和所属的用户组

cd mysql

chown -R mysql .

chgrp -R mysql .

 

 4.5 执行mysql 安装脚本

scripts/mysql_install_db --user=mysql

 

 4.6 再次设置 mysql 目录的拥有者

chown -R root .

 

 4.7 设置 data 目录的拥有者

chown -R mysql data

 

 4.8 复制 mysql 配置文件到/etc目录

        cp support-files/my-default.cnf  /etc/my.cnf

 

 4.9 启动 mysql

bin/mysqld_safe --user=mysql &

 

 4.10 初始化 root 用户密码

bin/mysqladmin -u root password 'new_password'

 

 4.11 复制mysql.server 脚本

cp support-files/mysql.server /etc/init.d/mysql.server

   在/etc/init.d目录下面的都可以通过service来启动,因此,可以通过如下方式启动和停止:

        service mysql.server [status|start|stop]

 

 4.12 把 /usr/local/mysql/bin/mysql 命令加到用户命令中,这样就不用每次都加 mysql命令的路径

ln -s /usr/local/mysql/bin/mysql /usr/local/bin/mysql

 

 4.13 登录mysql

mysql –u root -p

 

  4.14 mysql用户配置

删除用户表中密码为空和user为空的

mysql> use mysql;

mysql> select host,user,password from user;

mysql> delete from user where password = '';

 

 4.15 设置允许远程登录

        mysql> update user set host = '%';

 

到此mysql安装及配置基本完成。

 

 5. 主从复制配置

  将192.168.1.200作为主节点,192.168.1.201作为从节点

    5.1 修改主节点配置

vim /etc/my.cnf

[mysqld]中增加

server-id=1

log-bin=mysql-bin 

  

  修改完后重启mysql:

        root@hadoop01:/usr/local/mysql# service mysql.server restart

 

  登录mysql查看是否修改成功

        mysql> show variables like 'server_id';

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

        | Variable_name | Value |

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

        | server_id          |    1     |

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

        1 row in set (0.00 sec)

         mysql>

 

    5.2. 修改从节点配置

vim /etc/my.cnf

[mysqld]中增加

server-id=2

log-bin=mysql-bin

 

  修改完后重启mysql:

        root@hadoop02:/usr/local/mysql# service mysql.server restart

 

  登录mysql查看是否修改成功

        mysql> show variables like 'server_id';

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

        | Variable_name | Value |

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

        | server_id          |     2    |

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

        1 row in set (0.00 sec)

         mysql>

 

    5.3. 配置从节点

        在主节点服务器上执行:show master status;

        mysql> show master status;

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

        |            File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |

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

        | mysql-bin.000002 |      120   |                         |                              |                                |

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

        1 row in set (0.00 sec)

         mysql>

  记录下FilePosition的值,在配置从服务器时需要用到。

 

  在从服务器上运行如下命令:

    change master to

    master_host='192.168.1.200',

    master_user='root',

    master_password='123456',

    master_log_file='mysql-bin.000001',

    master_log_pos=120;


    5.4. 启动slave

        在从节点服务器上执行:

mysql> start slave;

Query OK, 0 rows affected (0.00 sec)

mysql>

        查看启动状态:

        mysql> show slave status\G;

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

                       Slave_IO_State: Waiting for master to send event

                          Master_Host: 192.168.1.200

                          Master_User: root

                          Master_Port: 3306

                        Connect_Retry: 60

                      Master_Log_File: mysql-bin.000002

                  Read_Master_Log_Pos: 120

                       Relay_Log_File: hadoop02-relay-bin.000003

                        Relay_Log_Pos: 283

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

                      Relay_Log_Space: 622

                      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: f71240d8-8c63-11e5-a0fd-000c2969b5ff

                     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)

 

    5.5 测试

        在主节点上创建表并插入数据

        mysql> create database t1;

        Query OK, 1 row affected (0.08 sec)

         mysql> use t1;

        Database changed

        mysql> create table t_user(id int(10),name varchar(50));

        Query OK, 0 rows affected (0.09 sec)

         mysql> insert into t_user(id,name) values(1,'wwss');

        Query OK, 1 row affected (0.00 sec)

         mysql> select * from t_user;

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

        |      id   | name |

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

        |    1      | wwss |

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

        1 row in set (0.00 sec)

        mysql>

 

        在从节点查看是否同步了数据:

        mysql>

        mysql> show databases;

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

        | Database                |

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

        | information_schema |

        | mysql                         |

        | performance_schema |

        | t1                                |

        | test                             |

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

        5 rows in set (0.00 sec)

                 mysql> use t1;

        Reading table information for completion of table and column names

        You can turn off this feature to get a quicker startup with -A

        Database changed

        mysql> show tables;

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

        | Tables_in_t1  |

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

        | t_user            |

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

        1 row in set (0.00 sec)

         mysql> select * from t_user;

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

        | id       | name |

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

        |    1      | wwss |

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

        1 row in set (0.00 sec)

        mysql>

        数据同步成功。

 

        测试在从节点中插入数据,能否同步到主节点:

        mysql> insert into t_user(id,name) values(2,'zhangsan');

        Query OK, 1 row affected (0.01 sec)

         mysql> select * from t_user;

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

        |      id   | name     |

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

        |    1      | wwss     |

        |    2      | zhangsan |

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

        2 rows in set (0.00 sec)

        mysql>

         

  在主节点查询:

        mysql> select * from t_user;

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

        |     id   | name |

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

        |    1     | wwss |

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

        1 row in set (0.00 sec)

        mysql>

        因此在从节点插入的数据无法同步到主节点。

 

     6. mysql双主结构配置

在从节点服务器上执行:show master status;

mysql> show master status;

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

| File                      | Position   | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |

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

| mysql-bin.000001 |      344   |                         |                               |                              |

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

1 row in set (0.00 sec)

mysql>

 

    在主节点服务器上运行如下命令:

    change master to

    master_host='192.168.1.201',

    master_user='root',

    master_password='123456',

    master_log_file='mysql-bin.000001',

    master_log_pos=344;

 

  在主节点服务上执行:

mysql> start slave;

Query OK, 0 rows affected (0.08 sec)

mysql>

 

        查看状态:

        mysql> show slave status\G;

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

                       Slave_IO_State: Waiting for master to send event

                          Master_Host: 192.168.1.201

                          Master_User: root

                          Master_Port: 3306

                        Connect_Retry: 60

                      Master_Log_File: mysql-bin.000001

                  Read_Master_Log_Pos: 344

                       Relay_Log_File: hadoop01-relay-bin.000002

                        Relay_Log_Pos: 283

                Relay_Master_Log_File: mysql-bin.000001

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

                      Relay_Log_Space: 459

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

                          Master_UUID: debcdb40-8c66-11e5-a10f-000c298a4d2b

                     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)

 

        测试:

        在从节点上插入数据:

        mysql> insert into t_user(id,name) values(3,'test10');

        Query OK, 1 row affected (0.00 sec)

         mysql> select * from t_user;

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

        | id       | name     |

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

        |    1     | wwss     |

        |    2     | zhangsan |

        |    3      | test10   |

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

        3 rows in set (0.00 sec)

         mysql>

        在主节点上查看:

        mysql> select * from t_user;

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

        | id        | name   |

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

        |    1     | wwss   |

        |    3     | test10 |

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

        2 rows in set (0.00 sec)

        mysql>

至此mysql主从复制和双主结构配置完成。 



你可能感兴趣的:(mysql,mysql主从复制,mysql双主结构)