MYSQL主从同步+读写分离

环境:CentOs 6.5(最小化安装)

关闭防火墙或添加对应规则,否则slave不能连接master,会导致同步失败

Mysql1:192.168.0.231                 Mysql2:192.168.0.232                    Mysql3:192.168.0.233

在Mysql1新建同步用户和新建一个数据库yfshare,在数据库里面新建表插入数据,以作标记:

mysql> select * from mytable;
+------+------+------------+
| name | sex  | brithday   |
+------+------+------------+
| aaa  | 1    | 1993-08-15 |
+------+------+------------+
1 row in set (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> CREATE USER [email protected] IDENTIFIED BY ‘redhat‘;
Query OK, 0 rows affected (0.00 sec)
mysql> GRANT REPLICATION SLAVE ON *.* TO [email protected] IDENTIFIED BY ‘redhat‘;
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> FLUSH TABLES WITH READ LOCK;
Query OK, 0 rows affected (0.00 sec)
[[email protected] ~]# mysqldump -u root -predhat yfshare >/root/yfshare.sql
[[email protected] ~]# ls yfshare.sql
yfshare.sql
[[email protected] ~]# scp yfshare.sql [email protected]:/root/
mysql> unlock tables;
Query OK, 0 rows affected (0.00 sec)
[[email protected] ~]# scp yfshare.sql

在mysql2上测试登录下这个用户(如果登不上看下是否被防火墙挡了在看my.cnf文件):
看看my.cnf这两个地方:#skip-networking和#bind-address

[[email protected] ~]# mysql -h192.168.0.231 -u aaa -predhat --port 3306
mysql>

在mysql2上新建数据库yfshare

[[email protected] ~]# mysql -u root -predhat
mysql> create database yfshare;
Query OK, 1 row affected (0.00 sec)
mysql> use yfshare;
Database changed
mysql> source yfshare.sql

配置mysql1的my.cnf文件

[[email protected] ~]# head -n 42 /usr/local/mysql/etc/my.cnf |tail -n 4
server-id=1
log_bin=mysql-bin
binlog-do-db=yfshare
binlog-ignore-db=test,mysql
[[email protected] ~]# 
[[email protected] ~]# /etc/init.d/mysqld restart
Shutting down MySQL. SUCCESS!
Starting MySQL.. SUCCESS!
[[email protected] ~]

查看mysql1状态:

mysql> show master status;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000010 |     2625 | yfshare      | test,mysql       |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
mysql>

配置mysql2的my.cnf文件:

[[email protected] ~]# head -n 44 /usr/local/mysql/etc/my.cnf |tail -n 6
server-id=2
log_bin=mysql-bin
binlog-do-db=yfshare
binlog-ignore-db=test,mysql
log_slave_updates=1                           //开启级联功能
[[email protected] ~]#
[[email protected] ~]# /etc/init.d/mysqld restart
Shutting down MySQL. SUCCESS!
Starting MySQL.. SUCCESS!
[[email protected] ~]#

进入mysql2控制台

mysql> show variables like ‘%server_id%‘;
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id     | 2     |
+---------------+-------+
1 row in set (0.01 sec)
mysql>
mysql> slave stop;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> change master to master_host=‘192.168.0.231‘,master_user=‘aaa‘,master_password=‘redhat‘,master_log_file=‘mysql-bin.000010‘ ,master_log_pos=2625;
Query OK, 0 rows affected (0.04 sec)
mysql> slave start;
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.0.231
                  Master_User: aaa
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000010
          Read_Master_Log_Pos: 2625
               Relay_Log_File: localhost-relay-bin.000004
                Relay_Log_Pos: 649
        Relay_Master_Log_File: mysql-bin.000010
             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: 2625
              Relay_Log_Space: 809
              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)
mysql>

在mysql2上配置:

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> CREATE USER ‘bbb‘@‘192.168.0.232‘ IDENTIFIED BY ‘redhat‘;
Query OK, 0 rows affected (0.00 sec)
mysql> GRANT REPLICATION SLAVE ON *.* TO ‘bbb‘@‘192.168.0.232‘ IDENTIFIED BY ‘redhat‘;
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> FLUSH TABLES WITH READ LOCK;
Query OK, 0 rows affected (0.00 sec)
[[email protected] ~]# mysqldump -u root -predhat yfshare >/root/yfshare.sql
[[email protected] ~]# scp yfshare.sql [email protected]:/root/
mysql> unlock tables;
Query OK, 0 rows affected (0.00 sec)
mysql> show master status;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000029 |      503 | yfshare      | test,mysql       |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
mysql>

在mysql3上配置:

