mysql主从服务搭建、主主服务搭建

mysql主从服务搭建

安装MySQL
以下配置基于安装时的配置

一. 主数据库

  1. 修改配置文件my.cnf

    auto_increment_increment=2
    auto_increment_offset=1
    character_set_server=utf8
    log-bin-trust-function-creators=1
    

    重要参数说明:
    log_bin:启动mysql二进制日志,如果没有配置这个将无法远程链接
    server-id:须保证每台mysql主机不重复
    auto_increment_increment:步进值auto_imcrement,一般有n台主MySQL就填n
    auto_increment_offset:设定数据库中自动增长的起点,两台mysql的起点必须不同,这样才能避免两台服务器同步时出现主键冲突

    完整示例配置,(同步所有数据库)

    [mysqld]
    user = mysql
    basedir = /usr/local/mysql
    datadir = /home/mysql/data
    port = 3306
    socket = /home/mysql/tmp/mysql.sock
    pid-file = /home/mysql/tmp/mysqld.pid
    tmpdir = /home/mysql/tmp
    skip_name_resolve = 1
    symbolic-links=0
    max_connections = 2000
    group_concat_max_len = 1024000
    sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
    lower_case_table_names = 1
    log_timestamps=SYSTEM
    character-set-server = utf8
    interactive_timeout = 2880000
    wait_timeout = 2880000
    max_allowed_packet = 1G
    binlog_cache_size = 32M
    sort_buffer_size = 16M
    read_buffer_size = 16M
    join_buffer_size = 16M
    tmp_table_size = 96M
    max_heap_table_size = 96M
    max_length_for_sort_data = 8096
    auto_increment_increment=2
    auto_increment_offset=1
    #logs
    server-id = 102
    log-error = /home/mysql/logs/error.log
    slow_query_log = 1
    slow_query_log_file = /data/mysql/logs/slow.log
    long_query_time = 3
    log-bin = /home/mysql/logs/binlog
    binlog_format = row
    expire_logs_days = 15
    sync_binlog = 1
    binlog_checksum = crc32
    binlog_format = mixed
    log_bin_trust_function_creators = 1
    relay-log = /home/mysql/logs/relay-bin
    relay-log-recovery = 1
    relay_log_purge = 1
    #innodb
    innodb_file_per_table = 1
    innodb_log_buffer_size = 16M
    innodb_log_file_size = 256M
    innodb_log_files_in_group = 2
    innodb_io_capacity = 2000
    innodb_io_capacity_max = 4000
    innodb_flush_neighbors = 0
    innodb_flush_method = O_DIRECT
    innodb_autoinc_lock_mode = 2
    innodb_read_io_threads = 8
    innodb_write_io_threads = 8
    innodb_buffer_pool_size = 2G
    [client]
    default-character-set=utf8
    [mysql]
    default-character-set=utf8
    
  2. 重启数据库并创建从节点用户和授权

    # 重启mysql
    systemctl restart mysql
    #登录mysql
    mysql -u root -p
    #创建用户
    grant replication slave, replication client on *.* to '103'@'10.42.11.103' identified by '123';
    #刷新
    flush privileges;
    #查看日志(记录下来,需要用到)
    show master status;
    

    mysql主从服务搭建、主主服务搭建_第1张图片

二、 从数据库

  1. 修改配置文件my.cnf
    配置和主数据库的配置相同就行了,注意部分参数需要保证是唯一的

  2. 设置同步节点

    change master to master_host='10.42.11.102', master_port=3306, master_user='103', master_password='123', master_log_file='binlog.000005', master_log_pos=621;
    

    这里master_user就是上面主数据库创建的用户,master_log_file和master_log_pos就是上面日志中的file和position

  3. 启动同步

    start slave;
    
  4. 查看同步状态

    show slave status\G;
    

    mysql主从服务搭建、主主服务搭建_第2张图片

三、 MySQL主主服务搭建

  1. 在从服务器上执行上面的主数据库的第2步
  2. 在主数据库上执行上面从数据的2、3步

你可能感兴趣的:(数据库,mysql,mysql,数据库,database)