赞
踩
IP | 简称 |
---|---|
192.168.94.21 | server21 |
192.168.94.31 | server31 |
如果是新安装的高版本MySQL,默认情况下是没有生成/etc/my.cnf这个文件的,那我们就在etc目录下新增一个my.cnf的配置文件,配置文件参数的意义可以参见博客,这里就不再赘述了。
[mysqld] sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES # 特别注意这个server-id,两台MySQL的不能设置成一样的哦 server-id=1 bind-address=0.0.0.0 port=3306 # 数据库安装目录 basedir=/usr/local/mysql # 数据文件存放目录 datadir=/data/mysqldata # 字符编码集 character_set_server=utf8 lower_case_table_names=1 binlog-format=mixed transaction_isolation=REPEATABLE-READ auto-increment-increment=2 #步长为2 auto-increment-offset=1 #奇数增长(两台server的配置不能一样哦) log-error=/usr/local/mysql/logs/err.log log-bin=/data/mysqldata/mysql-bin log-bin-index=/data/mysqldata/mysql-bin.index log_slave_updates=1 slow_query_log=1 slow_query_log_file=/usr/local/mysql/logs/slow-queries.log general_log=1 general_log_file=/usr/local/mysql/logs/general-log.log expire_logs_days=7 max_binlog_size=1024M # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 max_allowed_packet=128M max_connections=1000 #skip-name_resolve log_bin_trust_function_creators=1
[mysqld] sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES server-id=2 bind-address=0.0.0.0 port=3306 basedir=/usr/local/mysql datadir=/data/mysqldata character_set_server=utf8 lower_case_table_names=1 binlog-format=mixed transaction_isolation=REPEATABLE-READ auto-increment-increment=2 #步长为2 auto-increment-offset=2 #奇数增长 log-error=/usr/local/mysql/logs/err.log log-bin=/data/mysqldata/mysql-bin log-bin-index=/data/mysqldata/mysql-bin.index log_slave_updates=1 slow_query_log=1 slow_query_log_file=/usr/local/mysql/logs/slow-queries.log general_log=1 general_log_file=/usr/local/mysql/logs/general-log.log expire_logs_days=7 max_binlog_size=1024M # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 max_allowed_packet=128M max_connections=1000 #skip-name_resolve log_bin_trust_function_creators=1
service mysqld restart
如果是新安装的MySQL,登录到MySQL控制台需要先修改密码
设置数据库root用户密码
set password for root@localhost=password('123456');
mysql –u root –p
GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO slave@'196.168.94.31' IDENTIFIED BY "slave"; #创建同步账号
FLUSH PRIVILEGES; #权限修改立即生效
FLUSH TABLES WITH READ LOCK; #锁定数据库为只读,确保备份数据一致性
show master status;
CHANGE MASTER TO MASTER_HOST='196.168.94.31', MASTER_PORT = 3306, MASTER_USER='slave', MASTER_PASSWORD='slave', MASTER_LOG_FILE='mysql-bin.000002', MASTER_LOG_POS=1093;
GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO slave@'196.168.94.21' IDENTIFIED BY "slave"; #创建同步账号
FLUSH PRIVILEGES; #权限修改立即生效
FLUSH TABLES WITH READ LOCK; #锁定数据库为只读,确保备份数据一致性
show master status;
CHANGE MASTER TO MASTER_HOST='196.168.94.21', MASTER_PORT = 3306, MASTER_USER='slave', MASTER_PASSWORD='slave', MASTER_LOG_FILE='mysql-bin.000008', MASTER_LOG_POS=613;
start slave;
show slave status\G
查看同步状态信息,查看状态信息中的Slave_IO_Running和Slave_SQL_Running状态为YES即可,如图所示:unlock tables;
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。