mysql master-slave configuration

MySQL master-slave configuration

Master server settings

1. First create a user who can connect from the server and grant permissions (usually only read permissions are enough)

# mysql -u root -p
# 允许本地 IP 访问 localhost, 127.0.0.1
# insert into mysql.user(Host,User,Password) values('localhost','zhouz',password('1234'));
# 允许外网 IP 访问
# insert into mysql.user(Host,User,Password) values('%','zhouz',password('1234'));
# 刷新授权
# flush privileges;
# 创建数据库
# create database cmdb_v2 DEFAULT CHARSET utf8 COLLATE utf8_general_ci;
# 授予用户通过外网IP对于该数据库的全部权限
# grant all privileges on `zhouzdb`.* to 'zhouz'@'%' identified by '1234';
# 授予用户在本地服务器对该数据库的全部权限
# grant all privileges on `cmdb_v2`.* to 'zhouz'@'%' identified by '1234';
# 刷新权限
# flush privileges;

2. Find the configuration file my.cnf (or my.ini) of the main database, mine is in /data/mysql56/my.cnf, and add the following two lines

log_bin = mysql_bin #开启二进制日志
server_id = 1 #设置server-id

If no configuration file is found

View the directory where mysql reads my.cnf by default

If my.cnf using the specified directory is not set, mysql will read the my.cnf file in the root directory of the installation directory and the default directory when it is started.

/etc/my.cnf, /etc/mysql/my.cnf, /usr/local/etc/my.cnf, ~/.my.cnf These are the directories that mysql will search for my.cnf by default, the order of the first priority . If none of these directories exist, create one yourself, or copy one from elsewhere

命令
mysql --help|grep 'my.cnf'

输出
                      order of preference, my.cnf, $MYSQL_TCP_PORT,
/etc/my.cnf /etc/mysql/my.cnf /usr/local/etc/my.cnf ~/.my.cnf

This is the entire content of my my.cnf

# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html

[mysqld]

log_bin = mysql_bin #开启二进制日志
server_id = 1 #设置server-id

max_allowed_packet = 20M
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES 

3. Restart mysql, then check the main server binary file name and location

File is the binary file name

position is position

So far, we need to configure the master-slave, and everything needed on the master side has been obtained

at this time

Respective server ip, mysql account and password, binary file name and location of the main server

Set from server

1. Also find the configuration file of the slave server, just add the server_id line

server_id = 2 #设置server-id,这里的id不能和主的一样

2. Restart the server, enter mysql, execute the following statement, replace the data after the equal sign with the data we got from the server

CHANGE MASTER TO
MASTER_HOST='113.111.111.111',
MASTER_USER='root',
MASTER_PASSWORD='root',
MASTER_LOG_FILE='mysql_bin.000001',
MASTER_LOG_POS=442414;

3. If there is no error, our master-slave is completed

4. We execute start slave; turn on master-slave

start slave;

5. Execute SHOW SLAVE STATUS\G to check whether the master and slave are running normally

As shown in the figure, both must be yes to be considered normal. At this time, you can change the data of the master mysql at will. It's a surprise, the slave server should be automatically synchronized.

 SHOW SLAVE STATUS\G

Precautions

1. When the configuration is completed and Slave_IO_Running and Slave_SQL_Running are not all YES, there is an error message in the show slave status\G information, which can be corrected according to the error message.

2. When Slave_IO_Running and Slave_SQL_Running are not all YES, most of the problems are caused by inconsistent data.

3. There are db databases in both databases, but the first MySQL db has tab1, and the second MySQL db does not have tab1, it must not succeed.

4. The binary log name and location of the data have been obtained, and the data operation has been performed, resulting in the change of the POS. The previous POS is still used when configuring CHANGE MASTER.

5. After the slave is stopped, the data changes, and then start the slave. error. The correction method is to execute CHANGE MASTER again.

Guess you like

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