zoukankan      html  css  js  c++  java
  • MySQL之Haproxy+Keepalived+MySQL高可用均衡负载部署 (网络摘抄)

    来源于:https://blog.csdn.net/weisong530624687/article/details/71536837?utm_source=blogxgwz3

    一、安装主从MySQL

    1. 按照Linux-rpm安装MySQL步骤完成MySQL安装;

    2. 修改启动的配置文件

    (1)复制/usr/share/mysql路径下的my-default.cnf文件或my-medium.cnf文件至/etc目录

    (2)修改配置文件:

    server-id = 91   #指定服务器ID

    thread_concurrency = 4 #

    log-bin = mysql-bin    #开启二进制日志

    skip-name-resolve

    binlog-ignore = mysql   #忽略mysql和information_schema

    binlog-ignore = information_schema

    binlog-do_db = integrator_db #同步的数据库名,不配置此项则默认同步所有数据库

    (3)在Master服务器上创建MySQL用户(授权复制用户)

    命令:grant replication slave on *.* to '用户名'@'slave IP地址' identified by '密码';

    (4)查看master状态

    命令:show master status;

    (5)同步骤(2)修改slave的配置文件,其中server-id要设置比master大

    (6)在slave添加master配置

    命令:change master to

    ->master_host='master主机IP',

    ->master_port='master mysql服务使用端口',

    ->master_user='master mysql远程用户名',

    ->master_password='master mysql远程密码',

    ->master_log_file='master二进制日志文件',    #show master status中File的值

    ->master_log_pos='master日志地址';         show master status中Position的值

    (7)主从同步测试

    命令:show slave statusG

    当Slave_IO_Running和Slave_SQL_Running均为Yes时,表示同步成功。

    二、安装Haproxy

     master和slave安装配置步骤相同。

    1、上传安装包、授权并解压;

    命令:chmod 755 安装包名称;

    tar -zxvf 安装包名称;

    如本次使用haproxy-1.4.8.tar.gz安装包。  

    2、编译、安装

    (1)查看系统内核,选择对应编译命令

    查看内核命令:uname -a

    (2)编译

    如果是32位系统,则命令为:make TARGET=linux26 ARCH=i386 PREFIX=编译路径

    如果是64位系统,则命令为:make TARGET=linux26 ARCH=x86_64PREFIX=编译路径

    假设本次系统为64位,安装路径为/var/local/haproxy,则命令为:make TARGET=linux26 ARCH=x86_64 PREFIX=/var/local/haproxy

    (3)安装

    命令:make install PREFIX=编译路径

    同步骤(2),命令为:make install PREFIX=/var/local/haproxy

    3、创建配置文件、日志目录

    命令:mkdir -p /var/local/haproxy/logs/var/local/haproxy/conf  /var/local/haproxy/sbin

    4、编辑配置文件

    命令:vim /var/local/haproxy/conf/haproxy.conf,配置文件内容:

    global    

        log         127.0.0.1 local2  

        chroot      /var/lib/haproxy    

        pidfile     /var/run/haproxy.pid   

        maxconn     4096   

        user        haproxy    

        group       haproxy    

        daemon

        # turn on stats unix socket

        stats socket /var/lib/haproxy/stats

    defaults     

        mode                    http    

        log                     global   

        option                  tcplog

        option                  dontlognull   

        option                  redispatch   

        retries                 3    

        timeout queue           1m

        timeout connect         10s   

        timeout client          1m    

        timeout server          1m    

        timeout check           10s

        maxconn                 4096

    listen mysql_proxy

            bind 0.0.0.0:3307

            mode tcp

            balance  source

            option mysql-check user root password 111111    #远程用户

            server mysqldb1 192.168.0.91:3306 weight 1  check  inter 1s rise 2 fall 2        #master mysql

            server mysqldb2 192.168.0.121:3306 weight 2  check  inter 1s rise 2 fall 2 backup         #slave mysql

    listen stats     #监控

           mode http

           bind 0.0.0.0:8888

           stats enable

           stats uri /dbs

           stats realm Global statistics

           stats auth admin:admin

    配置文件说明同均衡负载-haproxy配置文件说明

    5、haproxy启动脚本管理

        命令:vi /etc/init.d/haproxy,增加配置:

    #!/bin/bash

    #

    # chkconfig: 2345 85 15

    # description: HA-Proxy is a TCP/HTTP reverse proxy which is particularly suited

    #              for high availability environments.

    # processname: haproxy

    # config: /etc/haproxy.cfg

    # pidfile: /var/run/haproxy.pid

    # Source function library.

    if [ -f /etc/init.d/functions ]; then

      . /etc/init.d/functions

    elif [ -f /etc/rc.d/init.d/functions ] ; then

      . /etc/rc.d/init.d/functions

    else

      exit 0

    fi

    CONF_FILE="/etc/haproxy/haproxy.cfg"

    HAPROXY_BINARY="/usr/local/sbin/haproxy"

    PID_FILE="/var/run/haproxy.pid"

    # Source networking configuration.

    . /etc/sysconfig/network

    # Check that networking is up.

    [ ${NETWORKING} = "no" ] && exit 0

    [ -f ${CONF_FILE} ] || exit 1

    RETVAL=0

    start() {

      $HAPROXY_BINARY -c -q -f $CONF_FILE

      if [ $? -ne 0 ]; then

        echo "Errors found in configuration file."

        return 1

      fi

      echo -n "Starting HAproxy: "

      daemon $HAPROXY_BINARY -D -f $CONF_FILE -p $PID_FILE

      RETVAL=$?

      echo

      [ $RETVAL -eq 0 ] && touch /var/lock/subsys/haproxy

      return $RETVAL

    }

    stop() {

      echo -n "Shutting down HAproxy: "

      killproc haproxy -USR1

      RETVAL=$?

      echo

      [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/haproxy

      [ $RETVAL -eq 0 ] && rm -f $PID_FILE

      return $RETVAL

    }

    restart() {

      $HAPROXY_BINARY -c -q -f $CONF_FILE

      if [ $? -ne 0 ]; then

        echo "Errors found in configuration file, check it with 'haproxy check'."

        return 1

      fi

      stop

      start

    }

    check() {

      $HAPROXY_BINARY -c -q -V -f $CONF_FILE

    }

    rhstatus() {

      pid=$(pidof haproxy)

      if [ -z "$pid" ]; then

        echo "HAProxy is stopped."

        exit 3

      fi

      status haproxy

    }

    condrestart() {

      [ -e /var/lock/subsys/haproxy ] && restart || :

    }

    # See how we were called.

    case "$1" in

      start)

        start

        ;;

      stop)

        stop

        ;;

      restart)

        restart

        ;;

      reload)

        restart

        ;;

      condrestart)

        condrestart

        ;;

      status)

        rhstatus

        ;;

      check)

        check

        ;;

      *)

        echo $"Usage: haproxy {start|stop|restart|reload|condrestart|status|check}"

        RETVAL=1

    esac

    exit $RETVAL

         

    6 、开启系统日志文件支持

    (1)修改/etc/syslog.conf,命令:vim /etc/syslog.conf ,添加内容:

    local2.*   /var/local/haproxy/logs/haproxy.log     #注意:local数字 该值与haproxy.conf中的配置保持一致

    (2)修改/etc/sysconfig/syslog,命令:vim/etc/sysconfig/rsyslog,添加内容:

    SYSLOGD_OPTIONS=”-c 2 -r -m 0″          #注释:-c 2 使用兼容模式,默认是 -c 5,-r开启远程日志,-m 0标记时间戳。单位是分钟,为0时,表示禁用该功能

    (3)重启rsyslog服务,命令:/etc/init.d/rsyslog restart

    (4)重启haproxy服务,命令:haproxy -f /var/haproxy/conf/haproxy.con

    7、可打开网页查看haproxy监控mysql的情况

    URL:http://haproxy服务器地址:端口/路径

    如步骤4配置的URL地址为:http://192.168.0.121:8888/dbs,用户名/密码均为admin

    具体安装步骤可参考:http://www.poluoluo.com/server/201601/450417.html

    http://www.tuicool.com/articles/67ZFBr

    三、Keepalived安装配置

    1、上传安装包、授权并解压;

    命令:chmod 755 安装包名称;

    tar  -zxvf 安装包名称;

        如本次使用keepalived-1.2.12.tar.gz安装包。

    2、编译、安装

    命令:./configure

    make& make install

    3、准备配置文件

    命令: cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/

    cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/

    mkdir /etc/keepalived

    cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/

    cp /usr/local/sbin/keepalived /usr/sbin/

    4、修改配置文件

    (1)master配置文件:

    global_defs {
        notification_email {
            mona.lai@sinoservices.com   //receiver email
       }
       notification_email_fromkobe.lv@sinoservices.com   //sender email
       smtp_server smtp.sinoservices.com
       smtp_connect_timeout 30
       router_id Mysql_Breakdown_Notification  #the master and backup must have the same router id
    }
     
    vrrp_instance VI_1 {
        state MASTER
        interface eth0
        virtual_router_id 56  
        priority 101
        advert_int 1
        authentication {  
            auth_type PASS
            auth_pass 1111
        }
        virtual_ipaddress {
            192.168.0.101
        }
    }
     
    virtual_server 192.168.0.101 3307 {
         delay_loop 2   
         lb_algo wrr   
         lb_kind DR    
         persistence_timeout 60
         protocol TCP
         real_server 192.168.0.91 3307 {
         weight 3
         notify_down /opt/chk_haproxy.sh
         TCP_CHECK {
         connect_timeout 10    
         nb_get_retry 3      
         delay_before_retry 3   
         connect_port 3307
        }
      }
    }

    vrrp_script chk_http_port {
        script "/opt/chk_haproxy.sh"
        interval 2
        weight 2
    }

    track_script {
        chk_http_port
    }

    (2)backup配置文件

    global_defs {
        notification_email {
            mona.lai@sinoservices.com   //receiver email
       }
       notification_email_fromkobe.lv@sinoservices.com   //sender email
       smtp_server smtp.sinoservices.com
       smtp_connect_timeout 30
       router_id Mysql_Breakdown_Notification  #the master and backup must have the same router id
    }
     
    vrrp_instance VI_1 {
        state BACKUP
        interface eth0
        virtual_router_id 56  
        priority 99
        advert_int 1
        authentication {  
            auth_type PASS
            auth_pass 1111
        }
        virtual_ipaddress {
            192.168.0.101
        }
    }
     
    virtual_server 192.168.0.101 3307 {
         delay_loop 2   
         lb_algo wrr   
         lb_kind DR    
         persistence_timeout 60
         protocol TCP
         real_server 192.168.0.91 3307 {
         weight 3
         notify_down /opt/chk_haproxy.sh
         TCP_CHECK {
         connect_timeout 10    
         nb_get_retry 3      
         delay_before_retry 3   
         connect_port 3307
        }
      }
    }

    vrrp_script chk_http_port {
        script "/opt/chk_haproxy.sh"
        interval 2
        weight 2
    }

    track_script {
        chk_http_port
    }

    5、启动keepalived

    先后在主、从服务器上启动keepalived:,命令:/etc/init.d/keepalived start
    在主服务器上查看是否已经绑定了虚拟IP,命令: ip addr

    6、设置脚本,让keepalive监控haproxy的状态

    监控脚本:

    #!/bin/bash

    if [ $(ps -C haproxy --no-header | wc -l) -eq 0 ];then

      haproxy -f /etc/haproxy/haproxy.cfg

    fi

    sleep 3

    if [ $(ps -C haproxy --no-header | wc -l) -eq 0 ];then

      /etc/init.d/keepalived stop

    fi

    7、测试无误后,重启MySQL、Haproxy、Keepalived,顺序依次为MySQL、Haproxy、Keepalived

    启动命令:service mysql restart

    killall -9 haproxy   -> haproxy -f /var/haproxy/conf/haproxy.con

    /etc/init.d/keepalived restart

    四、MySQL双主(主主)架构方案

    在企业中,数据库高可用一直是企业的重中之重,中小企业很多都是使用mysql主从方案,一主多从,读写分离等,但是单主存在单点故障,从库切换成主库需要作改动。因此,如果是双主或者多主,就会增加mysql入口,增加高可用。不过多主需要考虑自增长ID问题,这个需要特别设置配置文件,比如双主,可以使用奇偶,总之,主之间设置自增长ID相互不冲突就能完美解决自增长ID冲突问题。

    主从同步复制原理

    在开始之前,我们先来了解主从同步复制原理。

    复制分成三步:

    1. master将改变记录到二进制日志(binary log)中(这些记录叫做二进制日志事件,binary log events);

    2. slave将master的binary log events拷贝到它的中继日志(relay log);

    3. slave重做中继日志中的事件,将改变反映它自己的数据。

    下图描述了这一过程:


    该过程的第一部分就是master记录二进制日志。在每个事务更新数据完成之前,master在二日志记录这些改变。MySQL将事务串行的写入二进制日志,即使事务中的语句都是交叉执行的。在事件写入二进制日志完成后,master通知存储引擎提交事务。

    下一步就是slave将master的binary log拷贝到它自己的中继日志。首先,slave开始一个工作线程——I/O线程。I/O线程在master上打开一个普通的连接,然后开始binlog dump process。Binlog dump process从master的二进制日志中读取事件,如果已经跟上master,它会睡眠并等待master产生新的事件。I/O线程将这些事件写入中继日志。

    SQL slave thread处理该过程的最后一步。SQL线程从中继日志读取事件,更新slave的数据,使其与master中的数据一致。只要该线程与I/O线程保持一致,中继日志通常会位于OS的缓存中,所以中继日志的开销很小。

    此外,在master中也有一个工作线程:和其它MySQL的连接一样,slave在master中打开一个连接也会使得master开始一个线程。

    MySQL5.6以前的版本复制过程有一个很重要的限制——复制在slave上是串行化的,也就是说master上的并行更新操作不能在slave上并行操作。MySQL5.6版本参数slave-parallel-workers=1表示启用多线程功能。

    MySQL5.6开始,增加了一个新特性,是加入了全局事务ID (GTID) 来强化数据库的主备一致性,故障恢复,以及容错能力。

    官方文档:http://dev.mysql.com/doc/refman/5.6/en/replication-gtids.html

    MySQL双主(主主)架构方案思路是:

    1.两台mysql都可读写,互为主备,默认只使用一台(masterA)负责数据的写入,另一台(masterB)备用;

    2.masterA是masterB的主库,masterB又是masterA的主库,它们互为主从;

    3.两台主库之间做高可用,可以采用keepalived等方案(使用VIP对外提供服务);

    4.所有提供服务的从服务器与masterB进行主从同步(双主多从);

    5.建议采用高可用策略的时候,masterA或masterB均不因宕机恢复后而抢占VIP(非抢占模式);

    这样做可以在一定程度上保证主库的高可用,在一台主库down掉之后,可以在极短的时间内切换到另一台主库上(尽可能减少主库宕机对业务造成的影响),减少了主从同步给线上主库带来的压力;

    但是也有几个不足的地方:

    1.masterB可能会一直处于空闲状态(可以用它当从库,负责部分查询);

    2.主库后面提供服务的从库要等masterB先同步完了数据后才能去masterB上去同步数据,这样可能会造成一定程度的同步延时;

    架构的简易图如下:


    主主环境(这里只介绍2台主的配置方案):

    1.CentOS 6.8 64位2台:masterA(192.168.10.11),masterB(192.168.10.12)

    2.官方Mysql5.6版本

    搭建过程:

    1.安装MySQL服务(建议源码安装)

    1.1 yum安装依赖包

    yum -yinstall make gcc gcc-c++  ncurses-devel bison openssl-devel

    1.2 添加MySQL所需要的用户和组

    groupadd -g 27 mysql

    adduser -u 27 -g mysql -s /sbin/nologin mysql

    1.3 下载MySQL源码包

    mkdir -p /data/packages/src

    cd /data/packages/wget http://distfiles.macports.org/cmake/cmake-3.2.3.tar.gzwget http://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.34.tar.gz

    1.4 创建mysql数据目录

    mkdir -p /usr/local/mysql/data

    1.5 解压编译安装cmake、MySQL

    cd /data/packages/srctar -zxvf ../cmake-3.2.3.tar.gz

    cd cmake-3.2.3/

    ./bootstrap

    gmakemake install

    cd ../tar xf  mysql-5.6.34.tar.gz

    cd mysql-5.6.34

    cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DSYSCONFDIR=/etc -DWITH_SSL=bundled -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MYISAM_STORAGE_ENGINE=1 -DMYSQL_TCP_PORT=3306 -DMYSQL_UNIX_ADDR=/tmp/mysql.sock -DMYSQL_DATADIR=/usr/local/mysql/datamake &&make install

    1.6 添加开机启动脚本

    cp support-files/mysql.server /etc/rc.d/init.d/mysqld

    1.7 添加masterA配置文件/etc/my.cnf

    [client]

    port = 3306

    socket = /tmp/mysql.sock

    [mysqld]

    basedir = /usr/local/mysql

    port = 3306

    socket = /tmp/mysql.sock

    datadir = /usr/local/mysql/data

    pid-file = /usr/local/mysql/data/mysql.pid

    log-error = /usr/local/mysql/data/mysql.err

    server-id = 1

    auto_increment_offset = 1

    auto_increment_increment = 2                                            #奇数ID

    log-bin = mysql-bin                                                     #打开二进制功能,MASTER主服务器必须打开此项

    binlog-format=ROW

    binlog-row-p_w_picpath=minimal

    log-slave-updates=true

    gtid-mode=on

    enforce-gtid-consistency=true

    master-info-repository=TABLE

    relay-log-info-repository=TABLEsync-master-info=1

    slave-parallel-workers=0

    sync_binlog=0

    binlog-checksum=CRC32

    master-verify-checksum=1

    slave-sql-verify-checksum=1

    binlog-rows-query-log_events=1

    #expire_logs_days=5

    max_binlog_size=1024M                                                   #binlog单文件最大值

    replicate-ignore-db = mysql                                             #忽略不同步主从的数据库

    replicate-ignore-db = information_schema

    replicate-ignore-db = performance_schema

    replicate-ignore-db = test

    replicate-ignore-db = zabbix

    max_connections = 3000

    max_connect_errors = 30

    skip-character-set-client-handshake                                     #忽略应用程序想要设置的其他字符集

    init-connect='SET NAMES utf8'                                           #连接时执行的SQL

    character-set-server=utf8                                               #服务端默认字符集

    wait_timeout=1800                                                       #请求的最大连接时间

    interactive_timeout=1800                                                #和上一参数同时修改才会生效

    sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES                     #sql模式

    max_allowed_packet = 10M

    bulk_insert_buffer_size = 8M

    query_cache_type = 1

    query_cache_size = 128M

    query_cache_limit = 4M

    key_buffer_size = 256M

    read_buffer_size = 16K

    skip-name-resolve

    slow_query_log=1

    long_query_time = 6

    slow_query_log_file=slow-query.log

    innodb_flush_log_at_trx_commit = 2

    innodb_log_buffer_size = 16M

    [mysql]

    no-auto-rehash

    [myisamchk]

    key_buffer_size = 20M

    sort_buffer_size = 20M

    read_buffer = 2M

    write_buffer = 2M

    [mysqlhotcopy]

    interactive-timeout

    [mysqldump]

    quick

    max_allowed_packet = 16M

    [mysqld_safe]

    1.8 特别参数说明

    log-slave-updates = true     #将复制事件写入binlog,一台服务器既做主库又做从库此选项必须要开启

    #masterA自增长ID

    auto_increment_offset = 1

    auto_increment_increment = 2                                            #奇数ID

    #masterB自增加ID

    auto_increment_offset = 2

    auto_increment_increment = 2                                            #偶数ID

    1.9 添加masterB配置文件/etc/my.cnf

    [client]

    port = 3306

    socket = /tmp/mysql.sock

    [mysqld]

    basedir = /usr/local/mysql

    port = 3306

    socket = /tmp/mysql.sock

    datadir = /usr/local/mysql/data

    pid-file = /usr/local/mysql/data/mysql.pid

    log-error = /usr/local/mysql/data/mysql.err

    server-id = 2

    auto_increment_offset = 2

    auto_increment_increment = 2                                            #偶数ID

    log-bin = mysql-bin                                                     #打开二进制功能,MASTER主服务器必须打开此项

    binlog-format=ROW

    binlog-row-p_w_picpath=minimal

    log-slave-updates=true

    gtid-mode=on

    enforce-gtid-consistency=true

    master-info-repository=TABLE

    relay-log-info-repository=TABLEsync-master-info=1

    slave-parallel-workers=0

    sync_binlog=0

    binlog-checksum=CRC32

    master-verify-checksum=1

    slave-sql-verify-checksum=1

    binlog-rows-query-log_events=1

    #expire_logs_days=5

    max_binlog_size=1024M                                                   #binlog单文件最大值

    replicate-ignore-db = mysql                                             #忽略不同步主从的数据库

    replicate-ignore-db = information_schema

    replicate-ignore-db = performance_schema

    replicate-ignore-db = test

    replicate-ignore-db = zabbix

    max_connections = 3000

    max_connect_errors = 30

    skip-character-set-client-handshake                                     #忽略应用程序想要设置的其他字符集

    init-connect='SET NAMES utf8'                                           #连接时执行的SQL

    character-set-server=utf8                                               #服务端默认字符集

    wait_timeout=1800                                                       #请求的最大连接时间

    interactive_timeout=1800                                                #和上一参数同时修改才会生效

    sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES                     #sql模式

    max_allowed_packet = 10M

    bulk_insert_buffer_size = 8M

    query_cache_type = 1

    query_cache_size = 128M

    query_cache_limit = 4M

    key_buffer_size = 256M

    read_buffer_size = 16K

    skip-name-resolve

    slow_query_log=1

    long_query_time = 6

    slow_query_log_file=slow-query.log

    innodb_flush_log_at_trx_commit = 2

    innodb_log_buffer_size = 16M

    [mysql]

    no-auto-rehash

    [myisamchk]

    key_buffer_size = 20M

    sort_buffer_size = 20M

    read_buffer = 2M

    write_buffer = 2M

    [mysqlhotcopy]

    interactive-timeout

    [mysqldump]

    quick

    max_allowed_packet = 16M

    [mysqld_safe]

    1.10 初始化MySQL

    cd /usr/local/mysql

    scripts/mysql_install_db --user=mysql

    1.11 为启动脚本赋予可执行权限并启动MySQL

    chmod +x /etc/rc.d/init.d/mysqld/etc/init.d/mysqld start

    2.配置主从同步

    2.1 添加主从同步账户

    masterA上:

    mysql> grant replication slaveon *.* to 'repl'@'192.168.10.12' identifiedby '123456';

    mysql> flushprivileges;

    masterB上:

    mysql> grant replication slaveon *.* to 'repl'@'192.168.10.11' identifiedby '123456';

    mysql> flushprivileges;

    2.2 查看主库的状态

    masterA上:


    mysql> show master status;

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

    | File             | Position| Binlog_Do_DB| Binlog_Ignore_DB| Executed_Gtid_Set|

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

    | mysql-bin.000003 |      120 |              |                  |                   |

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

    1 rowin set (0.00 sec)


    masterB上


    mysql> show master status;

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

    | File             | Position| Binlog_Do_DB| Binlog_Ignore_DB| Executed_Gtid_Set|

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

    | mysql-bin.000003 |      437 |              |                  |                   |

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

    1 rowin set (0.00 sec)


    2.3 配置同步信息:

    masterA上:


    mysql> change masterto master_host='192.168.10.12',master_port=3306,master_user='repl',master_password='123456',master_log_file='mysql-bin.000003',master_log_pos=437;

    mysql> start slave;

    mysql> show slave statusG;


     

    显示有如下状态则正常:

    Slave_IO_Running: Yes

    Slave_SQL_Running: Yes

    masterB上:

    #本人是测试环境,可以保证没数据写入,否则需要的步骤是:先masterA锁表-->masterA备份数据-->masterA解锁表-->masterB导入数据-->masterB设置主从-->查看主从


    mysql> change masterto master_host='192.168.10.11',master_port=3306,master_user='repl',master_password='123456',master_log_file='mysql-bin.000003',master_log_pos=120;

    start slave;

    mysql> show slave statusG;


    显示有如下状态则正常:

    Slave_IO_Running: Yes

    Slave_SQL_Running: Yes

    3.测试主从同步

    3.1 在masterA上创建一个数据库测试同步效果


    mysql> show databases;

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

    | Database           |

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

    | information_schema|

    | mysql              |

    | performance_schema|

    | test               |

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

    4 rowsin set (0.00 sec)

    mysql> create database test01;

    Query OK, 1 row affected (0.00 sec)

    mysql> show databases;

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

    | Database           |

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

    | information_schema|

    | mysql              |

    | performance_schema|

    | test               |

    | test01             |

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

    5 rowsin set (0.00 sec)

    mysql> quit

    Bye

    [root@masterA data]#


    3.2 到masterB查看是否已经同步创建数据库


    mysql> show databases;

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

    | Database           |

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

    | information_schema|

    | mysql              |

    | performance_schema|

    | test               |

    | test01             |

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

    5 rowsin set (0.00 sec)

    mysql> quit

    Bye

    [root@masterB data]#


    4.开启MySQL5.6的GTID功能

    masterA和masterB分别执行如下命令:


    mysql> stop slave;

    Query OK, 0 rows affected (0.00 sec)

    mysql> change masterto MASTER_AUTO_POSITION=1;

    Query OK, 0 rows affected (0.01 sec)

    mysql> start slave;

    Query OK, 0 rows affected (0.00 sec)


    5.遇到的问题

    一种主从报错折腾了我半天:

    Last_IO_Errno: 1236

    Last_IO_Error: Got fatal error 1236 from master when reading data from binary log: 'Could not open log file'

    后面修改主从同步相关参数,确认原因是my.cnf增加了如下参数:

    log-bin = mysql-bin

    relay-log = mysql-bin


    从正常主主同步时的二进制日志文件显示,有2套二进制日志。因此推断上面2个参数导致不能产生2套二进制文件,故导致二进制文件错乱和丢失。

    五、附录:

    MySQL忘记了root密码,使用skip-grant-tables方式启动后,用set password的方式修改root密码时遇到错误

    ERROR 1290 (HY000)

    1.以skip-grant-tables方式启动mysql

    C:Users>mysqld --skip-grant-tables
    130411 10:09:30 [Warning] '--default-character-set' is deprecated and will be removed in a future re
    lease. Please use '--character-set-server' instead.
    130411 10:09:30 [Warning] Changed limits: max_open_files: 2048  max_connections: 700  table_cache: 6
    69

     2.用root用户登录,不用输入密码

    C:Users>mysql -uroot
    Welcome to the MySQL monitor.  Commands end with ; or g.
    Your MySQL connection id is 1
    Server version: 5.1.54-community-log MySQL Community Server (GPL)

    Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
    This software comes with ABSOLUTELY NO WARRANTY. This is free software,
    and you are welcome to modify and redistribute it under the GPL v2 license

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

    3.用set password方式修改root密码遇到错误ERROR 1290 (HY000)

    mysql> set password for root@'localhost'=PASSWORD('12345');
    ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot exe
    cute this statement

    4. 用update方式修改root密码正常
    mysql> update mysql.user set password=password('12345') where user='root' and host='localhost';
    Query OK, 1 row affected (0.00 sec)
    Rows matched: 1  Changed: 1  Warnings: 0

    5.刷新权限

    mysql> flush privileges;
    Query OK, 0 rows affected (0.01 sec)

    看来以skip-grant-tables方式启动mysql后,不能用set password的方式修改root密码

  • 相关阅读:
    Read-Copy Update Implementation For Non-Cache-Coherent Systems
    10 华电内部文档搜索系统 search04
    10 华电内部文档搜索系统 search05
    lucene4
    10 华电内部文档搜索系统 search01
    01 lucene基础 北风网项目培训 Lucene实践课程 索引
    01 lucene基础 北风网项目培训 Lucene实践课程 系统架构
    01 lucene基础 北风网项目培训 Lucene实践课程 Lucene概述
    第五章 大数据平台与技术 第13讲 NoSQL数据库
    第五章 大数据平台与技术 第12讲 大数据处理平台Spark
  • 原文地址:https://www.cnblogs.com/EikiXu/p/9921465.html
Copyright © 2011-2022 走看看