[[email protected] ~]# head -n 42 /usr/local/mysql/etc/my.cnf |tail -n 4

server-id=3

log_bin=mysql-bin

binlog-do-db=yfshare

binlog-ignore-db=test,mysql

[[email protected] ~]#

[[email protected] ~]# /etc/init.d/mysqld restart

Shutting down MySQL. SUCCESS!

Starting MySQL.. SUCCESS!

[[email protected] ~]#

在mysql3上测试登录下这个用户(如果登不上看下是否被防火墙挡了在看my.cnf文件):
看看my.cnf这两个地方:#skip-networking和#bind-address

[[email protected] ~]# mysql -h192.168.0.232 -u bbb -predhat --port 3306
mysql>
mysql> slave stop;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> change master to master_host=‘192.168.0.232‘,master_user=‘bbb‘,master_password=‘redhat‘,master_log_file=‘mysql-bin.000029‘ ,master_log_pos=503;
Query OK, 0 rows affected (0.04 sec)
mysql> slave start;
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.0.232
                  Master_User: bbb
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000029
          Read_Master_Log_Pos: 503
               Relay_Log_File: localhost-relay-bin.000006
                Relay_Log_Pos: 649
        Relay_Master_Log_File: mysql-bin.000029
             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: 503
              Relay_Log_Space: 809
              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: 2
1 row in set (0.00 sec)
mysql>

这样配置了后以后主服务器只要有新数据写入,从服务器就会自动同步

测试下:

在mysql2上不做任何数据操作:(mysql会自动同步)

mysq3也是:

Question:

Last_IO_Errno: 1593

Last_IO_Error: Fatal error: The slave I/O thread stops because master and slave have equal MySQL server ids; these ids must be different for replication to work (or the --replicate-same-server-id option must be used on slave but this does not always make sense; please check the manual before using it).

Last_SQL_Errno: 0

如果报这个错,是server-id与slave-id冲突了(通过这个命令查看show variables like ‘server_id‘;)

mysql> show variables like ‘server_id‘;

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

| Variable_name | Value |

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

| server_id     | 2     |

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

1 row in set (0.00 sec)

mysql>

在/usr/local/mysql/etc/my.cnf里面默认就定义了一个server-id=1把这个值改下就行了

搭建Mysql读写分离:(未测试)

关闭防火墙或添加对应规则,否则slave不能连接master,会导致同步失败

数据库Master主服务器:192.168.0.231

数据库从服务器:192.168.0.232

MySQL-Proxy调度服务器:192.168.0.233

先让mysql1与mysql2实现主从复制

[[email protected] ~]# yum -y install gcc gcc-c++ autoconf automake zlib ncurses-devel libtool flex pkgconfig libevent glib libxml
[[email protected] software]# tar -zxvf libmcrypt-2.5.7.tar.gz &>/dev/null
[[email protected] software]# cd libmcrypt-2.5.7
[[email protected] libmcrypt-2.5.7]# ./configure --prefix=/usr/local/libmcrypt &&make &&make install
[[email protected] software]# yum -y install readline readline-devel
[[email protected] software]# tar -zxvf lua-5.2.0.tar.gz &>/dev/null
[[email protected] software]# cd lua-5.2.0
[[email protected] lua-5.2.0]# make linux
[[email protected] lua-5.2.0]# make install
[[email protected] software]# tar -zxvf mysql-proxy-0.8.5-linux-el6-x86-64bit.tar.gz &>/dev/null
[[email protected] software]# mv mysql-proxy-0.8.5-linux-el6-x86-64bit /usr/local/mysql-proxy
[[email protected] software]# mkdir /usr/local/mysql-proxy/log -p
[[email protected] software]# touch /usr/local/mysql-proxy/log/mysql-proxy.log
[[email protected] software]# mkdir /usr/local/mysql-proxy/run -p
[[email protected] software]# touch /usr/local/mysql-proxy/run/mysql-proxy.pid
[[email protected] software]# /etc/init.d/mysql-proxy start
Starting mysql-proxy:
ok   
[[email protected] software]# netstat -tunlp|grep proxy
tcp        0      0 0.0.0.0:4040                0.0.0.0:*                   LISTEN      22325/mysql-proxy  
[[email protected] software]# ps -ef|grep mysql-proxy|grep -v grep
root     22325     1  0 09:13 ?        00:00:00 /usr/local/mysql-proxy/libexec/mysql-proxy --proxy-read-only-backend-addresses=192.168.0.232:3306 --proxy-backend-addresses=192.168.0.231:3306 --proxy-lua-script=/usr/local/mysql-proxy/share/doc/mysql-proxy/rw-splitting.lua --daemon --pid-file=/usr/local/mysql-proxy/run/mysql-proxy.pid --user=root --log-level=debug --log-file=/usr/local/mysql-proxy/log/mysql-proxy.log
[[email protected] software]#
=====脚本参数详解=====
PROXY_PATH=/usr/local/mysql-proxy/bin              //定义服务二进制文件路径
proxy-read-only-backend-addresses=192.168.0.232:3306        //指定只读mysql主机地址及端口
proxy-backend-addresses=192.168.0.231:3306               //指定mysql主机地址及端口
proxy-lua-script=/usr/local/mysql-proxy/share/doc/mysql-proxy/rw-splitting.lua"   //指定lua脚本,在这里,使用的是rw-splitting脚本,用于读写分离
$NICELEVEL $PROXY_PATH/mysql-proxy $PROXY_OPTIONS
--daemon                    //以守护进程方式启动
--pid-file=$PROXY_PID       //指定pid文件
--user=root                 //以root方式运行
--log-level=debug           //定义log日志级别,由高到低分别有(error|warning|info|message|debug)
--log-file=/usr/local/mysql-proxy/log/mysql-proxy.log          //定义log日志文件路径
在mysql-proxy上登录测试下:
mysql> show processlist;
+----+------+-----------+------+---------+------+-------+------------------+
| Id | User | Host      | db   | Command | Time | State | Info             |
+----+------+-----------+------+---------+------+-------+------------------+
|  3 | root | localhost | NULL | Query   |    0 | NULL  | show processlist |
+----+------+-----------+------+---------+------+-------+------------------+
1 row in set (0.00 sec)
mysql>

