雙節點環境
主服務器節點:192.168.200.4 node-1
從服務器節點:192.168.200.5 node-2
數據庫版本:10.1.30
安裝mariadb
[root@node-1 ~]# yum install -y mariadb-server
[root@node-2 ~]# yum install -y mariadb-server
主服務器配置
[root@node-1 ~]# vi /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
innodb_file_per_table=NO
log-bin=/var/lib/mysql/master-bin #log-bin沒指定存儲目錄,則是默認datadir指向的目錄
binlog_format=mixed
server-id=200 #每個服務器都需要添加server_id配置,各個服務器的server_id不能重複。
symbolic-links=0
[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid
#啓動mariadb
[root@node-1 ~]# systemctl start mariadb
#設置mysql登陸密碼
[root@node-1 ~]# mysql_secure_installation
Enter current password for root (enter for none): #按回車
Set root password? [Y/n] y
New password: #設置密碼
Re-enter new password: #確認密碼
Remove anonymous users? [Y/n] y #刪除匿名用戶
Disallow root login remotely? [Y/n] n #遠程禁止root登陸
Remove test database and access to it? [Y/n] y #刪除測試數據庫並訪問它
Reload privilege tables now? [Y/n] y #現在重新加載特權表
#登陸數據庫
[root@node-1 ~]# mysql -uroot -p000000
#創建帳號並賦予replication的權限
MariaDB [(none)]> GRANT REPLICATION SLAVE ON *.* TO 'root'@'192.168.200.%' IDENTIFIED BY '000000';
Query OK, 0 rows affected (0.00 sec)
#備份數據庫數據,用於導入到從數據庫中
MariaDB [(none)]> show master status;
+-------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| master-bin.000001 | 1502 | | |
+-------------------+----------+--------------+------------------+
1 row in set (0.00 sec) #記住file和position 從服務器要用到
[root@node-1 ~]# mysqldump -uroot -p000000 --all-databases > mysql.sql #導出數據庫
[root@node-1 ~]# ls
mysql.sql
#將mysql.sql複製到從服務器上
[root@node-1 ~]# scp mysql.sql node-2:/root/
mysql.sql 100% 466KB 466.4KB/s 00:00
從服務器配置
[root@node-2 ~]# vi /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
symbolic-links=0
innodb_file_per_table=NO
server-id=201
relay-log=/var/lib/mysql/relay-bin
[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid
#啓動mariadb
[root@node-2 ~]# systemctl start mariadb
#設置登陸密碼,和上面一樣
[root@node-2 ~]# mysql_secure_installation
#導入主服務器的數據庫
[root@node-2 ~]# mysql -uroot -p000000 < mysql.sql
#登陸數據庫
[root@node-2 ~]# mysql -uroot -p000000
#設置主從複製
MariaDB [(none)]> change master to master_host='192.168.200.4',master_user='root',master_password='000000',master_log_file='master-bin.000001',master_log_pos=1502;
Query OK, 0 rows affected (0.18 sec)
#這裏的master_log_file,,master_log_pos就是剛纔主服務器上面的file和position內容
#master_host設置當前服務器爲主服務器(192.168.200.4)的從庫
#開啓主從複製
MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.01 sec)
#查看從庫狀態
MariaDB [(none)]> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.200.4
Master_User: root
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-bin.000001
Read_Master_Log_Pos: 1502
Relay_Log_File: relay-bin.000002
Relay_Log_Pos: 538
Relay_Master_Log_File: master-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: 1502
Relay_Log_Space: 830
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: 200
Master_SSL_Crl:
Master_SSL_Crlpath:
Using_Gtid: No
Gtid_IO_Pos:
Replicate_Do_Domain_Ids:
Replicate_Ignore_Domain_Ids:
Parallel_Mode: conservative
1 row in set (0.00 sec)
#結果中Slave_IO_Running和Slave_SQL_Running必須爲Yes
驗證
#主服務器
[root@node-1 ~]# mysql -uroot -p000000
MariaDB [(none)]> create database test1;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> use test1;
Database changed
MariaDB [test1]> create table user (id int);
Query OK, 0 rows affected (0.33 sec)
MariaDB [test1]> show tables;
+-----------------+
| Tables_in_test1 |
+-----------------+
| user |
+-----------------+
1 row in set (0.00 sec)
MariaDB [test1]> desc user;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
+-------+---------+------+-----+---------+-------+
1 row in set (0.00 sec)
從服務器驗證
[root@node-2 ~]# mysql -uroot -p000000
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test1 |
+--------------------+
MariaDB [(none)]> use test1;
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 [test1]> show tables;
+-----------------+
| Tables_in_test1 |
+-----------------+
| user |
+-----------------+
1 row in set (0.00 sec)
MariaDB [test1]> desc user;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
+-------+---------+------+-----+---------+-------+
1 row in set (0.00 sec)
可以看到從服務器更新了主服務器的數據。