Mysql主从复制

                                   Mysql主从复制mysql

1、    功能:

能够当作一种备份方式sql

用来实现读写分离,缓解单个数据库的压力数据库

2、    备份原理

Master 上提供binlog服务器

    slave    经过 I/O线程从 master拿取 binlog,并复制到slave的中继日志中 slave    经过 SQL线程从 slave的中继日志中读取binlog ,而后解析到slave socket

3、    简单部署实验:

实验环境:ide

 

主服务器:mysql_1172.16.114.15/24测试

 

从服务器:mysql_2172.16.114.16/24spa

 

1.     在主服务器上配置:

编辑mysql配置文件vi /etc/my.cnf线程

 

 [root@mysql_1 ~]# cat /etc/my.cnfrest

[mysqld]

datadir=/var/lib/mysql

socket=/var/lib/mysql/mysql.sock

user=mysql

# Disabling symbolic-links isrecommended to prevent assorted security risks

symbolic-links=0

server-id = 1

log-bin=loyu-bin   #添加这两行内容

[mysqld_safe]

log-error=/var/log/mysqld.log

pid-file=/var/run/mysqld/mysqld.pid

 

 

重启mysqld服务

 

[root@mysql_1 ~]# /etc/init.d/mysqld restart

 

建立受权用户loyu

 

[root@mysql-1 ~]# mysql

……………

mysql>show master status;

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

| File            | Position | Binlog_Do_DB |Binlog_Ignore_DB |

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

|loyu-bin.000003 |      106 |              |                  |

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

1 row in set (0.00 sec)

mysql> grant replication slave on *.* to 'loyu'@'172.16.114.%' identified by'123456';

Query OK, 0 rows affected (0.00sec)

 

受权后登陆从服务器测试链接,若是链接不上须要立刻检查问题,而不是继续作下去。否则也会失败

 

[root@mysql-2 ~]# mysql -u loyu -h 172.16.114.15 -p

Enterpassword:

Welcome to the MySQLmonitor.  Commands end with ; or \g.

Your MySQL connection id is 3

Server version: 5.1.73-log Sourcedistribution

 

Copyright (c) 2000, 2013, Oracleand/or its affiliates. All rights reserved.

 

Oracle is a registered trademarkof Oracle Corporation and/or its

affiliates. Other names may betrademarks of their respective

owners.

 

Type 'help;' or '\h' for help.Type '\c' to clear the current input statement.

mysql>

 

 

2.     在从服务器上配置:

编辑mysql配置文件vi /etc/my.cnf

 

[root@mysql-2 ~]# cat /etc/my.cnf

[mysqld]

datadir=/var/lib/mysql

socket=/var/lib/mysql/mysql.sock

user=mysql

# Disabling symbolic-links isrecommended to prevent assorted security risks

symbolic-links=0

server-id= 2   #添加这行内容

[mysqld_safe]

log-error=/var/log/mysqld.log

pid-file=/var/run/mysqld/mysqld.pid

 

重启mysqld服务

 

[root@mysql-2 ~]# /etc/init.d/mysqld restart

 

在从库上指定主库的链接信息,并开启同步进程

 

[root@mysql-2 ~]# mysql

…………………..

mysql> CHANGE MASTER TO MASTER_HOST='172.16.114.15', MASTER_PORT=3306,MASTER_USER='loyu', MASTER_PASSWORD='123456', MASTER_LOG_FILE='loyu-bin.000003', MASTER_LOG_POS=106;

mysql>start slave;

Query OK, 0 rows affected (0.00sec)

mysql> show slave status\G

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

               Slave_IO_State: Waiting formaster to send event

                  Master_Host: 172.16.114.15

                  Master_User: loyu

                  Master_Port: 3306

                Connect_Retry: 60

              Master_Log_File: loyu-bin.000003

          Read_Master_Log_Pos: 259

               Relay_Log_File:mysqld-relay-bin.000002

                Relay_Log_Pos: 403

        Relay_Master_Log_File: loyu-bin.000003

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

              Relay_Log_Space: 559

              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)

能够看到

Slave_IO_Running: Yes (主从I/O正确)        

Slave_SQL_Running: Yes(主从进程正确)

 

 

测试主从数据同步:在主服务器(172.16.114.15)上操做;

建立数据库,建立表

 

mysql>create database loyu;

QueryOK, 1 row affected (0.00 sec)

 

mysql>show databases;

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

|Database           |

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

| information_schema|

|loyu               |

|mysql              |

|test               |

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

4 rowsin set (0.00 sec)

 

mysql>use loyu;

Databasechanged

mysql>create table loyu(id int(3),name char(10));

QueryOK, 0 rows affected (0.03 sec)

 

mysql> insert into loyu values (001,'loyu_mysql');

QueryOK, 1 row affected (0.00 sec)

 

 

在从服务器中查看是否有loyu数据库和表

能够看到是成功复制过来了

wKioL1XOqWCwbO8-AALElfuHJ4E921.jpg

相关文章
相关标签/搜索