环境说明
主库服务器: 192.168.71.151,CentOS 7,MariaDB 10已安装,无应用数据。
从库服务器1: 192.168.71.152,CentOS 7,MariaDB 10已安装,无应用数据。
从库服务器2: 192.168.71.153,CentOS 7,MariaDB 10已安装,无应用数据。
3个服务器的MariaDB都正常运行。
MariaDB 10服务器的常规安装配置可参照《MariaDB/MySQL在 CentOS 6.6上的编译安装/二进制源码包+授权详解》。
各个服务器的共同配置
以下操作在各个数据库服务器配置文件的[mysqld]部分下执行,数据库配置文件路径假定为 /etc/my.cnf 。/etc/my.cnf 中有关于主从配置的一些说明,见my.cnf中# Replication Master Server (default)和# Replication Slave (comment out master section to use this)部分。
打开各个数据库服务器的配置文件 my.cnf
检查确保各个服务器的skip-networking这行是注释掉的。主从复制需要数据库服务器使用IP监听的方式,不然使用UNIX socket方式监听,其他服务器访问不到。
把bind-address指定为各个服务器网卡的绑定IP上。
即在配置文件的 #skip-networking行后面添加bind-address=192.168.71.x,在192.168.71.151上配置为bind-address=192.168.71.151,在192.168.71.152上配置为bind-address=192.168.71.152,在192.168.71.153上配置为bind-address=192.168.71.153。
配置server_id。server_id值为1到2的32次方-1的整数,每个服务器都需要添加server_id配置,各个服务器的server_id需要保证唯一性互不相同,实践中通常设置为服务器IP地址的最后一位,即分别设置为server_id=151,server_id=152,server_id=153。
上述配置完后调用service mysql reload重新加载配置文件。
配置 主服务器
以下操作在主服务器192.168.71.151的/etc/my.cnf上进行。
确保log-bin是启用的,即log-bin=mysql-bin是非注释状态的,log-bin没指定存储目录,则是默认datadir指向的目录,可登录MariaDB shell通过如下命令查看:
MariaDB [(none)]> show variables like 'datadir'; +---------------+-----------------+ | Variable_name | Value | +---------------+-----------------+ | datadir | /var/lib/mysql/ | +---------------+-----------------+
创建帐号并赋予replication的权限
从库 从主库复制数据时需要使用这个帐号进行
MariaDB [(none)]> GRANT REPLICATION SLAVE ON *.* TO 'slave_user'@'192.168.71.%' IDENTIFIED BY 'bigs3cret'; Query OK, 0 rows affected (0.00 sec)
查看主库binary log的文件位置
主库锁表操作,不让数据库进行写入操作
MariaDB [(none)]> FLUSH TABLES WITH READ LOCK; Query OK, 0 rows affected (0.00 sec)
记录主库log文件及其当前位置
MariaDB [(none)]> SHOW MASTER STATUS; +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000002 | 326 | | | +------------------+----------+--------------+------------------+
记住File和Position的部分,后面会用到
保持当前MariaDB shell终端处于打开状态,即保持主库处于锁定状态,如果关闭MariaDB shell会导致主库恢复非锁定状态
备份主库已有数据并导入从库,如果主库中有数据需要先备份并导入到从库中。使用新的终端窗口或终端模拟器Tab ssh登录192.168.71.151服务器,执行如下语句进行数据库备份操作
mysqldump -uroot -p --all-databases > databases.sql
解锁 主库
数据备份完成后,就可以释放主库上的锁:
MariaDB [(none)]> UNLOCK TABLES; Query OK, 0 rows affected (0.00 sec)
在 从服务器 上的操作
以下操作需要在从库192.168.71.152和192.168.71.153上都执行。
导入备份的主库数据
mysql -uroot -p < databases.sql
设置relay-log
my.cnf文件中添加一行relay_log=relay-bin,如果不设置,默认是按主机名 + “-relay-bin”生成relay log。
设置主从复制
关于MySQL5.5以后和MariaDB不能在my.cnf文件中配置主库信息的说明
传送门----点我
其他的一些配置信息可以参考
cat `whereis mariadb|awk '{print $2"/support-files/my-large.cnf"}'`
MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST='192.168.71.151',MASTER_PORT=3306,\ MASTER_USER='slave_user',MASTER_PASSWORD='bigs3cret',\ MASTER_LOG_FILE='mysql-bin.000002',MASTER_LOG_POS= 326; Query OK, 0 rows affected (0.24 sec)
这个命令完成以下几个任务:
设置当前服务器为192.168.71.151的从库
提供当前数据库(从库)从主库复制数据时所需的用户名和密码,即上面的
GRANT REPLICATION SLAVE ON *.* TO 'slave_user'@'192.168.71.%' IDENTIFIED BY 'bigs3cret';设置的指定从库开始复制主库时需要使用的日志文件和文件位置,即上面主库执行SHOW MASTER STATUS;显示结果中的File和Position
开启主从复制
MariaDB [(none)]> START SLAVE; Query OK, 0 rows affected (0.00 sec)
查看从库状态
MariaDB [(none)]> SHOW SLAVE STATUS\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.71.151 Master_User: slave_user Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000002 Read_Master_Log_Pos: 326 Relay_Log_File: relay-bin.000001 Relay_Log_Pos: 306 Relay_Master_Log_File: mysql-bin.000002 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: 0 Last_Error: Skip_Counter: 0 Exec_Master_Log_Pos: 565 Relay_Log_Space: 826 Until_Condition: None Until_Log_File: Until_Log_Pos: 0 Master_SSL_Allowed: No Master_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: 0 Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 0 Last_IO_Error: Last_SQL_Errno: 0 Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 151 Master_SSL_Crl: Master_SSL_Crlpath: Using_Gtid: No Gtid_IO_Pos: 1 row in set (0.00 sec)
结果中Slave_IO_Running和Slave_SQL_Running必须为Yes,如果不是,需要根据提示的错误修改。
测试主从复制是否正常
在主库192.168.71.151的MariaDB shell上创建表或修改数据,看是否从库也跟着更新,如果跟着更新则说明正常。
例如,假定主库上有数据库 newdatabase,在主库上执行
MariaDB [(none)]> use newdatabase; Database changed MariaDB [newdatabase]> create table test (id int unsigned auto_increment primary key); Query OK, 0 rows affected (1.07 sec)
在每个从库上执行
MariaDB [(none)]> use newdatabase; Database changed MariaDB [newdatabase]> show tables; +-----------------------+ | Tables_in_newdatabase | +-----------------------+ | test | +-----------------------+ 1 rows in set (0.00 sec)
您可以选择一种方式赞助本站
支付宝扫一扫赞助
微信钱包扫描赞助
赏