Principle and Construction of MySQL Master-Slave replication
one。 The working process of master-slave replication:
II. MySQL replication type
Replication based on SQL statement
Row-based replication
Hybrid replication
three。 Experimental environment
OS:CentOS 6.5 x64
Master:192.168.0.134
Slave:192.168.0.135
three。 Configure master-slave replication
1. Configure time synchronization
Master: configured as a time server
[root@master ~] # yum install ntp-y Editor / etc/ntp.conf add the following two lines: server 127.127.1.0fudge 127.127.1.0 stratm 8service ntpd start
Slave: synchronize master time
[root@slave] # yum install ntpdate-y [root@slave ~] # ntpdate 192.168.0.134 6 May 06:37:58 ntpdate [6653]: adjust time server 192.168.0.134 offset-0.469705 sec
two。 Install MySQL
Slave and master:
[root@master] # yum install mysql-server mysql- y [root@master ~] # / etc/rc.d/init.d/mysqld start [root@master ~] # chkconfig mysqld on [root@master ~] # mysqladmin-u root password "123.com"
3. Edit configuration file
Master:
Edit / etc/my.conf add the following lines: server-id=134 # set id, start binary log log-slave-update=true restart MySQL service [root@smaster~] # service mysqld restart from different log-bin=master-bin #
Slave:
Edit / etc/my.conf add the following lines server-id=135relay-log=relay-log-binrelay-log-index=slave-relay-bin.indexread-only=1 # here you can set mysql to read-only and restart the MySQL service [root@slave ~] # service mysqld restart without taking effect on root
3. Log in to mysql and authorize slave
Master:
Mysql > grant replication slave on *. * to 'slave'@'192.168.0.%' identified by' 123456The query OK, 0 rows affected (0.00 sec) mysql > flush privileges;Query OK, 0 rows affected (0.00 sec) mysql > show master status +-+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +- -+-+ | master-bin.000001 | 181 | +- -+ 1 row in set (0.00 sec)
# File: log name Position: offset
4. Log in to MySQL to configure synchronization
Slave:
Mysql > change master to master_host='192.168.0.134',master_user='slave',master_password='123456',master_log_file='master-bin.000001',master_log_pos=181;Query OK, 0 rows affected (0.12 sec) mysql > start slave Query OK 0 rows affected (0.00 sec) mysql > show slave status\ gateway * 1. Row * * Slave_IO_State: Waiting for master to send event Master_Host: 192.168.0.134 Master_ User: slave Master_Port: 3306 Connect_Retry: 60 Master_Log_File: master-bin.000001 Read_Master_Log_Pos: 181 Relay_Log_File: relay-log-bin.000002 Relay_Log_Pos: 252 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: 181 Relay_Log_Space: 405 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: 1 row in set (0.00 sec)
# check the synchronization status: Slave_IO and Slave_SQL are YES, which means the synchronization between master and slave is successful.
four。 test
1. Create a new database on master
Mysql > show databases;+-+ | Database | +-+ | information_schema | | mysql | | test | +-+ 3 rows in set (0.00 sec) mysql > create database guoxh charset 'utf8';Query OK, 1 row affected (0.00 sec) mysql > show databases +-+ | Database | +-+ | information_schema | | guoxh | | mysql | | test | +-+ 4 rows in set (0.00 sec)
two。 View the database on slave
Mysql > show databases;+-+ | Database | +-+ | information_schema | | guoxh | | mysql | | test | +-+ 4 rows in set (0.00 sec)
# if you can see the newly created database on the slave, the master-slave replication configuration is successful.