MySQL/MariaDB数据库的主主复制
MySQL/MariaDB数据库的主主复制
作者:尹正杰
版权声明:原创作品,谢绝转载!否则将追究法律责任。
一.主主复制概述
1>.什么是主主复制
- 所谓的主主复制,说白了就是两台节点互为主从关系。
2>.主主复制考虑要点
- 由于主主复制,2个MySQL实例均可以进行写操作,需要考虑客户端在Innodb存储引擎中对同一行的操作,以及自动增长id的关系。
3>.主主复制的缺点
- 由于2个节点都可以进行写操作,很容易产生数据不一致的问题,因此生产环境中咱们得慎用呀!
一般情况下主从复制就够用了,考虑到可用性的话可以对master节点使用keepalived做一个高可用即可~
二.主主复制实战
1>.试验环境说明
- [root@node102.yinzhengjie.org.cn ~]# cat /etc/redhat-release
- CentOS Linux release 7.6. (Core)
- [root@node102.yinzhengjie.org.cn ~]#
- [root@node102.yinzhengjie.org.cn ~]# uname -r
- 3.10.-.el7.x86_64
- [root@node102.yinzhengjie.org.cn ~]#
- [root@node102.yinzhengjie.org.cn ~]# uname -m
- x86_64
- [root@node102.yinzhengjie.org.cn ~]#
- [root@node102.yinzhengjie.org.cn ~]# free -h
- total used free shared buff/cache available
- Mem: .7G 239M .0G 24M 446M .2G
- Swap: .9G 0B .9G
- [root@node102.yinzhengjie.org.cn ~]#
- [root@node102.yinzhengjie.org.cn ~]#
- [root@node102.yinzhengjie.org.cn ~]#
- [root@node102.yinzhengjie.org.cn ~]# mysql --version
- mysql Ver 15.1 Distrib 5.5.-MariaDB, for Linux (x86_64) using readline 5.1
- [root@node102.yinzhengjie.org.cn ~]#
node102.yinzhengjie.org.cn节点信息
- [root@node103.yinzhengjie.org.cn ~]# cat /etc/redhat-release
- CentOS Linux release 7.6. (Core)
- [root@node103.yinzhengjie.org.cn ~]#
- [root@node103.yinzhengjie.org.cn ~]# uname -r
- 3.10.-.el7.x86_64
- [root@node103.yinzhengjie.org.cn ~]#
- [root@node103.yinzhengjie.org.cn ~]# uname -m
- x86_64
- [root@node103.yinzhengjie.org.cn ~]#
- [root@node103.yinzhengjie.org.cn ~]# free -h
- total used free shared buff/cache available
- Mem: .7G 245M .9G 8.6M 552M .2G
- Swap: .9G 0B .9G
- [root@node103.yinzhengjie.org.cn ~]#
- [root@node103.yinzhengjie.org.cn ~]# mysql --verwsion
- mysql: unknown option '--verwsion'
- [root@node103.yinzhengjie.org.cn ~]#
- [root@node103.yinzhengjie.org.cn ~]# mysql --version
- mysql Ver 15.1 Distrib 5.5.-MariaDB, for Linux (x86_64) using readline 5.1
- [root@node103.yinzhengjie.org.cn ~]#
- [root@node103.yinzhengjie.org.cn ~]#
node103.yinzhengjie.org.cn节点信息
2>.编辑node102.yinzhengjie.org.cn节点的my.cnf配置文件
- [root@node102.yinzhengjie.org.cn ~]# cat /etc/my.cnf
- [mysqld]
- server-id = #为当前节点设置一个全局惟一的ID号,用于标识当前MySQL实例
- binlog_format = row #推荐使用基于行数据的复制
- log_bin = /data/mysql/logbin/master- #需要启用二进制日志,生产环境建议和数据文件分开放存放
- relay_log = relay-log- #relay log的文件路径,默认值hostname-relay-bin
- relay_log_index = relay-log-.index #默认值hostname-relay-bin.index
- auto_increment_offset = #指定当前实例自动增长的开始数字
- auto_increment_increment = #指定当前实例自动增长的幅度
- character-set-server = utf8mb4
- default_storage_engine = InnoDB
- datadir = /var/lib/mysql
- socket = /var/lib/mysql/mysql.sock
- [mysqld_safe]
- log-error = /var/log/mariadb/mariadb.log
- pid-file = /var/run/mariadb/mariadb.pid
- !includedir /etc/my.cnf.d
- [root@node102.yinzhengjie.org.cn ~]#
- [root@node102.yinzhengjie.org.cn ~]#
3>.启动node102.yinzhengjie.org.cn节点的MySQL实例
- [root@node102.yinzhengjie.org.cn ~]# mkdir -pv /data/mysql/logbin/ #创建二进制日志存放目录
- mkdir: created directory ‘/data’
- mkdir: created directory ‘/data/mysql’
- mkdir: created directory ‘/data/mysql/logbin/’
- [root@node102.yinzhengjie.org.cn ~]#
- [root@node102.yinzhengjie.org.cn ~]# ll /data/mysql/logbin/
- total
- [root@node102.yinzhengjie.org.cn ~]#
[root@node102.yinzhengjie.org.cn ~]# mkdir -pv /data/mysql/logbin/ #创建二进制日志存放目录
- [root@node102.yinzhengjie.org.cn ~]# ll -d /data/mysql/logbin/
- drwxr-xr-x root root Nov : /data/mysql/logbin/
- [root@node102.yinzhengjie.org.cn ~]#
- [root@node102.yinzhengjie.org.cn ~]# chown -R mysql:mysql /data/mysql/logbin/ #切记要授权!
- [root@node102.yinzhengjie.org.cn ~]#
- [root@node102.yinzhengjie.org.cn ~]# ll -d /data/mysql/logbin/
- drwxr-xr-x mysql mysql Nov : /data/mysql/logbin/
- [root@node102.yinzhengjie.org.cn ~]#
- [root@node102.yinzhengjie.org.cn ~]#
[root@node102.yinzhengjie.org.cn ~]# chown -R mysql:mysql /data/mysql/logbin/ #切记要授权!
- [root@node102.yinzhengjie.org.cn ~]# ll /data/mysql/logbin/
- total
- [root@node102.yinzhengjie.org.cn ~]#
- [root@node103.yinzhengjie.org.cn ~]# ll /var/lib/mysql/
- total
- [root@node103.yinzhengjie.org.cn ~]#
- [root@node102.yinzhengjie.org.cn ~]# systemctl start mariadb #启动数据库
- [root@node102.yinzhengjie.org.cn ~]#
- [root@node102.yinzhengjie.org.cn ~]# ll /data/mysql/logbin/
- total
- -rw-rw---- mysql mysql Nov : master-102.000001
- -rw-rw---- mysql mysql Nov : master-102.000002
- -rw-rw---- mysql mysql Nov : master-102.000003
- -rw-rw---- mysql mysql Nov : master-.index
- [root@node102.yinzhengjie.org.cn ~]#
- [root@node102.yinzhengjie.org.cn ~]# ll /var/lib/mysql/
- total
- -rw-rw---- mysql mysql Nov : aria_log.
- -rw-rw---- mysql mysql Nov : aria_log_control
- -rw-rw---- mysql mysql Nov : ibdata1
- -rw-rw---- mysql mysql Nov : ib_logfile0
- -rw-rw---- mysql mysql Nov : ib_logfile1
- drwx------ mysql mysql Nov : mysql
- srwxrwxrwx mysql mysql Nov : mysql.sock
- drwx------ mysql mysql Nov : performance_schema
- drwx------ mysql mysql Nov : test
- [root@node102.yinzhengjie.org.cn ~]#
- [root@node102.yinzhengjie.org.cn ~]#
[root@node102.yinzhengjie.org.cn ~]# systemctl start mariadb #启动数据库
4>.在node102.yinzhengjie.org.cn节点上创建有复制权限的用户账号
- [root@node102.yinzhengjie.org.cn ~]# mysql
- Welcome to the MariaDB monitor. Commands end with ; or \g.
- Your MariaDB connection id is
- Server version: 5.5.-MariaDB MariaDB Server
- Copyright (c) , , Oracle, MariaDB Corporation Ab and others.
- Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
- MariaDB [(none)]>
- MariaDB [(none)]> SHOW MASTER LOGS;
- +-------------------+-----------+
- | Log_name | File_size |
- +-------------------+-----------+
- | master-102.000001 | |
- | master-102.000002 | |
- | master-102.000003 | |
- +-------------------+-----------+
- rows in set (0.00 sec)
- MariaDB [(none)]>
- MariaDB [(none)]> GRANT REPLICATION SLAVE ON *.* TO 'copy'@'172.30.1.10%' IDENTIFIED BY 'yinzhengjie';
- Query OK, rows affected (0.00 sec)
- MariaDB [(none)]>
- MariaDB [(none)]> SHOW MASTER LOGS;
- +-------------------+-----------+
- | Log_name | File_size |
- +-------------------+-----------+
- | master-102.000001 | |
- | master-102.000002 | |
- | master-102.000003 | |
- +-------------------+-----------+
- rows in set (0.00 sec)
- MariaDB [(none)]>
- MariaDB [(none)]> SELECT user,host,password FROM mysql.user;
- +------+----------------------------+-------------------------------------------+
- | user | host | password |
- +------+----------------------------+-------------------------------------------+
- | root | localhost | |
- | root | node102.yinzhengjie.org.cn | |
- | root | 127.0.0.1 | |
- | root | :: | |
- | | localhost | |
- | | node102.yinzhengjie.org.cn | |
- | copy | 172.30.1.10% | *BD0B1F48FDC55BD27555FC2F22FF29A68A25A1D7 |
- +------+----------------------------+-------------------------------------------+
- rows in set (0.00 sec)
- MariaDB [(none)]>
- MariaDB [(none)]>
MariaDB [(none)]> GRANT REPLICATION SLAVE ON *.* TO 'copy'@'172.30.1.10%' IDENTIFIED BY 'yinzhengjie';
5>.编辑node103.yinzhengjie.org.cn节点的my.cnf配置文件
- [root@node103.yinzhengjie.org.cn ~]# cat /etc/my.cnf
- [mysqld]
- server-id =
- binlog_format = row
- log_bin = /data/mysql/logbin/master-
- relay_log = relay-log-
- relay_log_index = relay-log-.index
- auto_increment_offset = #注意这个起始位置不要和node102.yinzhengjie.org.cn配置一样哟
- auto_increment_increment =
- character-set-server = utf8mb4
- default_storage_engine = InnoDB
- datadir = /var/lib/mysql
- socket = /var/lib/mysql/mysql.sock
- [mysqld_safe]
- log-error = /var/log/mariadb/mariadb.log
- pid-file = /var/run/mariadb/mariadb.pid
- !includedir /etc/my.cnf.d
- [root@node103.yinzhengjie.org.cn ~]#
6>.启动node103.yinzhengjie.org.cn节点的MySQL实例
- [root@node103.yinzhengjie.org.cn ~]# install -d /data/mysql/logbin -o mysql -g mysql #创建存放的二进制目录
- [root@node103.yinzhengjie.org.cn ~]#
- [root@node103.yinzhengjie.org.cn ~]# ll -d /data/mysql/logbin/
- drwxr-xr-x mysql mysql Nov : /data/mysql/logbin/
- [root@node103.yinzhengjie.org.cn ~]#
- [root@node103.yinzhengjie.org.cn ~]# ll /data/mysql/logbin/
- total
- [root@node103.yinzhengjie.org.cn ~]#
[root@node103.yinzhengjie.org.cn ~]# install -d /data/mysql/logbin -o mysql -g mysql #创建存放的二进制目录
- [root@node103.yinzhengjie.org.cn ~]# ll /var/lib/mysql/
- total
- [root@node103.yinzhengjie.org.cn ~]#
- [root@node103.yinzhengjie.org.cn ~]# ll /data/mysql/logbin/
- total
- [root@node103.yinzhengjie.org.cn ~]#
- [root@node103.yinzhengjie.org.cn ~]# systemctl start mariadb
- [root@node103.yinzhengjie.org.cn ~]#
- [root@node103.yinzhengjie.org.cn ~]# ll /var/lib/mysql/
- total
- -rw-rw---- mysql mysql Nov : aria_log.
- -rw-rw---- mysql mysql Nov : aria_log_control
- -rw-rw---- mysql mysql Nov : ibdata1
- -rw-rw---- mysql mysql Nov : ib_logfile0
- -rw-rw---- mysql mysql Nov : ib_logfile1
- drwx------ mysql mysql Nov : mysql
- srwxrwxrwx mysql mysql Nov : mysql.sock
- drwx------ mysql mysql Nov : performance_schema
- drwx------ mysql mysql Nov : test
- [root@node103.yinzhengjie.org.cn ~]#
- [root@node103.yinzhengjie.org.cn ~]# ll /data/mysql/logbin/
- total
- -rw-rw---- mysql mysql Nov : master-103.000001
- -rw-rw---- mysql mysql Nov : master-103.000002
- -rw-rw---- mysql mysql Nov : master-103.000003
- -rw-rw---- mysql mysql Nov : master-.index
- [root@node103.yinzhengjie.org.cn ~]#
[root@node103.yinzhengjie.org.cn ~]# systemctl start mariadb
7>.配置两节点互为主主节点并验证
- [root@node103.yinzhengjie.org.cn ~]# mysql
- Welcome to the MariaDB monitor. Commands end with ; or \g.
- Your MariaDB connection id is
- Server version: 5.5.-MariaDB MariaDB Server
- Copyright (c) , , Oracle, MariaDB Corporation Ab and others.
- Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
- MariaDB [(none)]>
- MariaDB [(none)]> CHANGE MASTER TO
- -> MASTER_HOST='172.30.1.102',
- -> MASTER_USER='copy',
- -> MASTER_PASSWORD='yinzhengjie',
- -> MASTER_PORT=,
- -> MASTER_LOG_FILE='master-102.000003',
- -> MASTER_LOG_POS=,
- -> MASTER_CONNECT_RETRY=;
- Query OK, rows affected (0.01 sec)
- MariaDB [(none)]>
- MariaDB [(none)]> SHOW PROCESSLIST;
- +----+------+-----------+------+---------+------+-------+------------------+----------+
- | Id | User | Host | db | Command | Time | State | Info | Progress |
- +----+------+-----------+------+---------+------+-------+------------------+----------+
- | | root | localhost | NULL | Query | | NULL | SHOW PROCESSLIST | 0.000 |
- +----+------+-----------+------+---------+------+-------+------------------+----------+
- row in set (0.00 sec)
- MariaDB [(none)]>
- MariaDB [(none)]> START SLAVE;
- Query OK, rows affected (0.00 sec)
- MariaDB [(none)]>
- MariaDB [(none)]> SHOW PROCESSLIST\G
- *************************** . row ***************************
- Id:
- User: root
- Host: localhost
- db: NULL
- Command: Query
- Time:
- State: NULL
- Info: SHOW PROCESSLIST
- Progress: 0.000
- *************************** . row ***************************
- Id:
- User: system user
- Host:
- db: NULL
- Command: Connect
- Time:
- State: Waiting for master to send event
- Info: NULL
- Progress: 0.000
- *************************** . row ***************************
- Id:
- User: system user
- Host:
- db: NULL
- Command: Connect
- Time:
- State: Slave has read all relay log; waiting for the slave I/O thread to update it
- Info: NULL
- Progress: 0.000
- rows in set (0.00 sec)
- MariaDB [(none)]>
- MariaDB [(none)]> SHOW SLAVE STATUS\G
- *************************** . row ***************************
- Slave_IO_State: Waiting for master to send event
- Master_Host: 172.30.1.102
- Master_User: copy
- Master_Port:
- Connect_Retry:
- Master_Log_File: master-102.000003
- Read_Master_Log_Pos:
- Relay_Log_File: relay-log-103.000002
- Relay_Log_Pos:
- Relay_Master_Log_File: master-102.000003
- Slave_IO_Running: Yes
- Slave_SQL_Running: Yes
- Replicate_Do_DB:
- Replicate_Ignore_DB:
- Replicate_Do_Table:
- Replicate_Ignore_Table:
- Replicate_Wild_Do_Table:
- Replicate_Wild_Ignore_Table:
- Last_Errno:
- Last_Error:
- Skip_Counter:
- Exec_Master_Log_Pos:
- Relay_Log_Space:
- Until_Condition: None
- Until_Log_File:
- Until_Log_Pos:
- Master_SSL_Allowed: No
- Master_SSL_CA_File:
- Master_SSL_CA_Path:
- Master_SSL_Cert:
- Master_SSL_Cipher:
- Master_SSL_Key:
- Seconds_Behind_Master:
- Master_SSL_Verify_Server_Cert: No
- Last_IO_Errno:
- Last_IO_Error:
- Last_SQL_Errno:
- Last_SQL_Error:
- Replicate_Ignore_Server_Ids:
- Master_Server_Id:
- row in set (0.00 sec)
- MariaDB [(none)]>
- MariaDB [(none)]> SELECT user,host,password FROM mysql.user;
- +------+----------------------------+-------------------------------------------+
- | user | host | password |
- +------+----------------------------+-------------------------------------------+
- | root | localhost | |
- | root | node103.yinzhengjie.org.cn | |
- | root | 127.0.0.1 | |
- | root | :: | |
- | | localhost | |
- | | node103.yinzhengjie.org.cn | |
- | copy | 172.30.1.10% | *BD0B1F48FDC55BD27555FC2F22FF29A68A25A1D7 |
- +------+----------------------------+-------------------------------------------+
- rows in set (0.00 sec)
- MariaDB [(none)]> SHOW MASTER LOGS;
- +-------------------+-----------+
- | Log_name | File_size |
- +-------------------+-----------+
- | master-103.000001 | |
- | master-103.000002 | |
- | master-103.000003 | |
- +-------------------+-----------+
- rows in set (0.00 sec)
- MariaDB [(none)]>
- MariaDB [(none)]> QUIT
- Bye
- [root@node103.yinzhengjie.org.cn ~]#
配置node103.yinzhengjie.org.cn为node102.yinzhengjie.org.cn从节点详细过程
- [root@node102.yinzhengjie.org.cn ~]# mysql
- Welcome to the MariaDB monitor. Commands end with ; or \g.
- Your MariaDB connection id is
- Server version: 5.5.-MariaDB MariaDB Server
- Copyright (c) , , Oracle, MariaDB Corporation Ab and others.
- Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
- MariaDB [(none)]>
- MariaDB [(none)]> SHOW PROCESSLIST\G
- *************************** . row ***************************
- Id:
- User: root
- Host: localhost
- db: NULL
- Command: Query
- Time:
- State: NULL
- Info: SHOW PROCESSLIST
- Progress: 0.000
- *************************** . row ***************************
- Id:
- User: copy
- Host: node103.yinzhengjie.org.cn:
- db: NULL
- Command: Binlog Dump
- Time:
- State: Master has sent all binlog to slave; waiting for binlog to be updated
- Info: NULL
- Progress: 0.000
- rows in set (0.00 sec)
- MariaDB [(none)]>
- MariaDB [(none)]> SHOW MASTER LOGS;
- +-------------------+-----------+
- | Log_name | File_size |
- +-------------------+-----------+
- | master-102.000001 | |
- | master-102.000002 | |
- | master-102.000003 | |
- +-------------------+-----------+
- rows in set (0.00 sec)
- MariaDB [(none)]>
- MariaDB [(none)]> QUIT
- Bye
- [root@node102.yinzhengjie.org.cn ~]#
查看node102.yinzhengjie.org.cn节点的线程数
- [root@node103.yinzhengjie.org.cn ~]# mysql
- Welcome to the MariaDB monitor. Commands end with ; or \g.
- Your MariaDB connection id is
- Server version: 5.5.-MariaDB MariaDB Server
- Copyright (c) , , Oracle, MariaDB Corporation Ab and others.
- Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
- MariaDB [(none)]>
- MariaDB [(none)]> SHOW PROCESSLIST\G
- *************************** . row ***************************
- Id:
- User: root
- Host: localhost
- db: NULL
- Command: Query
- Time:
- State: NULL
- Info: SHOW PROCESSLIST
- Progress: 0.000
- *************************** . row ***************************
- Id:
- User: system user
- Host:
- db: NULL
- Command: Connect
- Time:
- State: Waiting for master to send event
- Info: NULL
- Progress: 0.000
- *************************** . row ***************************
- Id:
- User: system user
- Host:
- db: NULL
- Command: Connect
- Time:
- State: Slave has read all relay log; waiting for the slave I/O thread to update it
- Info: NULL
- Progress: 0.000
- rows in set (0.00 sec)
- MariaDB [(none)]>
- MariaDB [(none)]> QUIT
- Bye
- [root@node103.yinzhengjie.org.cn ~]#
查看node103.yinzhengjie.org.cn节点的线程数
- [root@node102.yinzhengjie.org.cn ~]# mysql
- Welcome to the MariaDB monitor. Commands end with ; or \g.
- Your MariaDB connection id is
- Server version: 5.5.-MariaDB MariaDB Server
- Copyright (c) , , Oracle, MariaDB Corporation Ab and others.
- Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
- MariaDB [(none)]>
- MariaDB [(none)]> SHOW DATABASES;
- +--------------------+
- | Database |
- +--------------------+
- | information_schema |
- | mysql |
- | performance_schema |
- | test |
- +--------------------+
- rows in set (0.00 sec)
- MariaDB [(none)]>
- MariaDB [(none)]> CREATE DATABASE devops;
- Query OK, row affected (0.00 sec)
- MariaDB [(none)]>
- MariaDB [(none)]> USE devops
- Database changed
- MariaDB [devops]>
- MariaDB [devops]> CREATE TABLE students(id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,name VARCHAR() NOT NULL,sex ENUM('boy','girl') DEFAULT 'boy',age TINYINT UNSIGNED,mobile CHAR(),address VARCHAR());
- Query OK, rows affected (0.00 sec)
- MariaDB [devops]>
- MariaDB [devops]> DESC students;
- +---------+---------------------+------+-----+---------+----------------+
- | Field | Type | Null | Key | Default | Extra |
- +---------+---------------------+------+-----+---------+----------------+
- | id | int() unsigned | NO | PRI | NULL | auto_increment |
- | name | varchar() | NO | | NULL | |
- | sex | enum('boy','girl') | YES | | boy | |
- | age | tinyint() unsigned | YES | | NULL | |
- | mobile | char() | YES | | NULL | |
- | address | varchar() | YES | | NULL | |
- +---------+---------------------+------+-----+---------+----------------+
- rows in set (0.00 sec)
- MariaDB [devops]> INSERT INTO students SET name='尹正杰',age=,address='北京';
- Query OK, row affected (0.00 sec)
- MariaDB [devops]>
- MariaDB [devops]> SELECT * FROM students;
- +----+-----------+------+------+--------+---------+
- | id | name | sex | age | mobile | address |
- +----+-----------+------+------+--------+---------+
- | | 尹正杰 | boy | | NULL | 北京 |
- +----+-----------+------+------+--------+---------+
- row in set (0.00 sec)
- MariaDB [devops]>
- MariaDB [devops]> SHOW MASTER LOGS;
- +-------------------+-----------+
- | Log_name | File_size |
- +-------------------+-----------+
- | master-102.000001 | |
- | master-102.000002 | |
- | master-102.000003 | |
- +-------------------+-----------+
- rows in set (0.00 sec)
- MariaDB [devops]>
- MariaDB [devops]> SHOW MASTER STATUS;
- +-------------------+----------+--------------+------------------+
- | File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
- +-------------------+----------+--------------+------------------+
- | master-102.000003 | | | |
- +-------------------+----------+--------------+------------------+
- row in set (0.00 sec)
- MariaDB [devops]>
- MariaDB [devops]> QUIT
- Bye
- [root@node102.yinzhengjie.org.cn ~]#
- [root@node102.yinzhengjie.org.cn ~]#
在node102.yinzhengjie.org.cn中创建测试数据
- [root@node103.yinzhengjie.org.cn ~]# mysql
- Welcome to the MariaDB monitor. Commands end with ; or \g.
- Your MariaDB connection id is
- Server version: 5.5.-MariaDB MariaDB Server
- Copyright (c) , , Oracle, MariaDB Corporation Ab and others.
- Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
- MariaDB [(none)]>
- MariaDB [(none)]> SHOW DATABASES;
- +--------------------+
- | Database |
- +--------------------+
- | information_schema |
- | devops |
- | mysql |
- | performance_schema |
- | test |
- +--------------------+
- rows in set (0.00 sec)
- MariaDB [(none)]>
- MariaDB [(none)]> USE devops
- Reading table information for completion of table and column names
- You can turn off this feature to get a quicker startup with -A
- Database changed
- MariaDB [devops]>
- MariaDB [devops]> SHOW TABLES;
- +------------------+
- | Tables_in_devops |
- +------------------+
- | students |
- +------------------+
- row in set (0.00 sec)
- MariaDB [devops]>
- MariaDB [devops]> SELECT * FROM students;
- +----+-----------+------+------+--------+---------+
- | id | name | sex | age | mobile | address |
- +----+-----------+------+------+--------+---------+
- | | 尹正杰 | boy | | NULL | 北京 |
- +----+-----------+------+------+--------+---------+
- row in set (0.00 sec)
- MariaDB [devops]>
- MariaDB [devops]> QUIT
- Bye
- [root@node103.yinzhengjie.org.cn ~]#
在node103.yinzhengjie.org.cn节点上可以看到和node102.yinzhengjie.org.cn节点一样的数据(配置成功)
- [root@node102.yinzhengjie.org.cn ~]# mysql
- Welcome to the MariaDB monitor. Commands end with ; or \g.
- Your MariaDB connection id is
- Server version: 5.5.-MariaDB MariaDB Server
- Copyright (c) , , Oracle, MariaDB Corporation Ab and others.
- Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
- MariaDB [(none)]>
- MariaDB [(none)]> SHOW PROCESSLIST\G
- *************************** . row ***************************
- Id:
- User: copy
- Host: node103.yinzhengjie.org.cn:
- db: NULL
- Command: Binlog Dump
- Time:
- State: Master has sent all binlog to slave; waiting for binlog to be updated
- Info: NULL
- Progress: 0.000
- *************************** . row ***************************
- Id:
- User: root
- Host: localhost
- db: NULL
- Command: Query
- Time:
- State: NULL
- Info: SHOW PROCESSLIST
- Progress: 0.000
- rows in set (0.00 sec)
- MariaDB [(none)]>
- MariaDB [(none)]> CHANGE MASTER TO
- -> MASTER_HOST='172.30.1.103',
- -> MASTER_USER='copy',
- -> MASTER_PASSWORD='yinzhengjie',
- -> MASTER_PORT=,
- -> MASTER_LOG_FILE='master-103.000003',
- -> MASTER_LOG_POS=,
- -> MASTER_CONNECT_RETRY=;
- Query OK, rows affected (0.01 sec)
- MariaDB [(none)]>
- MariaDB [(none)]> START SLAVE;
- Query OK, rows affected (0.00 sec)
- MariaDB [(none)]>
- MariaDB [(none)]> SHOW PROCESSLIST\G
- *************************** . row ***************************
- Id:
- User: copy
- Host: node103.yinzhengjie.org.cn:
- db: NULL
- Command: Binlog Dump
- Time:
- State: Master has sent all binlog to slave; waiting for binlog to be updated
- Info: NULL
- Progress: 0.000
- *************************** . row ***************************
- Id:
- User: root
- Host: localhost
- db: NULL
- Command: Query
- Time:
- State: NULL
- Info: SHOW PROCESSLIST
- Progress: 0.000
- *************************** . row ***************************
- Id:
- User: system user
- Host:
- db: NULL
- Command: Connect
- Time:
- State: Waiting for master to send event
- Info: NULL
- Progress: 0.000
- *************************** . row ***************************
- Id:
- User: system user
- Host:
- db: NULL
- Command: Connect
- Time:
- State: Slave has read all relay log; waiting for the slave I/O thread to update it
- Info: NULL
- Progress: 0.000
- rows in set (0.00 sec)
- MariaDB [(none)]>
- MariaDB [(none)]> SHOW SLAVE STATUS\G
- *************************** . row ***************************
- Slave_IO_State: Waiting for master to send event
- Master_Host: 172.30.1.103
- Master_User: copy
- Master_Port:
- Connect_Retry:
- Master_Log_File: master-103.000003
- Read_Master_Log_Pos:
- Relay_Log_File: relay-log-102.000002
- Relay_Log_Pos:
- Relay_Master_Log_File: master-103.000003
- Slave_IO_Running: Yes
- Slave_SQL_Running: Yes
- Replicate_Do_DB:
- Replicate_Ignore_DB:
- Replicate_Do_Table:
- Replicate_Ignore_Table:
- Replicate_Wild_Do_Table:
- Replicate_Wild_Ignore_Table:
- Last_Errno:
- Last_Error:
- Skip_Counter:
- Exec_Master_Log_Pos:
- Relay_Log_Space:
- Until_Condition: None
- Until_Log_File:
- Until_Log_Pos:
- Master_SSL_Allowed: No
- Master_SSL_CA_File:
- Master_SSL_CA_Path:
- Master_SSL_Cert:
- Master_SSL_Cipher:
- Master_SSL_Key:
- Seconds_Behind_Master:
- Master_SSL_Verify_Server_Cert: No
- Last_IO_Errno:
- Last_IO_Error:
- Last_SQL_Errno:
- Last_SQL_Error:
- Replicate_Ignore_Server_Ids:
- Master_Server_Id:
- row in set (0.00 sec)
- MariaDB [(none)]>
- MariaDB [(none)]> QUIT
- Bye
- [root@node102.yinzhengjie.org.cn ~]#
配置node102.yinzhengjie.org.cn为node103.yinzhengjie.org.cn从节点详细过程
- [root@node102.yinzhengjie.org.cn ~]# mysql
- Welcome to the MariaDB monitor. Commands end with ; or \g.
- Your MariaDB connection id is
- Server version: 5.5.-MariaDB MariaDB Server
- Copyright (c) , , Oracle, MariaDB Corporation Ab and others.
- Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
- MariaDB [(none)]>
- MariaDB [(none)]> SHOW PROCESSLIST\G
- *************************** . row ***************************
- Id:
- User: copy
- Host: node103.yinzhengjie.org.cn:
- db: NULL
- Command: Binlog Dump
- Time:
- State: Master has sent all binlog to slave; waiting for binlog to be updated
- Info: NULL
- Progress: 0.000
- *************************** . row ***************************
- Id:
- User: system user
- Host:
- db: NULL
- Command: Connect
- Time:
- State: Waiting for master to send event
- Info: NULL
- Progress: 0.000
- *************************** . row ***************************
- Id:
- User: system user
- Host:
- db: NULL
- Command: Connect
- Time:
- State: Slave has read all relay log; waiting for the slave I/O thread to update it
- Info: NULL
- Progress: 0.000
- *************************** . row ***************************
- Id:
- User: root
- Host: localhost
- db: NULL
- Command: Query
- Time:
- State: NULL
- Info: SHOW PROCESSLIST
- Progress: 0.000
- rows in set (0.00 sec)
- MariaDB [(none)]>
- MariaDB [(none)]> SHOW MASTER LOGS;
- +-------------------+-----------+
- | Log_name | File_size |
- +-------------------+-----------+
- | master-102.000001 | |
- | master-102.000002 | |
- | master-102.000003 | |
- +-------------------+-----------+
- rows in set (0.00 sec)
- MariaDB [(none)]>
- MariaDB [(none)]> QUIT
- Bye
- [root@node102.yinzhengjie.org.cn ~]#
查看node102.yinzhengjie.org.cn节点的线程数
- [root@node103.yinzhengjie.org.cn ~]# mysql
- Welcome to the MariaDB monitor. Commands end with ; or \g.
- Your MariaDB connection id is
- Server version: 5.5.-MariaDB MariaDB Server
- Copyright (c) , , Oracle, MariaDB Corporation Ab and others.
- Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
- MariaDB [(none)]>
- MariaDB [(none)]> SHOW PROCESSLIST\G
- *************************** . row ***************************
- Id:
- User: system user
- Host:
- db: NULL
- Command: Connect
- Time:
- State: Waiting for master to send event
- Info: NULL
- Progress: 0.000
- *************************** . row ***************************
- Id:
- User: system user
- Host:
- db: NULL
- Command: Connect
- Time:
- State: Slave has read all relay log; waiting for the slave I/O thread to update it
- Info: NULL
- Progress: 0.000
- *************************** . row ***************************
- Id:
- User: copy
- Host: node102.yinzhengjie.org.cn:
- db: NULL
- Command: Binlog Dump
- Time:
- State: Master has sent all binlog to slave; waiting for binlog to be updated
- Info: NULL
- Progress: 0.000
- *************************** . row ***************************
- Id:
- User: root
- Host: localhost
- db: NULL
- Command: Query
- Time:
- State: NULL
- Info: SHOW PROCESSLIST
- Progress: 0.000
- rows in set (0.00 sec)
- MariaDB [(none)]>
- MariaDB [(none)]> SHOW MASTER LOGS;
- +-------------------+-----------+
- | Log_name | File_size |
- +-------------------+-----------+
- | master-103.000001 | |
- | master-103.000002 | |
- | master-103.000003 | |
- +-------------------+-----------+
- rows in set (0.00 sec)
- MariaDB [(none)]> QUIT
- Bye
- [root@node103.yinzhengjie.org.cn ~]#
- [root@node103.yinzhengjie.org.cn ~]#
查看node103.yinzhengjie.org.cn节点的线程数
- [root@node103.yinzhengjie.org.cn ~]# mysql
- Welcome to the MariaDB monitor. Commands end with ; or \g.
- Your MariaDB connection id is
- Server version: 5.5.-MariaDB MariaDB Server
- Copyright (c) , , Oracle, MariaDB Corporation Ab and others.
- Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
- MariaDB [(none)]>
- MariaDB [(none)]> USE devops;
- Reading table information for completion of table and column names
- You can turn off this feature to get a quicker startup with -A
- Database changed
- MariaDB [devops]>
- MariaDB [devops]> SHOW TABLES;
- +------------------+
- | Tables_in_devops |
- +------------------+
- | students |
- +------------------+
- row in set (0.00 sec)
- MariaDB [devops]>
- MariaDB [devops]> SELECT * FROM students;
- +----+-----------+------+------+--------+---------+
- | id | name | sex | age | mobile | address |
- +----+-----------+------+------+--------+---------+
- | | 尹正杰 | boy | | NULL | 北京 |
- +----+-----------+------+------+--------+---------+
- row in set (0.00 sec)
- MariaDB [devops]>
- MariaDB [devops]> INSERT INTO students SET name='yinzhengjie',age=,address='shanxi';
- Query OK, row affected (0.01 sec)
- MariaDB [devops]>
- MariaDB [devops]> SELECT * FROM students;
- +----+-------------+------+------+--------+---------+
- | id | name | sex | age | mobile | address |
- +----+-------------+------+------+--------+---------+
- | | 尹正杰 | boy | | NULL | 北京 |
- | | yinzhengjie | boy | | NULL | shanxi |
- +----+-------------+------+------+--------+---------+
- rows in set (0.00 sec)
- MariaDB [devops]> QUIT
- Bye
- [root@node103.yinzhengjie.org.cn ~]#
在node103.yinzhengjie.org.cn中创建测试数据
- [root@node102.yinzhengjie.org.cn ~]# mysql
- Welcome to the MariaDB monitor. Commands end with ; or \g.
- Your MariaDB connection id is
- Server version: 5.5.-MariaDB MariaDB Server
- Copyright (c) , , Oracle, MariaDB Corporation Ab and others.
- Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
- MariaDB [(none)]>
- MariaDB [(none)]> USE devops
- Reading table information for completion of table and column names
- You can turn off this feature to get a quicker startup with -A
- Database changed
- MariaDB [devops]>
- MariaDB [devops]> SHOW TABLES;
- +------------------+
- | Tables_in_devops |
- +------------------+
- | students |
- +------------------+
- row in set (0.00 sec)
- MariaDB [devops]>
- MariaDB [devops]> SELECT * FROM students;
- +----+-------------+------+------+--------+---------+
- | id | name | sex | age | mobile | address |
- +----+-------------+------+------+--------+---------+
- | | 尹正杰 | boy | | NULL | 北京 |
- | | yinzhengjie | boy | | NULL | shanxi |
- +----+-------------+------+------+--------+---------+
- rows in set (0.00 sec)
- MariaDB [devops]>
- MariaDB [devops]> QUIT
- Bye
- [root@node102.yinzhengjie.org.cn ~]#
在node102.yinzhengjie.org.cn节点上可以看到和node103.yinzhengjie.org.cn节点一样的数据(配置成功)
三.主主复制在生产环境中容易出现的问题刨析
1>.在node102.yinzhengjie.org.cn节点的操作
- [root@node102.yinzhengjie.org.cn ~]# mysql
- Welcome to the MariaDB monitor. Commands end with ; or \g.
- Your MariaDB connection id is
- Server version: 5.5.-MariaDB MariaDB Server
- Copyright (c) , , Oracle, MariaDB Corporation Ab and others.
- Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
- MariaDB [(none)]>
- MariaDB [(none)]>
- MariaDB [(none)]>
- MariaDB [(none)]> USE devops
- Reading table information for completion of table and column names
- You can turn off this feature to get a quicker startup with -A
- Database changed
- MariaDB [devops]>
- MariaDB [devops]> SELECT * FROM students;
- +----+-------------+------+------+--------+---------+
- | id | name | sex | age | mobile | address |
- +----+-------------+------+------+--------+---------+
- | | 尹正杰 | boy | | NULL | 北京 |
- | | yinzhengjie | boy | | NULL | shanxi |
- +----+-------------+------+------+--------+---------+
- rows in set (0.00 sec)
- MariaDB [devops]>
- MariaDB [devops]> INSERT INTO students (name,age,mobile,address) VALUES ('Jason Yin',,,'beijing'),('Jay','',,'Taiwan'); #我们同时往表里插入2条记录
Query OK, rows affected (0.00 sec)- Records: Duplicates: Warnings:
- MariaDB [devops]>
- MariaDB [devops]> SELECT * FROM students; #不难发现,node102.yinzhengjie.org.cn自动增长是基于奇数的
- +----+-------------+------+------+--------+---------+
- | id | name | sex | age | mobile | address |
- +----+-------------+------+------+--------+---------+
- | | 尹正杰 | boy | | NULL | 北京 |
- | | yinzhengjie | boy | | NULL | shanxi |
- | | Jason Yin | boy | | | beijing |
- | | Jay | boy | | | Taiwan |
- +----+-------------+------+------+--------+---------+
- rows in set (0.00 sec)
- MariaDB [devops]>
- MariaDB [devops]> QUIT
- Bye
- [root@node102.yinzhengjie.org.cn ~]#
2>.在node103.yinzhengjie.org.cn节点的操作
- [root@node103.yinzhengjie.org.cn ~]# mysql
- Welcome to the MariaDB monitor. Commands end with ; or \g.
- Your MariaDB connection id is
- Server version: 5.5.-MariaDB MariaDB Server
- Copyright (c) , , Oracle, MariaDB Corporation Ab and others.
- Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
- MariaDB [(none)]>
- MariaDB [(none)]> USE devops
- Reading table information for completion of table and column names
- You can turn off this feature to get a quicker startup with -A
- Database changed
- MariaDB [devops]>
- MariaDB [devops]> SELECT * FROM students; #我们可以发现数据已经同步过来啦!
- +----+-------------+------+------+--------+---------+
- | id | name | sex | age | mobile | address |
- +----+-------------+------+------+--------+---------+
- | | 尹正杰 | boy | | NULL | 北京 |
- | | yinzhengjie | boy | | NULL | shanxi |
- | | Jason Yin | boy | | | beijing |
- | | Jay | boy | | | Taiwan |
- +----+-------------+------+------+--------+---------+
- rows in set (0.00 sec)
- MariaDB [devops]>
- MariaDB [devops]> INSERT students (age,sex,name,mobile,address) VALUES (,'girl','Gloria Tang Tsz-Kei',null,'Hong Kong'); #我们再次插入一条记录
Query OK, row affected (0.00 sec)- MariaDB [devops]>
- MariaDB [devops]> SELECT * FROM students; #我们发现新添加的记录自动增长ID竟然跳过了4,而是直接使用6,起始原因在于在添加记录之前就已经存在最大id数字5啦,虽说自动增长的id依旧是偶数,但这个问题我们得考虑一下哟~
- +----+---------------------+------+------+--------+-----------+
- | id | name | sex | age | mobile | address |
- +----+---------------------+------+------+--------+-----------+
- | | 尹正杰 | boy | | NULL | 北京 |
- | | yinzhengjie | boy | | NULL | shanxi |
- | | Jason Yin | boy | | | beijing |
- | | Jay | boy | | | Taiwan |
- | | Gloria Tang Tsz-Kei | girl | | NULL | Hong Kong |
- +----+---------------------+------+------+--------+-----------+
- rows in set (0.00 sec)
- MariaDB [devops]>
- MariaDB [devops]> QUIT
- Bye
- [root@node103.yinzhengjie.org.cn ~]#
- [root@node103.yinzhengjie.org.cn ~]#
MySQL/MariaDB数据库的主主复制的更多相关文章
- MySQL/MariaDB数据库的半同步复制
MySQL/MariaDB数据库的半同步复制 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.MySQL半同步复制概述 1>.MySQL默认的异步复制 默认情况下,M ...
- MySQL/MariaDB数据库的主从级联复制
MySQL/MariaDB数据库的主从级联复制 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.主从复制类型概述 1>.主从复制 博主推荐阅读: https://ww ...
- MySQL/MariaDB数据库的复制监控和维护
MySQL/MariaDB数据库的复制监控和维护 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.清理日志 1>.删除指定日志文件名称之前的日志(也可用基于时间) M ...
- MySQL/MariaDB数据库的复制加密
MySQL/MariaDB数据库的复制加密 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.MySQL的安全问题 1>.基于SSL复制 在默认的主从复制过程或远程连接 ...
- MySQL/MariaDB数据库的复制过滤器
MySQL/MariaDB数据库的复制过滤器 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.复制过滤器概述 1>.复制器过滤器功能 让从节点仅复制指定的数据库,或指 ...
- MySQL/MariaDB数据库的Galera高可用性集群实战
MySQL/MariaDB数据库的Galera高可用性集群实战 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Galera Cluster概述 1>.什么是Gale ...
- MySQL/MariaDB数据库的MHA实现高可用实战
MySQL/MariaDB数据库的MHA实现高可用实战 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.MySQL高可用常见的解决方案 1>.Multi-Master ...
- MySQL/MariaDB数据库的PROXY实现读写分离
MySQL/MariaDB数据库的PROXY实现读写分离 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.ProxySQL概述 1>.各家互联网公司读写分离的解决方案 m ...
- MySQL/MariaDB数据库的主从复制
MySQL/MariaDB数据库的主从复制 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.MySQL复制概述 1>.传统扩展方式 垂直扩展(也叫向上扩展,Sacle ...
随机推荐
- Centos7安装部署Rabbitmq教程
依赖关系: 版本依赖一定要提前看清楚. RabbitMQ相关版本依赖关系查看 https://www.rabbitmq.com/which-erlang.html 可以看到要求版本Erlang21.3 ...
- React全家桶+Material-ui构建的后台管理系统
一.简介 一个使用React全家桶(react-router-dom,redux,redux-actions,redux-saga,reselect)+Material-ui构建的后来管理中心. 二. ...
- LVDS接口液晶屏点屏流程
使用MStar的TSUM系列的芯片,主要是用来驱动LVDS的屏.在硬件设置无误的情况下(屏开关口.屏电压.PWM等),按照屏规格书上的参数配置屏参,就可以完成点屏了.大概分为几个步骤,确定屏供电电压, ...
- P4Merge的使用
(官网: https://www.perforce.com/products/helix-core-apps/merge-diff-tool-p4merge 可以作为一个stand alone app ...
- ASP.NET Core webapi json 返回时间格式问题
网站找了几个方案不好使,比如: 1: services.AddMvc().AddJsonOptions(opt => { opt.SerializerSettings.DateFormatStr ...
- Word 查找替换高级玩法系列之 -- 通配符大全B篇
未完 ...... 点击访问原文(进入后根据右侧标签,快速定位到本文)
- Python进阶----异常处理
Python进阶----异常处理 一丶错误和异常 错误: 语法错误(这种错误,根本过不了python解释器的语法检测,必须在程序执行前就改正) #语法错误示范一 if #语法错误示范二 ...
- python基础知识(二)
python基础知识(二) 字符串格式化 格式: % 类型 ---- > ' %类型 ' %(数据) %s 字符串 print(' %s is boy'%('tom')) ----> ...
- InheritedWidget and screen
self: import 'package:flutter/material.dart'; class GrantScreen { static double _width, _height; sta ...
- pandas-02 Series()和DataFrame()的区别与联系
pandas-02 Series()和DataFrame()的区别与联系 区别: series,只是一个一维数据结构,它由index和value组成. dataframe,是一个二维结构,除了拥有in ...