Mysql Master-Slave配置实验说明文档

一、主机信息

mysql1(master) 192.168.100.11 rhel5.4_x86 mysql-5.6.12.tar.gz

mysql2(slave) 192.168.100.12 rhel5.4_x86 mysql-5.6.12.tar.gz


二、mysql1(master)主机

1.停止mysql服务

2.配置文件

安装mysql软件自动生成配置文件my.cnf

[root@mysql1 ~]# cd /usr/local/mysql/
[root@mysql1 mysql]# vi my.cnf

文件末尾添加

# read_rnd_buffer_size = 2M 

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES 

######### Add Start ########
server-id=1 ## 服务器编号
log_bin=mysql-bin.log ##日志名称
read-only=0  ## 数据库可以读写
binlog-do-db=test  ## 需要复制的数据库
binlog-ignore-db=mysql ## 排除不需要复制的数据库

######### Add End ########

3.启动mysql服务

4.建立复制和启动slave服务用户

mysql> GRANT REPLICATION SLAVE ON *.* TO 'rep'@'%' IDENTIFIED BY '123456'  WITH GRANT OPTION;
Query OK, 0 rows affected (0.06 sec)

5.刷新系统表

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

6.显示master状态

mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 |      414 | test         | mysql            |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
mysql>

三、mysql2(slave)主机

1.停止mysql服务

2.配置文件
安装mysql软件自动生成配置文件my.cnf
[root@mysql1 ~]# cd /usr/local/mysql/
[root@mysql1 mysql]# vi my.cnf

文件末尾添加

# read_rnd_buffer_size = 2M 

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES 

######### Add Start ########
server-id=2
######### Add End ########


3.启动mysql服务


4.执行同步SQL语句

mysql> change master to master_host='192.168.100.11',master_user='rep',master_password='123456',master_log_file='mysql-bin.000001',master_log_pos=414;
Query OK, 0 rows affected, 2 warnings (0.13 sec)

5.启动同步服务

mysql> start slave;
Query OK, 0 rows affected (0.01 sec)

6.显示同步服务状态

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.100.11
                  Master_User: rep
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 414
               Relay_Log_File: mysql2-relay-bin.000002
                Relay_Log_Pos: 283
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes

1 row in set (0.00 sec)

ERROR: 
No query specified
mysql>

红色字体为需要检查项

三、验证主从线程


1.mysql1(master)主机

slave进程启动后,执行以下命令,查看master主机日志发送线程

mysql> show processlist;
+----+------+----------------------+------+-------------+------+-----------------------------------------------------------------------+------------------+
| Id | User | Host                 | db   | Command     | Time | State                                                                 | Info             |
+----+------+----------------------+------+-------------+------+-----------------------------------------------------------------------+------------------+
|  1 | root | localhost            | test | Query       |    0 | init                                                                  | show processlist |
|  2 | rep  | 192.168.100.12:48981 | NULL | Binlog Dump |  718 | Master has sent all binlog to slave; waiting for binlog to be updated | NULL             |
+----+------+----------------------+------+-------------+------+-----------------------------------------------------------------------+------------------+
2 rows in set (0.00 sec)

mysql>


2.mysql2(slave)主机

检查同步进程,接收master bin-log和应用传递过来的replay-log两个相关线程

mysql> show processlist;
+----+-------------+-----------+------+---------+------+-----------------------------------------------------------------------------+------------------+
| Id | User        | Host      | db   | Command | Time | State                                                                       | Info             |
+----+-------------+-----------+------+---------+------+-----------------------------------------------------------------------------+------------------+
|  1 | root        | localhost | NULL | Query   |    0 | init                                                                        | show processlist |
|  2 | system user |           | NULL | Connect |  549 | Waiting for master to send event                                            | NULL             |
|  3 | system user |           | NULL | Connect |  549 | Slave has read all relay log; waiting for the slave I/O thread to update it | NULL             |
+----+-------------+-----------+------+---------+------+-----------------------------------------------------------------------------+------------------+
3 rows in set (0.00 sec)

mysql>


四、执行master-slave复制验证

1.mysql1(master)主机

