System environment and architecture
# Hostname system version mysql version ip address
mysqlMaster <a href= "https://www.linuxprobe.com/" title= "centos" target= "_blank"> CentOS </a> 7. 4 mysql5. 7 192.168. mysqlSlave centos7 1.42. 4 mysql5. 7 192.168.1.43 #vip: 192.168.1.41
Database installed in the master and slave, respectively,
yum install epel* -y && yum clean all && yum makecache
rpm -Uvh http://repo.mysql.com/mysql57-community-release-el7.rpm
yum clean all && yum makecache
yum install gcc gcc-c++ openssl-devel mysql mysql-server mysql-devel -y
Create a database file storage path
mkdir /data/mysql -p
chown -R mysql:mysql /data/mysql
Mysql configuration profiles
# Mysql configuration profiles on mysqlMaster
/etc/my.cnf vi
[mysqld] Server-the above mentioned id = 1 # globally unique, can not be the same for each
log-bin = MySQL-bin # log-bin will indicate on binary logging, mysql-bin represents the naming format of the log file , bin-generated mysql .0001 like relay- (after pulling the main mysql log, log generated from the library) log = mysql-relay-bin # log format specified relay replicate-wild-ignore-table = mysql % # specify the table or database that is not synchronized, is mysql library name.% means that all of the tables below, the user table showing the mysql.user replicate-wild-ignore-table = test not different in the mysql.% replicate . -wild-information_schema the ignore-the Table =% #-Wild- replicate . do-boke the Table =% # # library that represents the synchronization Note: Do not use the main library on binlog- do-db or binlog-ignore-db options also # Do not use replicate- from the library do-db or replicate-ignore-db options, as this may create problems for cross-database update failure. Replicate_wild_do_table and replicate_wild_ignore_table recommended two options to copy from the library to solve filtration problems datadir = / data / mysql socket = / data / mysql / mysql.sock user = mysql symbolic-links =
0 log- error = / var / log / mysqld. Log File-PID = / var / RUN / mysqld / mysqld.pid character- SEt- Server UTF8 = [MySQL] Socket = / Data / MySQL / mysql.sock default Character- - SET = UTF8 [Client] Socket = / Data / MySQL / mysql.sock default-character- SET = UTF8 = User password = the root-NCYD Tianyu @ 0791 # if this field is not written in the unit with a <a href = "https://www.linuxcool.com/" title = "command" target = "_blank"> to enter the mysql command </a> will get an error, the default prompt can not find the path /var/lib/mysql/mysql.sock
Mysql configuration profiles on mysqlSlave
vi /etc/my.cnf
[mysqld]
server-id = 2
log-bin = mysql-bin
relay-log = mysql-relay-bin
replicate-wild-ignore-table=mysql.%
replicate-wild-ignore-table=test.%
replicate-wild-ignore-table=information_schema.%
datadir=/data/mysql
socket=/data/mysql/mysql.sock
user=mysql
symbolic-links=0
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
character-set-server=utf8
[mysql]
socket=/data/mysql/mysql.sock
default-character-set=utf8 [client] socket=/data/mysql/mysql.sock default-character-set=utf8 user=root password=NCYD-tianyu@0791
# If you do not write on this field, in the unit with a command to enter the mysql will complain, suggesting /var/lib/mysql/mysql.sock can not find the default path, initialize the database (needs to be initialized generated in / data / mysql directory the necessary information)
# mysql5.7.7 after initialization method
mysqld --user = --initialize --datadir = MySQL / Data / MySQL # mysql5.7.7 previous method for initializing
the mysql_install_db --user = --datadir = MySQL / Data / MySQL respectively activate the main from the database # in centos7 which must be turned off se <a href="https://www.linuxprobe.com/" title="linux" target="_blank"> linux </a>, whether in not start mysqld systemctl start mysqld were mainly doing security reinforcement from mysql # found in the previous step, first start mysql, the system automatically generates a password (CAT /var/log/mysqld.log | grep Pass) mysql_secure_installation # use this command to change the root password random (modified is NCYD-tianyu @ 0791) manually sync data (if the current environment, have been the main data from new)
1: created on the master user database for replication, and authorized
mysql> the GRANT Of REPLICATION SLAVE the ON * *. To 'tongbu' @ '192.168.1.43' IDENTIFIED by '123456789'; 2: On the first main mysql lock table (so that all tables become read-only) MySQL> flush the tables with the Read lock; # do not exit the terminal, whether in the lock on the failure 3: re-open another command line terminal, using tools such as myqldump export data (or directory stores mysql directly packed data, sent from the machine) 4: export to copy the data from the machine, and create a new library and import data from each set, each set of the main (to achieve a dual master mode)
In mysqlMaster mysqlSlave will set their own primary server role
MySQL> Show Master Status; # to view the status of mysqlSlave (File name record, and Position)
Change Master to
MASTER_HOST = '192.168.1.43',
MASTER_USER = 'tongbu', master_password = '123456789', MASTER_LOG_FILE = 'MySQL-bin.000002 ', MASTER_LOG_POS = 1006; MySQL> start slave; # start the replication process slave side (some versions are: start slave;) MySQL> Show slave Status \ G # View slaves end of the I / O processes, and process SQL
From mysqlMaster will set their own primary server role
MySQL> Show Master Status; # to view the status of mysqlMaster (File name record, and Position)
Change Master to
MASTER_HOST = '192.168.1.42',
MASTER_USER = 'tongbu', master_password = '123456789', MASTER_LOG_FILE = 'MySQL-bin.000002 ', MASTER_LOG_POS = 1006; MySQL> start slave; # start the copying process on the slave end MySQL> Show Status slave \ G # end view slaves I / O process, and the process SQL
Verify synchronization
1: Create a library on the first authentication myssqlMaster, the existence of the mysqlSlave
2: Create a library on the second verification mysqlSlave, the existence of the mysqlMaster
Installation keepalived achieve VIP switching to achieve high availability
-Y keepalived the install yum
# creates the inspection check in mysql mysqlMaster and mysqlSlave < A the href = "https://www.linuxcool.com/" title = "script" target = "_blank"> script </ A>, and give execute permissions touch /etc/keepalived/mysql_check.sh chmod + x /etc/keepalived/mysql_check.sh
/etc/keepalived/mysql_check.sh vi # / bin / bash!
# slave_is = ($ (MySQL-uroot--pNCYD-Tianyu @ 0791 -e "Show Slave Status \ G" | grep. "Slave _ * _ Running" | awk ' {print $ 2} ')) in the new version of myql, the direct write the password on the command line, will conduct safety tips, the solution is to write it in the mysql configuration file in the [client] field, writing see above mysql profile
slave_is = ($ (MySQL -e "Show Slave Status \ G" | grep . "the Slave _ * _ Running" | awk '{Print $ 2}')) IF [ " $ {slave_is [0]}" = "Yes " -a " $ slave_is [. 1] "= " Yes "] the then Exit 0 the else Exit. 1 Fi
# Note, keepalived examination script vrrp_script, identified only return two values, 0 for normal, non-zero indicate abnormal (in writing this script is to pay attention), it is not normal to do related switch
Configuring keepalived, in order to achieve high availability
/etc/keepalived/keepalived.conf VI
global_defs {
notification_email {
[email protected]
[email protected]
[email protected]
}
notification_email_from [email protected]
smtp_server 192.168. 200.1
smtp_connect_timeout 30
the router_id of the LVS_DEVEL
vrrp_skip_check_adv_addr #vrrp_strict Note # : when you can not ping through virtual VIP, comment out this line is recommended vrrp_garp_interval 0 vrrp_gna_interval 0} {vrrp_script mysql_check script "/etc/keepalived/mysql_check.sh" # this script, if found to be executed, you can check his authority 777 is not possible, the log prompts insecurity, can be directly chmod + x /etc/keepalived/mysql_check.sh on the line
#script "shutdown -r now" # Alternatively, direct debugging command interval The 2 weight 2} {vrrp_instance VI_1 State the MASTER on the MASTER is #mysqlMaster; mysqlSlave is the interface ens192 virtual_router_id the BACKUP 52 is priority 100 on #mysqlMaster 100; on mysqlSlave advert_int is 90 . 1 nopreempt # configure VIP (Note: failover preemption mode, as far as possible closed, is enabled by default, this parameter only needs to be provided to the high priority) AUTH_TYPE the PASS AUTH_PASS authentication { 1111} {track_script # call as defined above check mfsmaster script virtual_ipaddress} {mysql_check 192.168. 1.41 / 24}} dev ens192
Start keepalived
systemctl start keepalived