MYSQL主从配置
1.1 部署环境
主(master_mysql): 192.168.1.200 OS:CentOS 6.5
从(slave_mysql): 192.168.1.201 OS:CentOS 6.5
1.2 安装mysql
主和从: yum install mysql-server
1.3 配置
1.3.1 主配置(master_mysql配置)
vim
/etc/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]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
server-id=200
#设置主服务器的ID
innodb_flush_log_at_trx_commit=2
sync_binlog=1
#开启binlog日志同步功能
log-bin=mysql-bin-200
#binlog日志文件名
# Recommended in standard MySQL setup#binlog-
do
-db=xxxx
# 这个表示只同步某个库 (如果没有此项,表示同步所有的库)
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
~
1.3.2 配置完后,重启主库的mysql
service mysqld restart mysql -uroot -p ##登录mysql mysql>grant replication slave on *.* to 'mark'@'192.168.1.201' identified by '123456'; ##授权给从数据库服务器192.168.1.201,用户名mark,密码123456 mysql>show master status ; ##查看主库的状态 file,position这两个值很有用。要放到slave配置中 mysql> show master status; +----------------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +----------------------+----------+--------------+------------------+-------------------+ | mysql-bin-200.000001 | 120 | | | | +----------------------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec)1.3.3 配置从库服务器
vim /etc/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] # # Remove leading # and set to the amount of RAM for the most important data # cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%. # innodb_buffer_pool_size = 128M # # Remove leading # to turn on a very important data integrity option: logging # changes to the binary log between backups. # log_bin # # Remove leading # to set options mainly useful for reporting servers. # The server defaults are faster for transactions and fast SELECTs. # Adjust sizes as needed, experiment to find the optimal values. # join_buffer_size = 128M # sort_buffer_size = 2M # read_rnd_buffer_size = 2M datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 server-id=132 innodb_flush_log_at_trx_commit=2 sync_binlog=1 log-bin=mysql-bin-201 # Recommended in standard MySQL setup sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES [mysqld_safe] log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid
1.3.4 配置完后,重启从库的mysql
service mysqld restart mysql -uroot -p mysql> change master to master_host='192.168.1.200', master_user='mark' ,master_password='123456', master_log_file='mysql-bin-200.000002' ,master_log_pos=1167; mysql> start slave; ##开启从库 (stop slave:关闭从库) mysql> show slave status; ###Slave_IO_Running,Slave_SQL_Running 都为Yes的时候表示配置成功 mysql> show slave status \G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.217.131 Master_User: slave131 Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin-200.000001 Read_Master_Log_Pos: 120 Relay_Log_File: mysqld-relay-bin.000002 Relay_Log_Pos: 287 Relay_Master_Log_File: mysql-bin-200.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: 120 Relay_Log_Space: 461 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: 0 Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 0 Last_IO_Error: Last_SQL_Errno: 0 Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 131 Master_UUID: 3bcf2115-7056-11e7-b4b8-000c29a14a4a Master_Info_File: /var/lib/mysql/master.info SQL_Delay: 0 SQL_Remaining_Delay: NULL Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it Master_Retry_Count: 86400 Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: Executed_Gtid_Set: Auto_Position: 0 1.4:验证主从可以在主库上创建一个库,再在从库上刷新查看是否同步
参考原文:http://www.cnblogs.com/amuge/p/5863115.html