zoukankan      html  css  js  c++  java
  • mysql主从搭建

    mysql初始化
    /usr/local/mysql/scripts/mysql_install_db  --basedir=/usr/local/mysql --defaults-file=/etc/my3306.cnf --datadir=/home/mysql3306/mysql3306/ --user=mysql

    /usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my3306.cnf --user=mysql &
    /usr/local/mysql/bin/mysql -S /tmp/mysql3306.sock
    主库,从库打开binlog(修改binlog需要重新启动数据库)
    mysql> show variables like '%binlog%';
    +-----------------------------------------+----------------------+
    | Variable_name                           | Value                |
    +-----------------------------------------+----------------------+
    | binlog_cache_size                       | 4194304              |
    | binlog_checksum                         | CRC32                |
    | binlog_direct_non_transactional_updates | OFF                  |
    | binlog_error_action                     | IGNORE_ERROR         |
    | binlog_format                           | ROW                  |
    | binlog_gtid_simple_recovery             | OFF                  |
    | binlog_max_flush_queue_time             | 0                    |
    | binlog_order_commits                    | ON                   |
    | binlog_row_image                        | FULL                 |
    | binlog_rows_query_log_events            | OFF                  |
    | binlog_stmt_cache_size                  | 32768                |
    | binlogging_impossible_mode              | IGNORE_ERROR         |
    | innodb_api_enable_binlog                | OFF                  |
    | innodb_locks_unsafe_for_binlog          | OFF                  |
    | max_binlog_cache_size                   | 8388608              |
    | max_binlog_size                         | 1073741824           |
    | max_binlog_stmt_cache_size              | 18446744073709547520 |
    | simplified_binlog_gtid_recovery         | OFF                  |
    | sync_binlog                             | 1                    |
    +-----------------------------------------+----------------------+
    19 rows in set (0.00 sec)
    mysql>
    主库:
    mysql> show databases;清除多余的数据库
    drop database ----
    mysql> show master status;
    +------------------+----------+--------------+------------------+-------------------+
    | File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
    +------------------+----------+--------------+------------------+-------------------+
    | mysql-bin.000001 |     1196 |              |                  |                   |
    +------------------+----------+--------------+------------------+-------------------+
    1 row in set (0.00 sec)
    mysql> reset master;(有数据的话,就不要做这操作,紧急)
    mysql> select user,host,password from mysql.user;
    +------+-----------+----------+
    | user | host      | password |
    +------+-----------+----------+
    | root | localhost |          |
    | root | oggs      |          |
    | root | 127.0.0.1 |          |
    | root | ::1       |          |
    |      | localhost |          |
    |      | oggs      |          |
    +------+-----------+----------+
    6 rows in set (0.00 sec)
    mysql> delete from mysql.user where user='';
    mysql> delete from mysql.user where host='::1';
    Query OK, 1 row affected (0.01 sec)
    mysql> delete from mysql.user where host='oggs';
    Query OK, 1 row affected (0.01 sec)
    mysql> select user,host,password from mysql.user;
    +------+-----------+----------+
    | user | host      | password |
    +------+-----------+----------+
    | root | localhost |          |
    | root | 127.0.0.1 |          |
    +------+-----------+----------+
    2 rows in set (0.00 sec)
    mysql> flush privileges;
    Query OK, 0 rows affected (0.00 sec)

    在主服务器上建立帐户并授权slave:
    mysql> grant REPLICATION CLIENT,REPLICATION SLAVE on *.* to rep@'192.168.100.200' identified by 'rep’;(从端ip)
    mysql> flush privileges;
    查看主服务器状态
    查看主库的状态,查看主库的binlog的?文件名字,以及?目前binlog的pos,从库需要读取binlog并且进?行行同步
    mysql> show master status;
    +------------------+----------+--------------+------------------+-------------------+
    | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
    +------------------+----------+--------------+------------------+-------------------+
    | mysql-bin.000001 | 120 | | | |
    +------------------+----------+--------------+------------------+-------------------+
    执?行行完此步骤后不要再操作主服务器MYSQL,防?止主服务器状态值变化
     
    从库:
    mysql> show master status;
    mysql> reset master;
    从库设置需要同步的主库
    mysql> change master to master_host='192.168.100.100',master_user='rep',master_password='rep', master_log_file='mysql-bin.000007',master_log_pos=120,MASTER_PORT=3306;
    开启复制,关闭使用stop slave
    start slave;
    主库关闭读锁
    unlock table;
    检查从服务器复制功能状态:
    mysql> show slave statusG
    *************************** 1
    -----
    Slave_IO_Running: Yes-----------------此状态必须YES
    Slave_SQL_Running: Yes-----------------此状态必须YES

    -----
    双主搭建:
    主键冲突
    步长要不一致,防止binlog冲突
    主:1
    另一主:2

    ------------------------
    gtid
    在mysql中查看server_uuid,mysql在启动过程中,读取auto.cnf的server__uuid

    在从库开启GTID,开始从主库复制数据
    change master to
    master_host='192.168.60.22',master_port=3306,master_user='rep',master_password='123456',master_auto_p
    osition=1;
    -----------------------
    配置半同步复制需要先配置异步复制
    加载插件
    主库和从库都需要安装插件
    主库
    INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_master.so';
    从库
    INSTALL PLUGIN rpl_semi_sync_slave SONAME 'semisync_slave.so';
    查看插件是否加载成功
    show plugins;
    rpl_semi_sync_master | ACTIVE | REPLICATION | semisync_master.so | GPL
    启动半同步复制
    在安装完插件后,半同步复制默认是关闭的,这时需设置参数来开启半同步
    主库
    SET GLOBAL rpl_semi_sync_master_enabled = 1;
    从库
    SET GLOBAL rpl_semi_sync_slave_enabled = 1;
    以上的启动?式是在命令?操作,也可写在配置?件中。
    在有的?可?架构下,master和slave需同时启动,以便在切换后能继续使?半同步复制
    plugin-load = "rpl_semi_sync_master=semisync_master.so;rpl_semi_sync_slave=semisync_slave.so"
    rpl-semi-sync-master-enabled = 1
    rpl-semi-sync-slave-enabled = 1
    重启从库的IO线程
    STOP SLAVE IO_THREAD;
    START SLAVE IO_THREAD;
    如果没有重启,则默认还是异步复制,重启后,slave会在master上注册为半同步复制的slave??。
    这时候,主的error.log中会打印如下信息:
    2017-04-19 11:09:26 28300 [Note] Semi-sync replication switched ON with slave (server_id: 28703307)
    at (mysql-bin.000002, 510)
    2017-04-19 11:09:26 28300 [Note] Start semi-sync binlog_dump to slave (server_id: 28703307),
    pos(mysql-bin.000002, 510)
    2017-04-19 11:09:26 28300 [Note] Stop asynchronous binlog_dump to slave (server_id: 28703307)
    查看半同步是否在运?
    主库
    mysql> show status like 'Rpl_semi_sync_master_status';
    +-----------------------------+-------+
    | Variable_name | Value |
    +-----------------------------+-------+
    | Rpl_semi_sync_master_status | ON |
    +-----------------------------+-------+
    1 row in set (0.00 sec)
    从库
    mysql> show status like 'Rpl_semi_sync_slave_status';
    +----------------------------+-------+
    | Variable_name | Value |
    +----------------------------+-------+
    | Rpl_semi_sync_slave_status | ON |
    +----------------------------+-------+
    1 row in set (0.20 sec)
    这两个变量常?来监控主从是否运?在半同步复制模式下。
    mysql> show status like '%semi%';
    +--------------------------------------------+-------+
    | Variable_name | Value |
    +--------------------------------------------+-------+
    | Rpl_semi_sync_master_clients | 1 |
    | Rpl_semi_sync_master_net_avg_wait_time | 782 |
    | Rpl_semi_sync_master_net_wait_time | 3130 |
    | Rpl_semi_sync_master_net_waits | 4 |
    | Rpl_semi_sync_master_no_times | 1 |
    | Rpl_semi_sync_master_no_tx | 6 |
    | Rpl_semi_sync_master_status | ON |
    | Rpl_semi_sync_master_timefunc_failures | 0 |
    | Rpl_semi_sync_master_tx_avg_wait_time | 717 |
    | Rpl_semi_sync_master_tx_wait_time | 2871 |
    | Rpl_semi_sync_master_tx_waits | 4 |
    | Rpl_semi_sync_master_wait_pos_backtraverse | 0 |
    | Rpl_semi_sync_master_wait_sessions | 0 |
    | Rpl_semi_sync_master_yes_tx | 4 |
    +--------------------------------------------+———+
    Rpl_semi_sync_master_status:指示主服务器使?的是异步复制模式,还是半同步复制模式。
    Rpl_semi_sync_master_clients:显示有多少个从服务器配置成了半同步复制模式。
    Rpl_semi_sync_master_yes_tx:显示从服务器确认的成功提交数量。
    Rpl_semi_sync_master_no_tx:显示从服务器确认的不成功提交数量。
  • 相关阅读:
    《C/C++实现Console下的加载进度条模拟[美观版]》
    VSCode六大通用插件真香合集
    视频+图文 String类干货向总结
    视频+图文+动画 详解插入排序(轻松易理解系列)
    【视频+图文】Java经典基础练习题(六):猴子吃桃子问题
    【视频+图文+动画】详解选择排序(轻松易理解系列)
    【视频+图文】Java经典基础练习题(五):键盘输入一个五位数,判断这个数是否为回文数
    【视频+图文+动画】详解冒泡排序(万字长文,超级详细~~)
    详解时间、空间复杂度(内含彩蛋~~)
    【视频+图文】Java经典基础练习题(四):键盘输入一个正整数,获取这个正整数的每一位并将其输出
  • 原文地址:https://www.cnblogs.com/ect123/p/11739457.html
Copyright © 2011-2022 走看看