標籤:nlog The you art 生產 cti rdp 分布式 _id
1、介紹
MySQL作為世界上使用最為廣泛的資料庫之一,免費是其原因之一。但不可忽略的是它本身的功能的確很強大。隨著技術的發展,在實際的生產環境中,由單台MySQL資料庫伺服器不能滿足實際的需求。此時資料庫叢集就很好的解決了這個問題了。採用MySQL分布式叢集,能夠搭建一個高並發、負載平衡的叢集伺服器(這篇部落格暫時不涉及)。在此之前我們必須要保證每台MySQL伺服器裡的資料同步。資料同步我們可以通過MySQL內部配置就可以輕鬆完成,主要有主從複製和主主複製。
2、環境
主:192.168.182.155 centos7.2
從:192.168.182.156 centos7.2
3、主從複製3.1、修改設定檔
修改主從伺服器的設定檔/etc/my.cnf,在mysqld中添加log-bin=mysql-bin開啟二進位檔案
主服務配置:
[[email protected] ~]# cat /etc/my.cnf## This group is read both both by the client and the server# use it for options that affect everything#[client-server]## This group is read by the server#[mysqld]# Disabling symbolic-links is recommended to prevent assorted security riskssymbolic-links=0log-bin=mysql-bin
server-id=1
innodb_flush_log_at_trx_commit=1
sync_binlog=1
binlog_ignore_db=mysql
## include all files from the config directory#!includedir /etc/my.cnf.d
從伺服器配置:
[[email protected] ~]# cat /etc/my.cnf[mysqld]datadir=/var/lib/mysqlsocket=/var/lib/mysql/mysql.sock# Disabling symbolic-links is recommended to prevent assorted security riskssymbolic-links=0# Settings user and group are ignored when systemd is used.# If you need to run mysqld under a different user or group,# customize your systemd unit file for mariadb according to the# instructions in http://fedoraproject.org/wiki/Systemd[mysqld_safe]log-error=/var/log/mariadb/mariadb.logpid-file=/var/run/mariadb/mariadb.pidlog-bin=mysql-bin## include all files from the config directory#!includedir /etc/my.cnf.d
3.2、開始構建主從複製
在192.168.182.155中建立一個192.168.182.156主機中可以登入的MySQL使用者
登陸主要資料庫
mysql -uroot -pwc20080512;
建立串連帳號和密碼
MariaDB [(none)]> CREATE USER ‘mysql12‘@ ‘192.168.182.156‘ IDENTIFIED BY ‘mysql12‘;
授權
MariaDB [(none)]> GRANT REPLICATION SLAVE ON *.* TO ‘mysql12‘@192.168.182.156 IDENTIFIED BY ‘mysql12‘;Query OK, 0 rows affected (0.02 sec)
MariaDB [(none)]> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.02 sec)
執行以下命令鎖定資料庫以防止寫入資料
MariaDB [(none)]> FLUSH TABLES WITH READ LOCK;Query OK, 0 rows affected (0.07 sec)
退出mysql命令列,匯出資料庫
mysqldump -uroot -pwc20080512 -B heruiguo wanghaixue > /opt/mysql.sql;
scp 資料檔案到從伺服器
scp /opt/mysql.sql 192.168.182.156:/opt
登陸從伺服器匯入資料
[[email protected] ~]# mysql -uroot -pwc20080512</opt/mysql.sql
編輯設定檔my.cnf,在[mysqld]下面加入:
server-id=2
重啟資料庫
systemctl restart mariadb.service
查看192.168.182.155 MySQL伺服器二進位檔案名與位置
MariaDB [(none)]> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000010 | 327 | wordpress | mysql |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
解鎖資料庫
MariaDB [(none)]> UNLOCK TABLES;Query OK, 0 rows affected (0.00 sec)
在從伺服器執行
[[email protected] ~]# mysql -uroot -pwc20080512
CHANGE MASTER TO
MASTER_HOST=‘192.168.182.155‘,
MASTER_USER=‘mysql12‘,
MASTER_PASSWORD=‘mysql12‘,
MASTER_PORT=3306,
MASTER_LOG_FILE=‘mysql-bin.000010‘,
MASTER_LOG_POS=327;
啟動slave進程。
MariaDB [(none)]> START SLAVE;Query OK, 0 rows affected, 1 warning (0.00 sec)
查看主從複製是否配置成功
MariaDB [(none)]> SHOW SLAVE STATUS\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.182.155 Master_User: mysql12 Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000010 Read_Master_Log_Pos: 723 Relay_Log_File: mariadb-relay-bin.000002 Relay_Log_Pos: 929 Relay_Master_Log_File: mysql-bin.000010 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: 723 Relay_Log_Space: 1225 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: 0Master_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: 11 row in set (0.00 sec)
主從配置完成
測試
在主伺服器上建立資料庫
MariaDB [heruiguo]> create database aaa;Query OK, 1 row affected (0.01 sec)MariaDB [heruiguo]> SHOW MASTER STATUS;+------------------+----------+--------------+------------------+| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |+------------------+----------+--------------+------------------+| mysql-bin.000012 | 624 | | mysql |+------------------+----------+--------------+------------------+1 row in set (0.00 sec)
在從庫上驗證
MariaDB [heruiguo]> show databases;+--------------------+| Database |+--------------------+| information_schema || aaa || heruiguo || mysql || performance_schema || wanghaixue |+--------------------+6 rows in set (0.00 sec)
mysql主從複製