码迷,mamicode.com
首页 > 数据库 > 详细

mysql 主从同步-读写分离

时间:2017-12-03 15:38:17      阅读:269      评论:0      收藏:0      [点我收藏+]

标签:relay   backup   emctl   mysqld   字符   ted   文件   art   roo   

一、       实验环境

Master                   centos 7.3              192.168.138.13

Slave                     centos 7.3               192.168.138.14

二、在master操作

  1. 安装并配置文件

[root@localhost ~]# yum install mariadb-server -y

[root@localhost ~]# cat /etc/my.cnf

[mysqld]

datadir=/var/lib/mysql

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

server-id=1                  //必须唯一

log-bin=mysql-bin          //开启binlog日志

character-set-server=utf8         //设置字符集

        2.启动mysql

[root@localhost ~]# systemctl start mariadb

        3. 授权root用户

MariaDB [(none)]> grant all on *.* to root@‘localhost‘ identified by ‘123456‘;

MariaDB [(none)]> grant all on *.* to root@‘%‘ identified by ‘123456‘;

         4. 刷新权限表

MariaDB [(none)]> flush privileges

5.查看master状态

MariaDB [(none)]> show master status;

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

| File                 | Position | Binlog_Do_DB | Binlog_Ignore_DB |

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

| mysql-bin.000003 |    521 |                   |                  |

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

           

注意:如果主库中已存在数据,则需要备份拷到从库保持数据一致性

1.锁定数据表,避免在备份过程中,表被更新

mysql>LOCK TABLES tbl_name READ;

为表增加一个写锁定:

mysql>LOCK TABLES tbl_name WRITE;

解锁 : mysql>UNLOCK TABLES;

2.备份数据

方法一

Mysqldump  -uroot  -p –B db1 –T tb1  | gzip  > /mysqlbackup/tb1.sql.gz

方法二

直接备份datadir=/var/lib/mysql

                                                                                        

三、       在slave上操作

  1. 安装并配置/etc/my,cnf

[root@localhost ~]# yum install mariadb-server -y

[root@localhost ~]# cat /etc/my.cnf

[mysqld]

datadir=/var/lib/mysql

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

server-id=1                  //必须唯一

character-set-server=utf8         //设置字符集

        2. 启动mysql

[root@localhost ~]# systemctl start mariadb

3. 配置同步参数

MariaDB [(none)]> change master to

 master_host=‘192.168.138.13‘,

master_user=‘root‘,

master_password=‘123456‘,

master_port=3306,

master_log_file=‘mysql-bin.000003‘,

master_log_pos=521;

4. 启动主从同步进程

          MariaDB [(none)]> start slave;

        5. 检查状态

MariaDB [(none)]> show slave status\G;

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

               Slave_IO_State: Waiting for master to send event

                  Master_Host: 192.168.138.13

                  Master_User: root

                  Master_Port: 3306

                Connect_Retry: 60

              Master_Log_File: mysql-bin.000003

          Read_Master_Log_Pos: 521

               Relay_Log_File: mariadb-relay-bin.000002

                Relay_Log_Pos: 529

        Relay_Master_Log_File: mysql-bin.000003

             Slave_IO_Running: Yes

            Slave_SQL_Running: Yes

看到两个yes说明配置成功

四、 测试

         1.在slave上查看

        MariaDB [(none)]> show databases;

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

| Database           |

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

| information_schema |

| mysql              |

| performance_schema |

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

3 rows in set (0.00 sec)

2. 在master创建数据库

MariaDB [(none)]> create database testdb1;

Query OK, 1 row affected (0.00 sec)

3.  在slave查看

MariaDB [(none)]> show databases;

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

| Database           |

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

| information_schema |

| mysql              |

| performance_schema |

| testdb1            |

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

4 rows in set (0.00 sec)

mysql 主从同步-读写分离

标签:relay   backup   emctl   mysqld   字符   ted   文件   art   roo   

原文地址:http://www.cnblogs.com/sxchengchen/p/7966046.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!