一、工作原理
1、master開啓bin-log日誌
2、master設置授權給其他主機來同步自己的bin-log日誌
3、slave通過授權的用戶密碼來同步master bing-log的內容至relay-bin中,再從relay-log中讀取並寫到自己的sql。
二、環境:
master:192.168.1.11
slave:192.168.1.12
mysql版本:版本要一致
2、環境:
master:192.168.1.11
slave:192.168.1.12
mysql版本:版本要一致
三、安裝mariadb
安裝前先卸載之前的版本
[root@localhost ~]# rpm -qa|grep mariadb|xargs rpm -e –nodeps
安裝
[root@localhost ~]# yum install mariadb mariadb-server –y
[root@localhost ~]# rpm -qa|grep mariadb
mariadb-libs-5.5.65-1.el7.x86_64
mariadb-5.5.65-1.el7.x86_64
mariadb-server-5.5.65-1.el7.x86_64
啓動
[root@localhost ~]# systemctl start mariadb
[root@localhost ~]# ps -ef|grep mariadb
mysql 18043 17881 7 13:33 ? 00:00:01 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --log-error=/var/log/mariadb/mariadb.log --pid-file=/var/run/mariadb/mariadb.pid --socket=/var/lib/mysql/mysql.sock
root 18085 2482 0 13:33 pts/1 00:00:00 grep --color=auto mariadb
四、關閉selinux及配置防火牆
[root@localhost ~]# vim /etc/selinux/config
SELINUX=disabled
[root@localhost ~]# setenforce 0
[root@localhost ~]# firewall-cmd --zone=public --add-port=3306/tcp --permanent
success
[root@localhost ~]# firewall-cmd --reload
success
[root@localhost ~]# firewall-cmd --zone=public --list-ports
3306/tcp
五、配置主從
1、主服務器
配置my.cnf
[root@localhost ~]# vim /etc/my.cnf #添加兩行
[mysqld]
log-bin=mariadb-bin
server-id=1
重啓
[root@localhost ~]# systemctl restart mariadb
查看狀態及配置同步用戶權限等
[root@localhost ~]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.65-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> show master status; 查看主狀態
+--------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+--------------------+----------+--------------+------------------+
| mariadb-bin.000001 | 245 | | |
+--------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
MariaDB [(none)]> show master logs; 查看主日誌
+--------------------+-----------+
| Log_name | File_size |
+--------------------+-----------+
| mariadb-bin.000001 | 245 |
+--------------------+-----------+
1 row in set (0.00 sec)
MariaDB [(none)]> grant replication slave on *.* to "tongbu"@"192.168.1.%" identified by "123456"; 授權,讓192.168.1.0網段的主機使用tongbu用戶來同步服務器上的所有服務器
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> FLUSH PRIVILEGES; 刷新權限
Query OK, 0 rows affected (0.00 sec)
2、從服務器
安裝步驟同上
配置my.cnf
[root@localhost ~]# vim /etc/my.cnf
[mysqld]
server-id=2 增加此一行
重啓
[root@localhost ~]# systemctl restart mariadb
開始同步
[root@localhost ~]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.65-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> change master to master_host="192.168.1.11",
-> master_user="tongbu",
-> master_password="123456",
-> master_log_file="mariadb-bin.000001",
-> master_log_pos=474;
Query OK, 0 rows affected (0.05 sec)
MariaDB [(none)]> slave start;
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.1.11
Master_User: tongbu
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mariadb-bin.000001
Read_Master_Log_Pos: 474
Relay_Log_File: mariadb-relay-bin.000002
Relay_Log_Pos: 531
Relay_Master_Log_File: mariadb-bin.000001
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: 474
Relay_Log_Space: 827
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: 1
1 row in set (0.00 sec)
同步成功
六、測試
主服務器創建數據庫tb
MariaDB [(none)]> create database tb charset utf8;
Query OK, 1 row affected (0.00 sec)
從庫查看,是否有tb庫的存在
測試成功
注、mysqldump 4條常用備份語句
語句一:備份單一庫
[root@localhost src]# mysqldump -uroot -B tb >tb.sql 有密碼需要加上-p
語句二:備份所有數據庫
[root@localhost src]# mysqldump -uroot --all-databases >all.sql
語句三:備份數據庫,且記錄pos點,如此,在從庫還原後,直接從pos點開啓同步
[root@localhost ~]# mysqldump -uroot -p -B tb --master-data >tb.sql
語句四、刷新日誌,導出事件與函數
mysqldump -uroot -p1 -B zabbix --master-data --flush-logs -E -R > zabbix.sql
-----------------end