mysql> use test
Database changed
mysql> show tables;
Empty set (0.00 sec)


mysql> CREATE TABLE tasks(Id INT primary key AUTO_INCREMENT,
    -> Title VARCHAR(20), 
    -> Done BOOL,
    -> Description VARCHAR(200)
    -> );
Query OK, 0 rows affected (0.29 sec)

mysql> CREATE TABLE users(Id int primary key AUTO_INCREMENT,
    -> UserName VARCHAR(20) not null, 
    -> Password VARCHAR(20) not null, 
    -> Role VARCHAR(20),
    -> Description VARCHAR(200)
    -> );
Query OK, 0 rows affected (0.02 sec)

mysql> insert into tasks values(1,"Buy groceries",0,"Milk, Cheese, Pizza, Fruit, Tylenol");
insert into tasks values(2,"Learn Python",0,"Need to find a good Python tutorial on the web");
insert into tasks values(3,"Flask",0,"Login,WTF,Mail");
Query OK, 1 row affected (0.09 sec)

mysql> insert into tasks values(2,"Learn Python",0,"Need to find a good Python tutorial on the web");
Query OK, 1 row affected (0.01 sec)

mysql> insert into tasks values(3,"Flask",0,"Login,WTF,Mail");
Query OK, 1 row affected (0.00 sec)

mysql> insert into tasks values(4,"MySQL",0,"InoDB");
Query OK, 1 row affected (0.00 sec)

mysql> 
mysql> insert into users values(1,"admin","admin","administrator","administrator");
Query OK, 1 row affected (0.00 sec)

mysql> insert into users values(2,"user","123456","User","User");
Query OK, 1 row affected (0.01 sec)

mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| tasks          |
| users          |
+----------------+
2 rows in set (0.00 sec)

mysql> select * from tasks;
+----+---------------+------+------------------------------------------------+
| Id | Title         | Done | Description                                    |
+----+---------------+------+------------------------------------------------+
|  1 | Buy groceries |    0 | Milk, Cheese, Pizza, Fruit, Tylenol            |
|  2 | Learn Python  |    0 | Need to find a good Python tutorial on the web |
|  3 | Flask         |    0 | Login,WTF,Mail                                 |
|  4 | MySQL         |    0 | InoDB                                          |
+----+---------------+------+------------------------------------------------+
4 rows in set (0.03 sec)

mysql> select * from users;
+----+----------+----------+---------------+---------------+
| Id | UserName | Password | Role          | Description   |
+----+----------+----------+---------------+---------------+
|  1 | admin    | admin    | administrator | administrator |
|  2 | user     | 123456   | User          | User          |
+----+----------+----------+---------------+---------------+
2 rows in set (0.00 sec)

mysql> 

2.mysql2(slave)主机

mysql> use test;
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_test |
+----------------+
| tasks          |
| users          |
+----------------+
2 rows in set (0.00 sec)

mysql> select * from tasks;
+----+---------------+------+------------------------------------------------+
| Id | Title         | Done | Description                                    |
+----+---------------+------+------------------------------------------------+
|  1 | Buy groceries |    0 | Milk, Cheese, Pizza, Fruit, Tylenol            |
|  2 | Learn Python  |    0 | Need to find a good Python tutorial on the web |
|  3 | Flask         |    0 | Login,WTF,Mail                                 |
|  4 | MySQL         |    0 | InoDB                                          |
+----+---------------+------+------------------------------------------------+
4 rows in set (0.06 sec)

mysql> select * from users;
+----+----------+----------+---------------+---------------+
| Id | UserName | Password | Role          | Description   |
+----+----------+----------+---------------+---------------+
|  1 | admin    | admin    | administrator | administrator |
|  2 | user     | 123456   | User          | User          |
+----+----------+----------+---------------+---------------+
2 rows in set (0.00 sec)

mysql>

 

五.附加信息

1.增加mysql root的远程访问

GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root'  WITH GRANT OPTION;

flush privileges; (刷新系统表)

2.远程访问

mysql -h 远程主机IP(名称) -u root -P 3306(端口) -p

 

你可能感兴趣的:(mysql,master,slave,主从复制)