安装测试工具sysbench测试mysql性能:

[[email protected] software]# tar -zxvf sysbench-0.4.10.tar.gz &>/dev/null
[[email protected] software]# cd sysbench-0.4.10
[[email protected] sysbench-0.4.10]# head -n 78 configure.ac |tail -n 6|grep -v ^$
# Checks for programs.
AC_PROG_CC
#AC_PROG_LIBTOOL
AC_PROG_RANLIB
AX_CHECK_DOCBOOK
[[email protected] sysbench-0.4.10]# ./autogen.sh
[[email protected] sysbench-0.4.10]# ./configure --with-mysql-includes=/usr/local/mysql/include --with-mysql-libs=/usr/local/mysql/lib
[[email protected] sysbench-0.4.10]# make &&make install
[[email protected] ~]# ln -s /usr/local/bin/sysbench /bin/
[[email protected] ~]# ln -s /usr/local/mysql/lib/libmysqlclient.so.18 /usr/lib64/
[[email protected] ~]# sysbench --test=oltp --mysql-table-engine=innodb --oltp-table-size=10000 --mysql-socket=/usr/local/mysql/mysql.sock --mysql-user=root --mysql-password=redhat --mysql-db=testsysbench prepare
sysbench 0.4.10:  multi-threaded system evaluation benchmark
No DB drivers specified, using mysql
Creating table ‘sbtest‘...
Creating 10000 records in table ‘sbtest‘...
[[email protected] ~]# sysbench --test=oltp --mysql-table-engine=innodb --oltp-table-size=10000 --mysql-socket=/usr/local/mysql/mysql.sock --mysql-user=root --mysql-password=redhat --mysql-db=testsysbench run
sysbench 0.4.10:  multi-threaded system evaluation benchmark
No DB drivers specified, using mysql
WARNING: Preparing of "BEGIN" is unsupported, using emulation
Running the test with following options:
Number of threads: 1
Doing OLTP test.
Running mixed OLTP test
Using Special distribution (12 iterations,  1 pct of values are returned in 75 pct cases)
Using "BEGIN" for starting transactions
Using auto_inc on the id column
Maximum number of requests for OLTP test is limited to 10000
Threads started!
Done.
OLTP test statistics:
    queries performed:
        read:                            140000
        write:                           50000
        other:                           20000
        total:                           210000
    transactions:                        10000  (110.69 per sec.)
    deadlocks:                           0      (0.00 per sec.)
    read/write requests:                 190000 (2103.17 per sec.)
    other operations:                    20000  (221.39 per sec.)
Test execution summary:
    total time:                          90.3398s
    total number of events:              10000
    total time taken by event execution: 90.2339
    per-request statistics:
         min:                                  7.30ms
         avg:                                  9.02ms
         max:                                 94.94ms
         approx.  95 percentile:               9.73ms
Threads fairness:
    events (avg/stddev):           10000.0000/0.00
    execution time (avg/stddev):   90.2339/0.00
