标签:like value permanent 这一 slave The apollo for 它的
MariaDB数据库的主从复制方案,是其自带的功能,并且主从复制并不是复制磁盘上的数据库文件,而是通过binlog日志复制到需要同步的从服务器上.
MariaDB数据库支持单向、双向、链式级联等不同业务场景的复制.在复制的过程中,一台服务器充当主服务器(Master),接收来自用户的内容更新,而一个或多个其他的服务器充当从服务器(slave),接收来自Master上binlog文件的日志内容,解析出SQL,重新更新到Slave,使得主从服务器数据达到一致.
主从复制的逻辑有以下几种:
1)一主一从,单向主从同步模式,只能在Master端写入数据;
2)一主多从,
3)双主复制逻辑架构,此架构可以在Master1或Master2进行数据写入,或者两端同时写入(特殊设置);
在生产环境中,MySQL主从复制都是异步的复制方式,即不是严格的实时复制,但是给用户的体验都是实时的.MySQL主从复制集群功能使得MySQL数据库支持大规模高并发读写成为可能,且有效的保护了服务器宕机的数据备份.
利用复制功能当Master服务器出现问题时,我们可以人工的切换到从服务器继续提供服务,此时服务器的数据和宕机时的数据几乎完全一致.复制功能也可用作数据备份,但是如果人为的执行drop,delete等语句删除,那么从库的备份功能也就失效了.
(1)master将改变记录到二进制日志(binary log)中(这些记录叫做二进制日志事件,binary log events);
(2)slave将master的binary log events拷贝到它的中继日志(relay log);
(3)slave重做中继日志中的事件,将改变反映它自己的数据;
systemctl status mariadb
systemctl stop mariadb
vi /etc/my.cnf
# 修改内容如下:
# server-id服务的唯一标识(主从之间都必须不同);
# log-bin启动二进制日志名称为mysql-bin
[mysqld]
server-id=1
log-bin=apollo-mysql-bin
systemctl start mariadb
[root@localhost etc]# mysql -uroot -p
MariaDB [mysql]> create user 'apollo'@'%' identified by 'apollo123';
Query OK, 0 rows affected (0.00 sec)
mysql> set global validate_password_policy=0;
MariaDB [mysql]> grant replication slave on *.* to 'apollo'@'%';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> select user,host,password from mysql.user;
+--------+-----------------------+-------------------------------------------+
| user | host | password |
+--------+-----------------------+-------------------------------------------+
| root | localhost | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
| root | localhost.localdomain | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
| root | 127.0.0.1 | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
| root | ::1 | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
| apollo | % | *F4C3606BB4D20E38BDAC60DD383666A1F3D72A67 |
+--------+-----------------------+-------------------------------------------+
6 rows in set (0.00 sec)
MariaDB [(none)]> show grants for apollo@'%';
+-----------------------------------------------------------------------------------------------------------------+
| Grants for apollo@% |
+-----------------------------------------------------------------------------------------------------------------+
| GRANT REPLICATION SLAVE ON *.* TO 'apollo'@'%' IDENTIFIED BY PASSWORD '*91310B9B3DD9D3B34E510A8C8BEE1096516C0C94' |
+-----------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
MariaDB [mysql]> flush table with read lock;
Query OK, 0 rows affected (0.02 sec)
MariaDB [(none)]> show master status;
+--------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+--------------------+----------+--------------+------------------+
| a-mysql-bin.000001 | 615 | | |
+--------------------+----------+--------------+------------------+
1 row in set (0.01 sec)
# File是二进制日志文件名,
# Position是日志开始的位置,后面从库会用到.
[root@localhost ~]# mysqldump -uroot -p --all-databases > /opt/root.sql
MariaDB [(none)]> show master status;
+--------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+--------------------+----------+--------------+------------------+
| a-mysql-bin.000001 | 615 | | |
+--------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
[root@localhost ~]# scp /opt/zdk.sql root@192.168.13.183:/opt/
The authenticity of host '192.168.13.183 (192.168.13.183)' can't be established.
ECDSA key fingerprint is SHA256:inppMYl6FXIzV/XOV6TTlSUmP4TY96mz6sujwtTY7nk.
ECDSA key fingerprint is MD5:69:b7:4c:cc:18:86:94:be:d9:63:4d:4c:cb:62:fa:67.
# 输入yes
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.13.183' (ECDSA) to the list of known hosts.
# 输入数据库密码
root@192.168.13.183's password:
zdk.sql 100% 467KB 5.0MB/s 00:00
mysql -uroot -p
source /opt/root.sql
# 这一步完成后,再登录数据库,如出现密码不正确,请输入主库数据库密码.
systemctl status mariadb
systemctl stop mariadb
vi /etc/my.cnf
[mysqld]
server-id=10
主库:
MariaDB [(none)]> show variables like 'server_id';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id | 1 |
+---------------+-------+
1 row in set (0.00 sec)
MariaDB [(none)]> show variables like 'log_bin';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_bin | ON |
+---------------+-------+
1 row in set (0.00 sec)
从库:
MariaDB [(none)]> show variables like 'server_id';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id | 10 |
+---------------+-------+
1 row in set (0.00 sec)
MariaDB [(none)]> show variables like 'log_bin';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_bin | OFF |
+---------------+-------+
1 row in set (0.01 sec)
# 主库IP地址:'192.168.13.189'
# 如果以下信息不知道,可以通过命令show master status;
change master to master_host='192.168.13.189',
master_user='apollo',
master_password='apollo123',
master_log_file='a-mysql-bin.000001',
master_log_pos=615;
MariaDB [(none)]> change master to master_host='192.168.13.189',
-> master_user='apollo',
-> master_password='apollo123',
-> master_log_file='a-mysql-bin.000001',
-> master_log_pos=615;
Query OK, 0 rows affected (0.05 sec)
show slave status\G;
start slave;
show slave status\G;
# 查看两条参数,确保主从正常
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
MariaDB [(none)]> unlock tables;
Query OK, 0 rows affected (0.00 sec)
# 这个是在主库里面执行的哦
MariaDB [(none)]> create database db2;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| db1 |
| db2 |
| information_schema |
| mysql |
| performance_schema |
+--------------------+
5 rows in set (0.00 sec)
暂时没啥好总结的,就先按照上面的步骤进行吧!如有问题,后续更正!
标签:like value permanent 这一 slave The apollo for 它的
原文地址:https://www.cnblogs.com/apollo1616/p/10314806.html