mysql主从同步分库分表同步

一、MySQL数据库的安装

创新互联建站-成都网站建设公司,专注成都做网站、网站建设、网站营销推广,域名申请,网页空间,网站托管维护有关企业网站制作方案、改版、费用等问题,请联系创新互联建站

分别在master 和slave上源码安装mysql数据库

1.1 安装相关包
1.1.1 cmake软件
cd /home/oldboy/tools/
tar xf cmake-2.8.8.tar.gz
cd cmake-2.8.8
./configure
#CMake has bootstrapped.  Now run gmake.
gmake
gmake install
cd ../

1.1.2 依赖包
yum install ncurses-devel -y

1.2 开始安装mysql
1.2.1 创建用户和组
groupadd mysql
useradd mysql -s /sbin/nologin -M -g mysql

1.2.2 解压编译MySQL
tar zxf mysql-5.5.32.tar.gz
cd mysql-5.5.32
cmake . -DCMAKE_INSTALL_PREFIX=/application/mysql/mysql-5.5.40 \
-DMYSQL_DATADIR=/application/mysql/mysql-5.5.40/data \
-DMYSQL_UNIX_ADDR=/application/mysql/mysql-5.5.40/tmp/mysql.sock \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DEXTRA_CHARSETS=gbk,gb2312,utf8,ascii \
-DENABLED_LOCAL_INFILE=ON \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_FEDERATED_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 \
-DWITHOUT_PARTITION_STORAGE_ENGINE=1 \
-DWITH_FAST_MUTEXES=1 \
-DWITH_ZLIB=bundled \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_READLINE=1 \
-DWITH_EMBEDDED_SERVER=1 \
-DWITH_DEBUG=0

#-- Build files have been written to: /home/oldboy/tools/mysql-5.5.32
提示,编译时可配置的选项很多,具体可参考结尾附录或官方文档:
make
#[100%] Built target my_safe_process
make install
ln -s /application/mysql-5.5.32/ /application/mysql
如果上述操作未出现错误,则MySQL5.5.32软件cmake方式的安装就算成功了。

1.3安装数据库成功后

1.3.1 修改mysql安装目录的权限,全部修改为mysql

chown -R mysql:mysql /application/mysql

1.3.2 初始化数据库

cd /application/mysql/scripts

[root@localhost scripts]# ./mysql_install_db --basedir=/application/mysql --datadir=/data/mysql --user=mysql

1.3.3 替换/etc/下的配置文件和数据库的启动文件

/application/mysql/support-files

cp -r my-small.cnf  /etc/my.cnf

cp -r mysql.server  /etc/init.d/mysql

1.4启动数据库

/etc/init.d/mysql  start

/etc/init.d/mysql  stop

/etc/init.d/mysql  restart

二、mysql数据库分库分表的同步

2.1master配置文件

log-bin=mysql-bin

binlog_format=row

server-id       = 15

binlog-do-db=sales

/etc/init.d/mysql  restart(重启数据库)

2.2slave配置文件

server-id       = 25

relay-log=relay-bin
read-only = 1
replicate-do-db = sales

replicate-do-db =user_info

replicate-ignore-db = information_schema
replicate-ignore-db = mysql
replicate-ignore-db = user_info

replicate-do-table =sales.story    // 所要同步的数据库的单个表

/etc/init.d/mysql  restart(重启数据库)

2.3在master上创建同步的用户
 grant replication client,replication slave on *.* to rep@'172.27.1.%' identified by 'password';

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

ERROR:
No query specified

2.4在slave上执行如下语句,同步到master

change master to master_host='172.27.1.12'(主库IP地址),master_user='rep',master_password='financial',master_log_file='mysql-bin.000003',master_log_pos=150;

start slave(启动从库)

stop slave(停止从库)

reset slave(重置从库)

启动数据库后查看slave的状态如下:

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.27.1.12
                  Master_User: rep
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 150
               Relay_Log_File: relay-bin.000002
                Relay_Log_Pos: 269
        Relay_Master_Log_File: mysql-bin.001603
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: sales
          Replicate_Ignore_DB: information_schema,mysql,user_info
           Replicate_Do_Table: sales.story
       Replicate_Ignore_Table:
      Replicate_Wild_Do_Table:
  Replicate_Wild_Ignore_Table:
                   Last_Errno: 0
                   Last_Error:
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 150
              Relay_Log_Space: 419
              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: 15
1 row in set (0.00 sec)

ERROR:
No query specified

上述表示数据库master and slave 已配置好,可以进行同库单个表的配置

测试:

1.分别在master和slave创建数据库sales(row模式下需要手动创建数据库)。

2.use sales库下,在master上创建表

CREATE TABLE user_info (
    PersonID int,
    LastName varchar(255),
    FirstName varchar(255),
    Address varchar(255),
    City varchar(255) 
);

3.在master上创建表

CREATE TABLE story (
    PersonID int,
    LastName varchar(255),
    FirstName varchar(255),
    Address varchar(255),
    City varchar(255) 
);

查看slave同步状态:

a. story表同步成功

b. user_info表同步不成功

则mysql数据库单库,单表同步成功。

如果只是单库同步,则在slave的配置文件中去掉:

replicate-do-table =sales.story    // 所要同步的数据库的单个表

在安装过程中发现从低版本往高版本同步没有问题,从高版本往低版本同步一直出现问题。

三、数据库5.7二进制版本

下载链接:
https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz
直接解压使用,授权,初始化命令变为:
 bin/mysqld --initialize --user=mysql --basedir=/application/mysql-5.7.18 --datadir=/application/mysql-5.7.18/data

注意:初始化会生成登录数据库的初始密码,要记得保存。


当前名称:mysql主从同步分库分表同步
本文地址:http://scyanting.com/article/pgpigp.html