[[email protected] ~]#
时间: 2024-10-11 20:59:25

MYSQL主从同步+读写分离的相关文章

MySQL主从同步读写分离的集群配置

大型网站为了解决大量的高并发访问问题,除了在网站实现分布式负载均衡,远远不够.到了数据业务层.数据访问层,如果还是传统的数据结构,或者只是单单靠一台服务器支持,如此多的数据库连接操作,服务器性能再好数据库必然会崩溃.数据丢失的话,后果更是不堪设想.这时候,我们会考虑如何减少数据库的连接,一方面采用优秀的代码框架,进行代码的优化,采用优秀的数据缓存技术如:memcached等.如果资金充足的话,必然会想到假设服务器集群,来分担主数据库的压力.或者在硬件设备上,投入大量资金,购买高性能的服务器.出名

使用amoeba配置mysql主从同步读写分离

实验环境:需要三台手工编译安装的mysql,一台主,两台从主: 192.168.88.100从1: 192.168.88.103从2: 192.168.88.104前段服务器: 192.168.88.101 -----在主服务器192.168.88.100上做---------------配置NTP服务器---------- //在主服务器上搭ntp服务,提供时间同步 yum install -y ntp vi /etc/ntp.conf restrict 192.168.88.0 mask 2

mysql主从同步读写分离

https://pan.baidu.com/s/1tm_FQ4C8heQqzx01URr85A //软件连接百度网盘 三台mysql数据库:主数据库服务器:192.168.80.100 从数据库服务器1:192.168.80.101 从数据库服务器2:192.168.80.102 一台amoeba调度器:192.168.80.103 一台mariadb客户端: 192.168.80.104 注:五台虚拟机第一步都写以下两行 systemctl stop firewalld //关闭防火墙 set

mysql主从同步读写分离配置

#mysql主库配置添加: #缓存用户表及索引数据的最主要缓存空间,一般建议50%~80%,但应该以mysql服务器实际情况来定,如果跑着其他 #的应用服务或者有MyISAM表,则需要先减去系统+应用+MyISAM引擎需要的内存,再得出innodb_buffer_pool_size的 #实际值,具体情况具体分析. innodb_buffer_pool_size = 4096M #打开bin-log日志,以供slave机器读取同步日志. log-bin=/mnt/mysql/log/mysql-b

mysql 主从同步-读写分离

一.       实验环境 Master                   centos 7.3              192.168.138.13 Slave                     centos 7.3               192.168.138.14 二.在master操作 安装并配置文件 [[email protected] ~]# yum install mariadb-server -y [[email protected] ~]# cat /etc/m

搭建Mysql-proxy实现主从同步读写分离

代理服务器 Wg61 192.168.0.180 (Mysql-proxy)主服务器 Wg62 192.168.0.142 (负责写入数据)从服务器 Wg63 192.168.0.156 (负责只读数据) 实验思路: 下载Mysql-proxy,在代理服务器Wg61上安装lua语言 Wg61安装proxy,添加/etc/profile的环境变量参数 修改proxy配置文件参数,测试读写分离 搭建主从服务器,创建测试数据库表并授权用户访问权限 启动Mysql-proxy测试读写分离 测试从服务器挂

Mysql主从配置+读写分离(转)

   MySQL从5.5版本开始,通过./configure进行编译配置方式已经被取消,取而代之的是cmake工具.因此,我们首先要在系统中源码编译安装cmake工具. 注:安装前须查看是否已经安装了如下依赖包,如果没有请安装. apt-get -y install gcc g++ libncurses5-dev ncurses-devel openssl 一.主库安装及配置 1.源码安装cmake # tar xf cmake-3.0.0.tar.gz # cd cmake-3.0.0 # .

Mysql主从配置+读写分离

Mysql主从配置+读写分离     MySQL从5.5版本开始,通过./configure进行编译配置方式已经被取消,取而代之的是cmake工具.因此,我们首先要在系统中源码编译安装cmake工具. 注:安装前须查看是否已经安装了如下依赖包,如果没有请安装. apt-get -y install gcc g++ libncurses5-dev ncurses-devel openssl   一.主库安装及配置 1.源码安装cmake # tar xf cmake-3.0.0.tar.gz #

mysql主从及读写分离

主从同步 1 主配置 把237配置为主数据库服务器 授权用户从42数据库服务器连接自己的时候有拷贝数据的权限 grant   replication    slave  on  *.*    to   [email protected]"172.40.7.42"   identified by "123456"; vim /etc/my.cnf [mysqld] server_id=237 log-bin=master    #必须开启 :wq service  m