zoukankan      html  css  js  c++  java
  • MySQL5.7 Replication主从复制配置教程

     最近配置mysql5.7主从复制的时候碰到了些问题,老老实实按老版本的步骤配置会有错误,后来自己查看了官方文档,才解决了问题,在这里总结一下5.7的配置步骤,

    大体步骤跟老版本的还是一样的,只是有一些小区别,下面进行具体介绍。

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

        系统环境:win7


    我只安装了mysql server,其他配套工具没安装。安装后发现根目录下有一个my-default.ini文件,请把该文件改名成my.ini, 打开这个文件,文件内容如下:

    # For advice on how to change settings please see
    # http://dev.mysql.com/doc/refman/5.7/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 = .....
    # Remove leading # to turn on a very important data integrity option: logging
    # changes to the binary log between backups.
    # bin log file name of the log to create
    # log_bin= .....
    # These are commonly set, remove the # and set as required.
    # basedir = .....
    # datadir = .....
    # port = .....
    
    # Remove leading # to set options mainly useful for reporting servers.
    # The server defaults are faster for transactions and fast SELECTs.
    # Adjust sizes as needed, experiment to find the optimal values.
    # join_buffer_size = 128M
    # sort_buffer_size = 2M
    # read_rnd_buffer_size = 2M 
    sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

    把如下几个属性改动一下,

    server_id = 101 注意这个101是唯一标识你这个mysql服务器的,一般取你电脑ip的最后几位,以便好区分。
    log_bin=mysql-bin 注意mysql-bin是一个存放二进制log文件的路径,我这里指定了一个mysql当前安装目录的mysql-bin文件夹
    port=3306     3306是你设定的端口

    改完之后在mysql command line client(mysql命令行客户端)执行如下两个命令中一个,初始化data目录

    在window系统:
    mysqld --initialize-insecure; 解释:初始化data目录的同时,会自动生成无密码的root用户,
    mysqld --initialize; 解释:初始化data目录的同时,会自动生成带随机密码的root用户,
    在 Unix 或者Unix相关,相似的系统:
    mysqld --initialize --user=mysql
    bin/mysqld --initialize-insecure --user=mysql

    如上操作完成后,需要用上面生成的用户名登录服务,如下:

    • 如果你用 --initialize 初始化data目录,请用如下命令登录服务

       mysql -u root -p
      执行上面命令,会提示输入密码,输入随机生成的密码即可。

      如果你不知道这个随机密码,请查看error log文件查找这个随机密码。

    • 如果用 --initialize-insecure 初始化data目录,请用root用登录,并不需要输入密码就可以登录,如下命令:

       mysql -u root --skip-password

    用没有密码的root用户登录后可以给这个用户设置密码,设置方法如下命令:

     ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password';

    另外,如果 mysqld 无法识别你的mysql安装目录和mysql数据存放目录,请手工指定:方法有二

    第一种:即在输入命令的时候多加两个参数--basedir和--datadir,具体如下

    mysqld --initialize --user=mysql
             --basedir=your mysql install dir e.g:/opt/mysql/mysql
            --datadir=your mysql data dir e.g:/opt/mysql/mysql/data

    第二种:在你的my.ini文件增加两个属性:

    basedir= you mysql install dir e.g:/opt/mysql/mysql
    datadir=you mysql data dir e.g:/opt/mysql/mysql/data

    详细请参考:http://dev.mysql.com/doc/refman/5.7/en/data-directory-initialization-mysqld.html

     配置完后你可以重启服务了,命令:mysqld restart --console;或者手动关掉 mysqld进程后执行命令:mysqld --console;。

    请用同样的方法设置 master和slave但是两者的端口要不同。

            至此,剩下的操作跟老版本一样了,剩下的操作请参考其他文章,例如[z]如何在一台windows主机上搭建mysql主从复制 这篇文件,从如下文字开始:

    3、在主库添加一个用户 repl 并指定replication权限
    create user 'repl'@'127.0.0.1' identified by 'asdf';
    
    GRANT REPLICATION SLAVE ON *.* TO 'repl'@'127.0.0.1'; -- --这里我指定数据库(test.*)时报错,而指定全库(*.*)时会成功。
    
    4、保持主从mysql的test数据库初始状态一致。
    
    .....................

    ===完====

    下面的附加信息也许你会用到,或者碰到这些问题:

    1、用 show variables like '%log_bin%';查看状态:

    配置失败的状态如下:

    +---------------------------------+-------+
    | Variable_name | Value |
    +---------------------------------+-------+
    | log_bin | OFF |
    | log_bin_basename | |
    | log_bin_index | |
    | log_bin_trust_function_creators | OFF |
    | log_bin_use_v1_row_events | OFF |
    | sql_log_bin | ON |
    +---------------------------------+-------+
    6 rows in set, 1 warning (0.01 sec)

    配置成功的类似是如下:

    +---------------------------------+---------------------------------------------
    -----------------+
    | Variable_name | Value
    |
    +---------------------------------+---------------------------------------------
    -----------------+
    | log_bin | ON
    |
    | log_bin_basename | C:Program FilesMySQLMySQL Server 5.7data
    mysql-bin |
    | log_bin_index | C:Program FilesMySQLMySQL Server 5.7data
    mysql-bin.index |
    | log_bin_trust_function_creators | OFF
    |
    | log_bin_use_v1_row_events | OFF
    |
    | sql_log_bin | ON
    |
    +---------------------------------+---------------------------------------------
    -----------------+
    6 rows in set, 1 warning (0.01 sec)

    2、如果执行mysqld --console,报类似如下错,手动停掉mysqld进程后再执行。

    2016-02-06T13:23:13.951203Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is
    deprecated. Please use --explicit_defaults_for_timestamp server option (see doc
    umentation for more details).
    2016-02-06T13:23:13.951203Z 0 [Warning] 'NO_ZERO_DATE', 'NO_ZERO_IN_DATE' and 'E
    RROR_FOR_DIVISION_BY_ZERO' sql modes should be used with strict mode. They will
    be merged with strict mode in a future release.
    2016-02-06T13:23:13.951203Z 0 [Warning] 'NO_AUTO_CREATE_USER' sql mode was not s
    et.
    2016-02-06T13:23:13.951203Z 0 [Warning] Insecure configuration for --secure-file
    -priv: Current value does not restrict location of generated files. Consider set
    ting it to a valid, non-empty path.
    2016-02-06T13:23:13.952203Z 0 [Note] mysqld (mysqld 5.7.10-log) starting as proc
    ess 38432 ...
    2016-02-06T13:23:13.976205Z 0 [Note] InnoDB: Mutexes and rw_locks use Windows in
    terlocked functions
    2016-02-06T13:23:13.976205Z 0 [Note] InnoDB: Uses event mutexes
    2016-02-06T13:23:13.977205Z 0 [Note] InnoDB: Memory barrier is not used
    2016-02-06T13:23:13.977205Z 0 [Note] InnoDB: Compressed tables use zlib 1.2.3
    2016-02-06T13:23:13.977205Z 0 [Note] InnoDB: Number of pools: 1
    2016-02-06T13:23:13.978205Z 0 [Note] InnoDB: Not using CPU crc32 instructions
    2016-02-06T13:23:14.005206Z 0 [Note] InnoDB: Initializing buffer pool, total siz
    e = 128M, instances = 1, chunk size = 128M
    2016-02-06T13:23:14.019207Z 0 [Note] InnoDB: Completed initialization of buffer
    pool
    2016-02-06T13:23:14.092211Z 0 [Note] InnoDB: Highest supported file format is Ba
    rracuda.
    2016-02-06T13:23:14.239220Z 0 [Note] InnoDB: Creating shared tablespace for temp
    orary tables
    2016-02-06T13:23:14.240220Z 0 [Note] InnoDB: Setting file '.ibtmp1' size to 12
    MB. Physically writing the file full; Please wait ...
    2016-02-06T13:23:14.450232Z 0 [Note] InnoDB: File '.ibtmp1' size is now 12 MB.
    2016-02-06T13:23:14.453232Z 0 [Note] InnoDB: 96 redo rollback segment(s) found.
    96 redo rollback segment(s) are active.
    2016-02-06T13:23:14.453232Z 0 [Note] InnoDB: 32 non-redo rollback segment(s) are
    active.
    2016-02-06T13:23:14.463233Z 0 [Note] InnoDB: 5.7.10 started; log sequence number
    1210265
    2016-02-06T13:23:14.464233Z 0 [Note] InnoDB: Loading buffer pool(s) from C:Prog
    ram FilesMySQLMySQL Server 5.7dataib_buffer_pool
    2016-02-06T13:23:14.464233Z 0 [Note] Plugin 'FEDERATED' is disabled.
    2016-02-06T13:23:14.464233Z 0 [Note] InnoDB: not started
    mysqld: Table 'mysql.plugin' doesn't exist
    2016-02-06T13:23:14.465233Z 0 [ERROR] Can't open the mysql.plugin table. Please
    run mysql_upgrade to create it.
    2016-02-06T13:23:14.478233Z 0 [Note] InnoDB: Buffer pool(s) load completed at 16
    0206 21:23:14
    2016-02-06T13:23:14.586240Z 0 [Warning] Gtid table is not ready to be used. Tabl
    e 'mysql.gtid_executed' cannot be opened.
    2016-02-06T13:23:14.609241Z 0 [Warning] Failed to set up SSL because of the foll
    owing SSL library error: SSL context is not usable without certificate and priva
    te key
    2016-02-06T13:23:14.609241Z 0 [Note] Server hostname (bind-address): '*'; port:
    3306
    2016-02-06T13:23:14.611241Z 0 [Note] IPv6 is available.
    2016-02-06T13:23:14.612241Z 0 [Note] - '::' resolves to '::';
    2016-02-06T13:23:14.612241Z 0 [Note] Server socket created on IP: '::'.
    2016-02-06T13:23:14.615241Z 0 [Warning] Failed to open optimizer cost constant t
    ables
    
    2016-02-06T13:23:14.617241Z 0 [ERROR] Fatal error: Can't open and lock privilege
    tables: Table 'mysql.user' doesn't exist
    2016-02-06T13:23:14.619242Z 0 [ERROR] Aborting
    
    2016-02-06T13:23:14.619242Z 0 [Note] Binlog end
    2016-02-06T13:23:14.640243Z 0 [Note] Shutting down plugin 'ngram'
    2016-02-06T13:23:14.640243Z 0 [Note] Shutting down plugin 'partition'
    2016-02-06T13:23:14.640243Z 0 [Note] Shutting down plugin 'BLACKHOLE'
    2016-02-06T13:23:14.641243Z 0 [Note] Shutting down plugin 'ARCHIVE'
    2016-02-06T13:23:14.641243Z 0 [Note] Shutting down plugin 'PERFORMANCE_SCHEMA'
    2016-02-06T13:23:14.641243Z 0 [Note] Shutting down plugin 'MRG_MYISAM'
    2016-02-06T13:23:14.641243Z 0 [Note] Shutting down plugin 'MyISAM'
    2016-02-06T13:23:14.641243Z 0 [Note] Shutting down plugin 'INNODB_SYS_VIRTUAL'
    2016-02-06T13:23:14.642243Z 0 [Note] Shutting down plugin 'INNODB_SYS_DATAFILES'
    
    2016-02-06T13:23:14.642243Z 0 [Note] Shutting down plugin 'INNODB_SYS_TABLESPACE
    S'
    2016-02-06T13:23:14.643243Z 0 [Note] Shutting down plugin 'INNODB_SYS_FOREIGN_CO
    LS'
    2016-02-06T13:23:14.643243Z 0 [Note] Shutting down plugin 'INNODB_SYS_FOREIGN'
    2016-02-06T13:23:14.643243Z 0 [Note] Shutting down plugin 'INNODB_SYS_FIELDS'
    2016-02-06T13:23:14.643243Z 0 [Note] Shutting down plugin 'INNODB_SYS_COLUMNS'
    2016-02-06T13:23:14.643243Z 0 [Note] Shutting down plugin 'INNODB_SYS_INDEXES'
    2016-02-06T13:23:14.644243Z 0 [Note] Shutting down plugin 'INNODB_SYS_TABLESTATS
    '
    2016-02-06T13:23:14.644243Z 0 [Note] Shutting down plugin 'INNODB_SYS_TABLES'
    2016-02-06T13:23:14.644243Z 0 [Note] Shutting down plugin 'INNODB_FT_INDEX_TABLE
    '
    2016-02-06T13:23:14.644243Z 0 [Note] Shutting down plugin 'INNODB_FT_INDEX_CACHE
    '
    2016-02-06T13:23:14.644243Z 0 [Note] Shutting down plugin 'INNODB_FT_CONFIG'
    2016-02-06T13:23:14.645243Z 0 [Note] Shutting down plugin 'INNODB_FT_BEING_DELET
    ED'
    2016-02-06T13:23:14.645243Z 0 [Note] Shutting down plugin 'INNODB_FT_DELETED'
    2016-02-06T13:23:14.645243Z 0 [Note] Shutting down plugin 'INNODB_FT_DEFAULT_STO
    PWORD'
    2016-02-06T13:23:14.645243Z 0 [Note] Shutting down plugin 'INNODB_METRICS'
    2016-02-06T13:23:14.645243Z 0 [Note] Shutting down plugin 'INNODB_TEMP_TABLE_INF
    O'
    2016-02-06T13:23:14.646243Z 0 [Note] Shutting down plugin 'INNODB_BUFFER_POOL_ST
    ATS'
    2016-02-06T13:23:14.646243Z 0 [Note] Shutting down plugin 'INNODB_BUFFER_PAGE_LR
    U'
    2016-02-06T13:23:14.646243Z 0 [Note] Shutting down plugin 'INNODB_BUFFER_PAGE'
    2016-02-06T13:23:14.646243Z 0 [Note] Shutting down plugin 'INNODB_CMP_PER_INDEX_
    RESET'
    2016-02-06T13:23:14.646243Z 0 [Note] Shutting down plugin 'INNODB_CMP_PER_INDEX'
    
    2016-02-06T13:23:14.647243Z 0 [Note] Shutting down plugin 'INNODB_CMPMEM_RESET'
    2016-02-06T13:23:14.647243Z 0 [Note] Shutting down plugin 'INNODB_CMPMEM'
    2016-02-06T13:23:14.647243Z 0 [Note] Shutting down plugin 'INNODB_CMP_RESET'
    2016-02-06T13:23:14.647243Z 0 [Note] Shutting down plugin 'INNODB_CMP'
    2016-02-06T13:23:14.647243Z 0 [Note] Shutting down plugin 'INNODB_LOCK_WAITS'
    2016-02-06T13:23:14.648243Z 0 [Note] Shutting down plugin 'INNODB_LOCKS'
    2016-02-06T13:23:14.648243Z 0 [Note] Shutting down plugin 'INNODB_TRX'
    2016-02-06T13:23:14.648243Z 0 [Note] Shutting down plugin 'InnoDB'
    2016-02-06T13:23:14.648243Z 0 [Note] InnoDB: FTS optimize thread exiting.
    2016-02-06T13:23:14.648243Z 0 [Note] InnoDB: Starting shutdown...
    2016-02-06T13:23:14.748249Z 0 [Note] InnoDB: Dumping buffer pool(s) to C:Progra
    m FilesMySQLMySQL Server 5.7dataib_buffer_pool
    2016-02-06T13:23:14.751249Z 0 [Note] InnoDB: Buffer pool(s) dump completed at 16
    0206 21:23:14
    2016-02-06T13:23:16.137328Z 0 [Note] InnoDB: Shutdown completed; log sequence nu
    mber 1210284
    2016-02-06T13:23:16.138328Z 0 [Note] InnoDB: Removed temporary tablespace data f
    ile: "ibtmp1"
    2016-02-06T13:23:16.138328Z 0 [Note] Shutting down plugin 'MEMORY'
    2016-02-06T13:23:16.138328Z 0 [Note] Shutting down plugin 'CSV'
    2016-02-06T13:23:16.139328Z 0 [Note] Shutting down plugin 'sha256_password'
    2016-02-06T13:23:16.139328Z 0 [Note] Shutting down plugin 'mysql_native_password
    '
    2016-02-06T13:23:16.139328Z 0 [Note] Shutting down plugin 'binlog'
    2016-02-06T13:23:16.139328Z 0 [Note] mysqld: Shutdown complete

     原创文章,转载请注明:http://www.cnblogs.com/langtianya/p/5185577.html 

    相关:

    centos下mysql集群配置例子

    centos下mysql集群配置例子

    最后简单介绍一下主从复制和读写分离

           1 主从复制,是用来建立一个和主数据库完全一样的数据库环境,称为从数据库;主数据库一般是实时的业务数据库,从数据库的作用和使用场合一般有几个: 一是作为后备数据库,主数据库服务器故障后,可切换到从数据库继续工作; 二是可在从数据库作备份、数据统计等工作,这样不影响主数据库的性能;

           2 读写分离,是指读与写分别使用不同的数据库,当然一般是在不同服务器上的;在同一台服务器上的读写环境,估计只是用来测试吧。 一般读写的数据库环境配置为,一个写入的数据库,一个或多个读的数据库,各个数据库分别位于不同的服务器上,充分利用服务器性能和数据库性能;当然,其中 会涉及到如何保证读写数据库的数据一致,这个就可以利用主从复制技术来完成。 一般应用场合为:业务吞吐量很大

    相关:MYSQL主主切换(主库宕机)

    mysql一主多从宕机从库切换主继续和从同步教程

  • 相关阅读:
    WorkerMan源码分析(resetStd方法,PHP中STDIN, STDOUT, STDERR的重定向)
    linux:nohup 不生成 nohup.out的方法
    PHP系统编程--PHP进程信号处理(转)
    shell脚本实例总结
    saltstack 迭代项目到客户端并结合jenkins自动发布多台服务器
    自动化运维工具 SaltStack 搭建
    coding利用Webhook实现Push代码后的jenkins自动构建
    基于jquery地图特效全国网点查看代码
    基于jquery判断浏览器版本过低代码
    EntityFramework Model有外键时,Json提示循环引用 解决方法
  • 原文地址:https://www.cnblogs.com/langtianya/p/5185577.html
Copyright © 2011-2022 走看看