1、主從服務器設置。mysql
主服務器Ip:172.16.22.6linux
從服務器ip:172.16.22.8sql
2、主從服務器都須要設置的數據庫
主從服務器下載安裝mariadb-5.5.36:vim
一、解壓安全
tar xf mariadb-5.5.36-linux-x86_64.tar.gz -C /usr/localbash
二、建立軟連接服務器
cd /usr/localide
ln -sv mariadb-5.5.36-linux-x86_64 mysqlspa
三、建立MySQL數據存放位置
mkdir /mydata/data -pv
mkdir /mydata/binlogs -pv
mkdir /mydata/reaylogs -pv (從服務器上建立)
四、建立mysql組,mysql用戶
groupadd -r mysql
useradd -r -g mysql -s /sbin/nologin mysql
五、修改目錄的屬主、屬組
chown -R mysql.mysql /mydata/*
六、修改mariadb安裝目錄的屬主、屬組
cd /usr/local/mysql
chown -R root.mysql ./
七、複製mariadb的配置文件和啓動文件
mkdir /etc/mysql
cd support-files
cp my-large.cnf /etc/mysql/my.cnf
cp mysql.server /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld
chkconfig --add mysqld
八、導入二進制文件
vim /etc/profile.d/mysqld.sh
export PATH=/usr/local/mysql/bin:$PATH
主服務器配置:
vim /etc/mysql/my.cnf 添加: datadir = /mydata/data 修改: log-bin=/mydata/binlogs/master-bin
從服務器配置
vim /etc/mysql/my.cnf 添加: datadir = /mydata/data relay-log = /mydata/reaylogs/relay-log 修改: #log-bin=mysql-bin #binlog_format=mixed server-id = 11
九、初始化MySQL數據庫
scripts/mysql_install_db --datadir=/mydata/data --user=mysql
3、主服務器須要另外設置的:
建立有複製權限的從服務器帳號:
MariaDB [(none)]> grant replication slave, replication client on *.* to repluser@'172.16.%.%' identified by 'replp@ss'; Query OK, 0 rows affected (0.01 sec) MariaDB [(none)]> flush privileges; Query OK, 0 rows affected (0.00 sec)
或者:
MariaDB [(none)]> grant replication slave, replication client on *.* to repluser@'172.16.22.8' identified by 'replp@ss';
Query OK, 0 rows affected (0.01 sec)
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
查看主節點的二進制日誌處在那個文件的那個位置:
MariaDB [(none)]> show master status; +-------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +-------------------+----------+--------------+------------------+ | master-bin.000001 | 495 | | | +-------------------+----------+--------------+------------------+ 1 row in set (0.00 sec)
4、從服務器須要另外設置的
使用有複製權限的帳號鏈接master
一、鏈接主服務器時若是報:
MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST='172.16.22.6',MASTER_USER='repluser',MASTER_PASSWORD='replp@ss',MASTER_LOG_FILE='master-bin.000001',MASTER_LOG_POS=495;
ERROR 1201 (HY000): Could not initialize master info structure; more error messages can be found in the MariaDB error log
說明master.info文件出錯。
解決方法:
MariaDB [(none)]> reset slave;
Query OK, 0 rows affected (0.00 sec)
做用:刪除/mydata/data/master.info文件
MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST='172.16.22.6',MASTER_USER='repluser',MASTER_PASSWORD='replp@ss',MASTER_LOG_FILE='master-bin.000001',MASTER_LOG_POS=495; Query OK, 0 rows affected (0.03 sec)
查看從節點狀態:
MariaDB [(none)]> show slave status\G *************************** 1. row *************************** Slave_IO_State: Master_Host: 172.16.22.6 Master_User: repluser Master_Port: 3306 Connect_Retry: 60 Master_Log_File: master-bin.000001 Read_Master_Log_Pos: 495 Relay_Log_File: relay-log.000001 Relay_Log_Pos: 4 Relay_Master_Log_File: master-bin.000001 Slave_IO_Running: No Slave_SQL_Running: No 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: 495 Relay_Log_Space: 245 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: NULL 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: 0 1 row in set (0.01 sec)
二、啓動IO_THREAD、SQL_THREAD線程:
MariaDB [(none)]> START SLAVE IO_THREAD; Query OK, 0 rows affected (0.00 sec) MariaDB [(none)]> START SLAVE SQL_THREAD; 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: 172.16.22.6 Master_User: repluser Master_Port: 3306 Connect_Retry: 60 Master_Log_File: master-bin.000001 Read_Master_Log_Pos: 495 Relay_Log_File: relay-log.000002 Relay_Log_Pos: 530 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: 495 Relay_Log_Space: 818 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)
主從複製完成。
5、驗證一下:
在主節點上建立數據庫testdb
MariaDB [(none)]> create database testdb;
Query OK, 1 row affected (0.00 sec)
在從節點上看testdb,是否建立成功。
MariaDB [(none)]> show databasts; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'databasts' at line 1 MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | | testdb | +--------------------+ 5 rows in set (0.00 sec)
查看從節點狀態:
MariaDB [(none)]> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 172.16.22.6 Master_User: repluser Master_Port: 3306 Connect_Retry: 60 Master_Log_File: master-bin.000001 Read_Master_Log_Pos: 582 Relay_Log_File: relay-log.000002 Relay_Log_Pos: 617 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: 582 Relay_Log_Space: 905 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)
附:
一、中止從節點複製功能方法:
MariaDB [(none)]> stop slave; Query OK, 0 rows affected (0.00 sec) MariaDB [(none)]> show slave status\G *************************** 1. row *************************** Slave_IO_State: Master_Host: 172.16.22.6 Master_User: repluser Master_Port: 3306 Connect_Retry: 60 Master_Log_File: master-bin.000003 Read_Master_Log_Pos: 582 Relay_Log_File: relay-log.000002 Relay_Log_Pos: 617 Relay_Master_Log_File: master-bin.000003 Slave_IO_Running: No Slave_SQL_Running: No
二、啓動從節點複製功能方法:
MariaDB [(none)]> start slave; Query OK, 0 rows affected (0.00 sec) MariaDB [(none)]> show slave status\G *************************** 1. row *************************** Slave_IO_State: Master_Host: 172.16.22.6 Master_User: repluser Master_Port: 3306 Connect_Retry: 60 Master_Log_File: master-bin.000003 Read_Master_Log_Pos: 582 Relay_Log_File: relay-log.000002 Relay_Log_Pos: 617 Relay_Master_Log_File: master-bin.000003 Slave_IO_Running: Yes Slave_SQL_Running: Yes
三、複製中應該注意的問題:
一、如何限制從服務器只讀?
更改slave的全局服務器變量read-only爲YES;
注意:此限制對於擁有SUPER權限用戶無效;
MariaDB [mysql]> SET GLOBAL read_only = 1;
[mysqld]
read_only = 1
阻止全部用戶執行寫操做:
MariaDB [mysql]> flush tables with read lock;
二、如何保證主從複製時的事務安全?
前提:mysql對二進制日誌事件數據會有緩衝;
在master上設置以下參數:
sync_binlog = 1