zoukankan      html  css  js  c++  java
  • 实战-Mysql主从复制

    前言:
    Mysql内建的复制功能是构建大型高性能应用程序的基础。由于目前mysql的高可用性架构MMM和MHA均建立在复制的基础之上,本文就mysql主从复制进行实战描述,希望对读者提供帮助。之前

    服务器规划:
    主库:192.168.2.153
    备库:192.168.2.154
    mysql版本使用5.6.26脚本编译安装,并指定数据目录。

    主从复制:
    1.创建复制账号:

    主库:
    mysql> grant replication slave on *.* to repl@'192.168.2.%' identified by 'slave';
    备库:
    mysql> grant replication slave on *.* to repl@'192.168.2.%' identified by 'slave';
    

    2.配置主库和备库:
    修改my.cnf:

    			主库:
    			log_bin = mysql-bin
    			server_id= 153
    			备库:
    			log_bin = mysql-bin
    			server_id = 154
    			relay_log = /mvtech/mysql/log/mysql-relay-bin
    			log_slave_updates = 1
    			read_only = 1(不知道应用场合 暂时不添加)
    

    3.启动复制

    		备库:
    			CHANGE MASTER TO MASTER_HOST='192.168.2.153',
    			MASTER_USER='repl',
    			MASTER_PASSWORD='slave',
    			MASTER_LOG_FILE='mysql-bin.000006',
    			MASTER_LOG_POS=0;
    		查看状态:show slave statusG
    			Slave_IO_Running: No
                            Slave_SQL_Running: No
    
    		启动备库
    			start slave
    		查看备库状态:show slave statusG
    			Slave_IO_Running: Yes
                            Slave_SQL_Running: Yes
    		查看备库线程:
    		show processlistG
    

    4.附主库和备库的my.cnf
    主库:

    [mysqld]
    sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES 
    server-id = 153
    skip-name-resolve
    #skip-networking
    back_log = 500
    max_connections = 1000
    max_connect_errors = 600
    open_files_limit = 65535
    table_open_cache = 384
    max_allowed_packet = 4M
    binlog_cache_size = 1M
    max_heap_table_size = 8M
    tmp_table_size = 16M
    read_buffer_size = 2M
    read_rnd_buffer_size = 16M
    sort_buffer_size = 16M
    join_buffer_size = 8M
    thread_cache_size = 8
    query_cache_size = 8M
    query_cache_limit = 2M
    key_buffer_size = 512M
    ft_min_word_len = 4
    transaction_isolation = REPEATABLE-READ
    log_bin = mysql-bin
    binlog_format = mixed
    expire_logs_days = 30
    log_error = /mvtech/mysql/log/mysql-error.log
    slow_query_log = 1
    long_query_time = 6
    performance_schema = 0
    explicit_defaults_for_timestamp
    #lower_case_table_names = 1
    skip-external-locking
    default-storage-engine = InnoDB
    innodb_file_per_table = 1
    innodb_open_files = 500
    innodb_buffer_pool_size = 64M
    innodb_write_io_threads = 4
    innodb_read_io_threads = 4
    innodb_thread_concurrency = 8
    innodb_purge_threads = 1
    innodb_flush_log_at_trx_commit = 2
    innodb_log_buffer_size = 2M
    innodb_log_file_size = 32M
    innodb_log_files_in_group = 3
    innodb_max_dirty_pages_pct = 90
    innodb_lock_wait_timeout = 120 
    bulk_insert_buffer_size = 8M
    myisam_sort_buffer_size = 64M
    myisam_max_sort_file_size = 10G
    myisam_repair_threads = 1
    interactive_timeout = 28800
    wait_timeout = 28800
    [mysqldump]
    quick
    max_allowed_packet = 16M
    [myisamchk]
    key_buffer_size = 8M
    sort_buffer_size = 8M
    read_buffer = 4M
    write_buffer = 4M
    

    备库:

    [mysqld]
    sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES 
    server-id = 154
    skip-name-resolve
    #skip-networking
    back_log = 500
    max_connections = 1000
    max_connect_errors = 600
    open_files_limit = 65535
    table_open_cache = 384
    max_allowed_packet = 4M
    binlog_cache_size = 1M
    max_heap_table_size = 8M
    tmp_table_size = 16M
    read_buffer_size = 2M
    read_rnd_buffer_size = 16M
    sort_buffer_size = 16M
    join_buffer_size = 8M
    thread_cache_size = 8
    query_cache_size = 8M
    query_cache_limit = 2M
    key_buffer_size = 512M
    ft_min_word_len = 4
    transaction_isolation = REPEATABLE-READ
    log_bin = mysql-bin
    #slave add
    relay_log = /mvtech/mysql/log/mysql-relay-bin
    read_only = 1
    log_slave_updates = 1
    skip_slave_start
    #slave add
    binlog_format = mixed
    expire_logs_days = 30
    log_error = /mvtech/mysql/log/mysql-error.log
    slow_query_log = 1
    long_query_time = 6
    performance_schema = 0
    explicit_defaults_for_timestamp
    #lower_case_table_names = 1
    skip-external-locking
    default-storage-engine = InnoDB
    innodb_file_per_table = 1
    innodb_open_files = 500
    innodb_buffer_pool_size = 64M
    innodb_write_io_threads = 4
    innodb_read_io_threads = 4
    innodb_thread_concurrency = 8
    innodb_purge_threads = 1
    innodb_flush_log_at_trx_commit = 2
    innodb_log_buffer_size = 2M
    innodb_log_file_size = 32M
    innodb_log_files_in_group = 3
    innodb_max_dirty_pages_pct = 90
    innodb_lock_wait_timeout = 120 
    bulk_insert_buffer_size = 8M
    myisam_sort_buffer_size = 64M
    myisam_max_sort_file_size = 10G
    myisam_repair_threads = 1
    interactive_timeout = 28800
    wait_timeout = 28800
    [mysqldump]
    quick
    max_allowed_packet = 16M
    [myisamchk]
    key_buffer_size = 8M
    sort_buffer_size = 8M
    read_buffer = 4M
    write_buffer = 4M
    

    5.测试主从复制
    在主库中创建新库,备库也有新库产生,往主库中导入大量数据时可通过show processlist G 查看执行操作,
    6.mysql手工切换主从

       	#主库:
       	show slave hosts;
       	#备库:
       	#查看状态
       	mysql> show slave statusG
       	#关闭IO线程
       	mysql> stop slave IO_THREAD;
       	mysql> stop slave;
       	mysql> reset master;
       	mysql> show slave statusG
       	mysql> reset slave all;
       	#此时丛库已恢复至单机状态。
       	mysql> show binary logs;
       	#主库操作:
       		CHANGE MASTER TO MASTER_HOST='192.168.2.154',
       		MASTER_USER='repl',
       		MASTER_PASSWORD='slave',
       		MASTER_LOG_FILE='mysql-bin.000003',
       		MASTER_LOG_POS=120;
    

    手动切换主从复制完毕。
    注:mysql user库中有空的用户建议删掉,因为有时切换时会出现不使用密码即可登录的现象。将空用户删掉后重启mysql即可

  • 相关阅读:
    阿里巴巴集团2014年校园招聘系统project师北京笔试题
    HDU 3104 Combination Lock(数学题)
    MYSQL数据迁移
    nginx配置文件中的location中文详解
    使用JQuery解析、处理JSON数据(应用在课程表)
    OpenResty+lua+GraphicsMagick生成缩略图
    解决使用base64解码太慢的问题,原因是根本就不应该使用此方法解决。
    Bringing up interface eth0: Determining if ip address 10.109.67.81 is already in use for device eth0...
    高效访问Internet-启用ISA Server的缓存
    在OpenResty中使用lua-zlib的方法
  • 原文地址:https://www.cnblogs.com/sdhzdtwhm/p/8026507.html
Copyright © 2011-2022 走看看