mysql主从分离搭建

MySQL主从复制原理

主服务器数据库的每次操作都会记录在其二进制文件mysql-bin.xxx(该文件可以在mysql目录下的data目录中看到)中,从服务器启动一个I/O线程使用专用账号登录到主服务器中读取该二进制文件,并将文件内容写入到自己本地的中继日志relay-log文件中,然后从服务器在启动一个SQL线程并根据中继日志中的内容执行SQL语句写入到同步的数据库中.
原理图如下:

mysql主从原理.jpg

前期准备:

关闭防火墙和selinux:

iptables -F #关闭防火墙
setenforce 0  #关闭selinux
getenforce    #查看selinux状态
vim /etc/selinux/config  #在配置文件中修改selinux
[root@localhost html]# vim /etc/selinux/config 


# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled   #改成disabled 
# SELINUXTYPE= can take one of three two values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected. 
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted

:wq

下载源码包:
在两台服务器上都下地好源码包,将源码包下载到/usr/local/src路径中
mysql源码包地址:
http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz

开发环境与编译工具:
在两台服务器上搭建好编译环境

yum -y groupinstall Development Tools
yum -y install make gcc-c++ cmake bison-devel ncurses-devel libaio libaio-devel perl-Data-Dumper net-tools 

1.安装mysql并配置:

要做主从复制首先要安装好mysql,两台服务器安装是一样,步骤如下:

查询并卸载默认mysql:

rpm -qa | grep mysql  #如果查询到mysql,就是用rpm -e命令来卸载了原来的mysql
rm -rf /etc/my.cnf    #删除原来mysql的配置文件

下载并解压:

cd /usr/local/src  #进入下载目录
wget  http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz

tar -zxvf mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz  #在下载目录中解压文件

进入下载目录并复制文件:
我们将mysql统一安装在/usr/localmysql目录中,方便管理.

