Mysql master-slave

       In daily life, a large amount of data of a company will be stored in the database. Of course, a database cannot guarantee the security and reliability of data. If the server fails one day, it cannot be solved immediately. This will cause certain losses to the company, so today our experiment will be a mysql double-click hot backup. No matter which mysql server is writing, modifying or adding any data, the other server will synchronize the data.

 1. Environmental requirements:

Two CentOS 6.5 system servers
Server A: 192.168.1.10
Server B: 192.168.1.20

 Second, server A configuration

[root@loaclhost ~]#yum -y install mysql mysql-server mysql-devel
[root@loaclhost ~]#service mysqld start            
[root@loaclhost ~]#chkconfig mysqld on
[root@loaclhost ~]#ckconfig --add mysqld
[root@loaclhost ~]#mysqladmin -u root password 123456
[root@loaclhost ~]#mysql -u root -p123456 // Login to the database
mysql>grant replication slave,file on *.* to 'B'@'192.168.1.20' identified by '123456';   //授权B
mysql>flush privileges; // refresh privileges
mysql>exit
[root@loaclhost ~]#service mysqld stop //Stop the database
[root@loaclhost ~]#vim /etc/my.cnf
#Modify and add the My.cnf configuration file as follows:
log-bin=mysql-bin
server-id = 1 //The IDs of the master and slave cannot be the same
binlog-do-db=test //I am here to enter the hot standby for the test database
binlog-ignore-db=mysql
replicate-do-db=test
replicate-ignore-db=mysql
log-slave-updates=true
slave-skip-errors=all
skip-name-resolve
sync_binlog=1
auto_increment_increment=2
auto_increment_offset=1
[root@loaclhost ~]#service mysqld start
[root@loaclhost ~]#mysql -u root -p123456
mysql>flush tables with read lock; //Lock the table
mysql>show master status;
#Display the following: A
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 |      106 | test         | mysql            |
+------------------+----------+--------------+------------------+
mysql>change master to //The following content is first show master status; view server B
-> master_host='192.168.1.20',    
-> master_user='B',
-> master_password='123456',
->master_log_file='mysql-bin.000001',
->master_log_pos=106;
mysql>start slave; //Start slave
mysql>show slave status\G; //Check whether the IO process and SQL process are enabled
#Display the following content IO SQL process must be YES to succeed
         Relay_Log_File: mysqld-relay-bin.000002
                Relay_Log_Pos: 251
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: test
          Replicate_Ignore_DB: mysql


 3. Server B configuration

[root@loaclhost ~]#yum -y install mysql mysql-server mysql-devel
[root@loaclhost ~]#service mysqld start            
[root@loaclhost ~]#chkconfig mysqld on
[root@loaclhost ~]#ckconfig --add mysqld
[root@loaclhost ~]#mysqladmin -u root password 123456
[root@loaclhost ~]#mysql -u root -p123456 // Login to the database
mysql>grant replication slave,file on *.* to 'A'@'192.168.1.10' identified by '123456';   //授权A
mysql>flush privileges; // refresh privileges
mysql>exit
[root@loaclhost ~]#service mysqld stop //Stop the database
[root@loaclhost ~]#vim /etc/my.cnf
#Modify and add the My.cnf configuration file as follows:
log-bin=mysql-bin
server-id       = 2
binlog-do-db=test //I am here to enter the hot standby for the test database
binlog-ignore-db=mysql
replicate-do-db=test
replicate-ignore-db=mysql
log-slave-updates=true
slave-skip-errors=all
skip-name-resolve
sync_binlog=1
auto_increment_increment=2
auto_increment_offset=1
[root@loaclhost ~]#service mysqld start
[root@loaclhost ~]#mysql -u root -p123456
mysql>flush tables with read lock; //Lock the table
mysql>show master status;
#Display the following: A
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 |      106 | test         | mysql            |
+------------------+----------+--------------+------------------+
mysql>change master to //The following content is first show master status; view server A
-> master_host='192.168.1.10',    
-> master_user='B',
-> master_password='123456',
->master_log_file='mysql-bin.000001',
->master_log_pos=106;
mysql>start slave; //Start slave
mysql>show slave status\G; //Check whether the IO process and SQL process are enabled
#Display the following content IO SQL process must be YES to succeed
         Relay_Log_File: mysqld-relay-bin.000002
                Relay_Log_Pos: 251
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: test
          Replicate_Ignore_DB: mysql

 

Fourth, test A server
Release the respective locks, and then perform the data insertion test
mysql>unlock tables;
mysql>use test # (switch to test database)
mysql>create table t11_replicas
-> (id int not null auto_increment primary key,
-> str varchar(255) not null) engine myisam;

mysql>insert into t11_replicas(str) values
-> ('This is a master to master test table');

mysql>show tables;
Now we can view a table named t11_replicas, we can also view this table when we switch to server B and use the view command.
 5. Server B test
Release the respective locks, and then perform the data insertion test
mysql>unlock tables;
mysql>use test # (switch to test database)
mysql>create table t22_replicas
-> (id int not null auto_increment primary key,
-> str varchar(255) not null) engine myisam;

mysql>insert into t11_replicas(str) values
-> ('This is a master to master test table');

mysql>show tables;
Now we can view a table named t22_replicas, we can also view this table when we switch to server A and use the view command.
 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326991308&siteId=291194637