Install the mysql database, and call the shots (2)

Install the mysql database  according to the first article , and be the master and slave (1)  Install two mysql; after doing this, let's be the master and slave!

ip servers role
192.168.80.119 1 master
192.168.80.110 2 slave

Open the binlog log on the master and add the following configuration

vim /etc/my.cnf

[mysqld]

server-id=1 #服务器id (主从必须不一样)

log-bin=mysql-bin #打开日志(主机需要打开),这个mysql-bin也可以自定义,这里也可以加上路径

#作为主机的配置,可以不写,默认所有

binlog-do-db=                 #要给从机同步的库(多个写多行)

binlog-ignore-db=mysql #不给从机同步的库(多个写多行)

binlog-ignore-db=information_schema

binlog-ignore-db=performance_schema

binlog-ignore-db=sys

expire_logs_days=7 #自动清理 7 天前的log文件,可根据需要修改

restart the database

service mysqld restart

mysql -uroot -p

password

mysql> show variables like '%log_bin%';

 

Create a backup account in the master database: backup is the username, % means any remote address, the following means that the backup of any remote address with a password of 123456 can connect to the master host

mysql> grant replication slave on *.* to 'backup'@'%' identified by '123456';

mysql> use mysql

mysql> select user,authentication_string,host from user;

Restart the MySQL service and set a read lock, which means that you can only read and cannot update, in order to obtain a consistent snapshot

mysql> show master status\G

View the current binary log name and offset value on the master server. The file and position here should be consistent with those on the slave

Export the data of master (192.168.80.119) and import it into slave

master: export as many databases as there are

mysqldump -uroot -proot -hlocalhost test> /opt/test.bak

#如果要传所有就用*,如果传具体某一个,就写文件名

 scp /opt/*  [email protected]:/opt/

Configure slave

add configuration

vim /etc/my.cnf

[mysqld]

server_id=2

#加上以下参数可以避免更新不及时,SLAVE 重启后导致的主从复制出错。

read_only = 1

master_info_repository=TABLE

relay_log_info_repository=TABLE

#避免一些错误导致主从断开

slave-skip-errors = 1062,1032,1060

#relay_log_recovery=1 #从机禁止写

#super_read_only=1 #从机禁止写

 

Then import it into the mysql database. If the employees database on the slave does not exist, create it first, and then import it.

mysql> create database employees;

mysql -uroot -p123456 -hlocalhost employees < /opt/employees.bak

service mysqld restart

mysql -uroot -proot

#进入sql命令

mysql> stop slave;

mysql> change master to

-> master_host='192.168.80.119', #master的ip

-> master_user='backup', #备份用户名

-> master_password='123456', #密码

-> master_log_file='mysql-bin.000002', #上面截图,且要与master的参数一致

-> master_log_pos=9992929; #上面截图,且要与master的参数一致

mysql> start slave;

Check the status of the slave machine

mysql> show slave status \G

2 yes in the picture means the setting is successful

Turn off the read lock of the master database, and test, two yes, and the Read_Master_Log_Pos of the slave is consistent with the Position of the master, then ok.

The slave database may be smaller than the master database, and execute it several times to see if the data is consistent and changing, and it is close, because there is a delay in master-slave synchronization.

Create a new database on the main database and take a look,

master:

mysql> create database test1;

mysql> SHOW DATABASES;

slave:

mysql> SHOW DATABASES;

If test1 appears in the slave, it means that the master-slave synchronization is successful.

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325112516&siteId=291194637