MySQL主从配置

Posted 水杉

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL主从配置相关的知识,希望对你有一定的参考价值。

更多mysql复制的知识请看

MySQL官方文档

《MySQL性能调优与架构设计》

环境:

  • 主数据库master,本地win7,192.168.1.102
  • 从数据库slave,虚拟机CentOS,192.168.56.1

1. 修改主从数据库配置

修改master数据库

(my.ini的局部 )

[mysqld]
log-bin=mysql-bin   #[必须]启用二进制日志
server-id=1      #[必须]服务器唯一ID,默认是1,一般取IP最后一段
# binlog-do-db=testbbc   #[可选]指定需要同步的数据库

 

修改slave数据库

(my.cnf的局部)

log-bin=mysql-bin   #[不是必须]启用二进制日志
server-id=2      #[必须]服务器唯一ID,默认是1,一般取IP最后一段
# binlog-do-db=testbbc #[可选]//同步数据库

重启主从数据库

 

2. 配置主从数据库

登录master数据库,给slave数据库授权

mysql> grant replication slave on *.* to root@192.168.56.1 identified by root;
Query OK, 0 rows affected (0.00 sec)

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

mysql> select host,user,password from mysql.user;
+--------------+------+-------------------------------------------+
| host         | user | password                                  |
+--------------+------+-------------------------------------------+
| localhost    | root | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
| 127.0.0.1    | root | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
| ::1          | root | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
| %            | root | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
| 192.168.56.1 | root | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
+--------------+------+-------------------------------------------+
5 rows in set (0.00 sec)

查看master数据库的状态

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

# 这里需要记录 File 以及 Position 的值,在操作从服务器时会用到

 

配置slave服务器

# 执行同步SQL语句
mysql> change master to master_host=192.168.56.1,master_user=root,master_password=root,master_log_file=mysql-bin.000001,master_log_pos=333;
Query OK, 0 rows affected (0.06 sec)

# 启动同步进程
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)

# 主从同步检查
mysql> show slave status \G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.56.1
                  Master_User: root
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 333
               Relay_Log_File: Centos6-relay-bin.000002
                Relay_Log_Pos: 253
        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: 333
              Relay_Log_Space: 411
              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
1 row in set (0.00 sec)

# 可以看到:Slave_IO_Running | Slave_SQL_Running两个值都是YES,说明配置成功了

 

3. 主从数据库测试

主数据库创建数据库,并在这个库建表,插入一条记录

mysql> create database test_db;
Query OK, 1 row affected (0.00 sec)

mysql> use test_db;
Database changed

mysql> create table test_tb(id int, name varchar(20));
Query OK, 0 rows affected (0.06 sec)

mysql> insert into test_tb(id,name) values(1, aaaa);
Query OK, 1 row affected (0.00 sec)

 

分别查看主从数据库

# 主数据库
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
| test_db            |
| testbbc            |
+--------------------+
6 rows in set (0.17 sec)

mysql> select * from test_tb;
+------+------+
| id   | name |
+------+------+
|    1 | aaaa |
+------+------+
1 row in set (0.00 sec)

 

# 从数据库
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
| test_db            |
+--------------------+
5 rows in set (0.02 sec)

mysql> use test_db;
Database changed
mysql> select * from test_tb;
+------+------+
| id   | name |
+------+------+
|    1 | aaaa |
+------+------+
1 row in set (0.00 sec)

 

结果主从数据库都有test_db和test_tb表以及表数据,说明主从数据库配置成功!!!

 

以上是关于MySQL主从配置的主要内容,如果未能解决你的问题,请参考以下文章

Mysql主从配置,实现读写分离

mysql主从配置,读写分离

MySQL主从复制与读写分离配置及实操

Mysql主从配置,实现读写分离

centos 5.4中mysql主从同步配置方法

Mysql主从配置和跳过事务