mkdir /usr/local/mysql   #创建安装目录,方便以后管理
cp  -R ./mysql-5.6.35-linux-glibc2.5-x86_64/* /usr/local/mysql/  #复制解压文件的目录中

进入安装目录并安装:

cd /usr/local/mysql/
 mkdir /data   #创建数据目录
 useradd -s /sbin/nologin mysql  #创建mysql用户
 ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql  #初始化脚本

复制配置文件并配置:

cp support-files/my-default.cnf /etc/my.cnf  #配置文件
cp support-files/mysql.server /etc/init.d/mysql.server  #启动脚本
在vim /etc/my.cnf配置如下:
[root@localhost mysql]# vim /etc/my.cnf

# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL.

[mysqld]

# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M

# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin

# These are commonly set, remove the # and set as required.
# basedir = .....
# datadir = .....
datadir =/data/mysql   #添加这个数据存放路径
# port = .....
# server_id = .....
# socket = .....
socket = /tmp/mysql.sock   #还有这个,其他暂时不用动
...省略...

:wq

启动mysql并初始设置:

/etc/init.d/mysql.server start   #启动mysql
/usr/local/mysql/bin/mysql_secure_installation  #初始设置,包含root密码设置等等

检查服务是否启动:

[root@localhost src]#  netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      995/sshd            
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1350/master         
tcp6       0      0 :::22                   :::*                    LISTEN      995/sshd            
tcp6       0      0 ::1:25                  :::*                    LISTEN      1350/master         
tcp6       0      0 :::3306                 :::*                    LISTEN      15505/mysqld   

到这里两台mysql服务器已经搭建完成了,下面开始做主从复制配置.

2.mysql 主从复制:

主节点IP: 192.168.1.107
从节点IP: 192.168.1.110

主节点配置:
编译/etc/my.cnf文件的[mysqld]下加入如下配置

[root@localhost src]# vim /etc/my.cnf

# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html
# *** DO NOT EDIT THIS FILE. It's a template which will be copied to the
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL.

[mysqld]

# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M

# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin

server_id = 1   #主数据库端ID号
log-bin = mysql-bin    #开启二进制日志  
binlog-do-db = db   #需要复制的数据库名,如果复制多个数据库,重复设置这个选项即可   
auto_increment_offset = 1   #这个参数一般用在主主同步中,用来错开自增值, 防止键值冲突
auto_increment_increment = 1   #这个参数一般用在主主同步中,用来错开自增值, 防止键值冲突
log_bin_trust_function_creators = 1  #将函数复制到slave 


...省略...


:wq

重启mysql并配置用户密码:

#重启mysql
[root@localhost mysql]# /etc/init.d/mysql.server restart
Shutting down MySQL.. SUCCESS! 
Starting MySQL. SUCCESS! 

#登录mysql
[root@localhost mysql]# /usr/local/mysql/bin/mysql -uroot -p123456
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.35-log MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

#配置从服务器登录的用户密码,权限等
mysql> grant replication slave,replication client on *.* to 'slave'@'192.168.1.110' identified by 'slpass';
Query OK, 0 rows affected (0.00 sec)

#刷新权限
mysql>  FLUSH PRIVILEGES;

查看主节点状态:

mysql> show master status\G;
*************************** 1. row ***************************
             File: mysql-bin.000003
         Position: 430
     Binlog_Do_DB: 
 Binlog_Ignore_DB: 
Executed_Gtid_Set: 
1 row in set (0.00 sec)

ERROR: 
No query specified


从节点配置:
编译/etc/my.cnf文件的[mysqld]下加入如下配置

[root@localhost src]# vim /etc/my.cnf

# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html
# *** DO NOT EDIT THIS FILE. It's a template which will be copied to the
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL.

[mysqld]

# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M

server_id = 2   #从节点的ID号,不能和主节点的一样
relay_log = relay-log  #开启中继日志
read_only = ON  #只读开启,实现读写分离
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin

# These are commonly set, remove the # and set as required.
# basedir = .....
# datadir = .....
datadir = /data/mysql
...省略...

:wq

"/etc/my.cnf" 36L, 1224C                   
   

重启从节点mysql并配置:

#重启mysql
[root@localhost mysql]# /etc/init.d/mysql.server restart
Shutting down MySQL.. SUCCESS! 
Starting MySQL. SUCCESS! 

#登录从节点mysql
[root@localhost mysql]# /usr/local/mysql/bin/mysql -uroot -p123456
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.35 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

#设置复制的主节点数据库,pos位置
mysql> CHANGE MASTER TO MASTER_HOST='192.168.1.107',MASTER_USER='slave',MASTER_PASSWORD='slpass',MASTER_LOG_FILE='mysql-bin.000003',MASTER_LOG_POS=0;
Query OK, 0 rows affected, 2 warnings (0.01 sec)


查看从节点状态:

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: 
                  Master_Host: 192.168.1.107
                  Master_User: slave
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000003   #复制的二进制文件
          Read_Master_Log_Pos: 4  #从哪个pos位置开始复制
               Relay_Log_File: relay-log.000001
                Relay_Log_Pos: 4
        Relay_Master_Log_File: mysql-bin.000003
             Slave_IO_Running: No    #复制功能还没有启动
            Slave_SQL_Running: No   #复制功能还没有启动
              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: 4
              Relay_Log_Space: 120
              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: NULL
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 1236
                Last_IO_Error: Got fatal error 1236 from master when reading data from binary log: 'Could not find first log file name in binary log index file'
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 1
                  Master_UUID: 23562d2f-6c4d-11e8-9fbd-000c29f223ba
             Master_Info_File: /data/mysql/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: 
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 180610 00:35:58
     Last_SQL_Error_Timestamp: 
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: 
            Executed_Gtid_Set: 
                Auto_Position: 0
1 row in set (0.01 sec)

ERROR: 
No query specified

开启主从复制并查看状态:

#开启复制功能
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.1.107
                  Master_User: slave
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 430  #已经复制到430的pos了
               Relay_Log_File: relay-log.000002
                Relay_Log_Pos: 593
        Relay_Master_Log_File: mysql-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: 430
              Relay_Log_Space: 760
              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: 23562d2f-6c4d-11e8-9fbd-000c29f223ba
             Master_Info_File: /data/mysql/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)

ERROR: 
No query specified

看到 Slave_IO_Running: Yes 和Slave_SQL_Running: Yes 都是yes了,说明主从复制已经生效了,环境搭建完成.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,761评论 5 460
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,953评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,998评论 0 320
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,248评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,130评论 4 356
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,145评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,550评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,236评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,510评论 1 291
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,601评论 2 310
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,376评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,247评论 3 313
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,613评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,911评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,191评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,532评论 2 342
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,739评论 2 335

推荐阅读更多精彩内容