Simple configuration of mysql master slave server

  • 2020-05-07 20:34:29
  • OfStack

First, add logged-in user privileges on the main server:
GRANT REPLICATE SLAVE on *.* to 'username'@'host' identified by 'password'

Then set up the my.ini file for the primary server.
server-id =1 # primary server identity
log-bin # enables 2-base logging
binlog-do-bin =databasename # the name of the database to be backed up, and more than one can repeat this configuration process
binlog-ignore-db =databasename # database name not backed up, multiple can repeat this configuration process

Configuration from the server
server-id =2 # from the server id, range 1-- 2E 32-1
master-host =hostname/hostip # master server ip or hostname
master-user =username # login to the user name of the main server
master-password =password # login password for the main server
master-port = portid # running port number of the primary server
replicate-do-db =databasename # database names to synchronize, multiple can repeat the configuration process
replicate-ignore =db=databasename # out-of-sync database name, multiple can repeat this configuration
log-bin # starts binary logging from the server
Time to reconnect automatically after master-connect-retry = seconds # after disconnecting from the primary server
skip-start-slave # prevents the synchronization program from being started with the server

At this point, a simple master-slave server configuration is complete.
Commands to run on master server:
show master status; Check the running status of the primary server
show slave hosts; View the list of slave server hosts
Supplement:
Use show slave status on the slave server
Slave_IO_Running, No, IO_THREAD is not started, please execute slave start [IO_THREAD]
If Slave_SQL_Running is No, there is an error in the copy. If Last_error field is checked, slave start [SQL_THREAD] is executed.
View the Slave_IO_State field
An empty // copy is not started
Connecting to master// master is not connected
Waiting for master to send event// has been connected
Supplement: slave can be created using the LOAD DATA FROM MASTER statement. But there are constraints:
To be all MyISAM tables, the data tables must have SUPER permissions, and the replication users of master must have RELOAD and SUPER permissions.
Clean up existing log changes by performing RESET MASTER on master,
At this point, slave cannot start IO_THREAD because the master log cannot be found. Please clear the data directory
relay-log.info, hosname-relay-bin * restart mysql
The default files for relay log files are hostname-relay-bin.nnn and hostname-relay-bin.index. Available from the server
relay-log and -- relay-log-index options modified. There is also one relay-log.info relay information file available from the slave server
-- relay-log-info-file startup option to change the file name.
Two mysql are configured as master and slave
Relevant commands on the main server:
show master status
show slave hosts
show logs
show binlog events
purge logs to ''log_name''
purge logs before ''date''
reset master(old version flush master)
set sql_log_bin=
Relevant commands from the server:
slave start
slave stop
SLAVE STOP IO_THREAD // this thread writes the master log locally
SLAVE start IO_THREAD
SLAVE STOP SQL_THREAD // this thread applies the log written to the local to the database
SLAVE start SQL_THREAD
reset slave
SET GLOBAL SQL_SLAVE_SKIP_COUNTER
load data from master
show slave status(SUPER,REPLICATION CLIENT)
CHANGE MASTER MASTER_HOST=, MASTER_PORT=,MASTER_USER=, MASTER_PASSWORD= // dynamic change of master information
PURGE MASTER [before 'date'] deletes logs that have been synchronized at master

Related articles: