
mysql>GRANT REPLICATION SLAVE ON *.* TO ‘backup’@’10.6.172.3‘ IDENTIFIED BY ‘123456’;
赋予从机权限,有多台丛机,就执行多次
2.打开主机A的my.cnf,输入
server-id = 1 #主机标示,整数
log_bin = /var/log/mysql/mysql-bin.log #确保此文件可写
read-only =0 #主机,读写都可以
binlog-do-db =test #需要备份数据,多个写多行
binlog-ignore-db =mysql #不需要备份的数据库,多个写多行
3.打开从机B的my.cnf,输入
server-id = 2
log_bin = /var/log/mysql/mysql-bin.log
master-host =192.168.1.100
master-user =backup
master-pass =123456
master-port =3306
master-connect-retry=60 #如果从服务器发现主服务器断掉,重新连接的时间差(秒)
replicate-do-db =test #只复制某个库
replicate-ignore-db=mysql #不复制某个库
蓝色的参数在mysql5.5中已经废弃了,必须在slave上采用CHANGE MASTER TO来设置slave
Use the CHANGE MASTER TO command (fully described in our manual) -
# the syntax is:
#
# CHANGE MASTER TO MASTER_HOST= # MASTER_USER= # # where you replace # # # Example: # # CHANGE MASTER TO MASTER_HOST='125.5.12.1', MASTER_PORT=3306, # MASTER_USER='joe', MASTER_PASSWORD='secret'; MASTER_LOG_FILE=’LOGFILE’ 4.同步数据库 不用太费事,只把主从库都启动即可自动同步,如果不嫌麻烦的话可以把主库的内容导出成SQL,然后在从库中运行一遍 5.先重启主机A的mysql,再重启从机B的mysql 6.验证 在主机A中,mysql>show master status\\G; 在从机B中,mysql>show slave status\\G; 如果不复制,尝试启动主从模式,在从机中start slave. 报错:“ERROR 1200 (HY000): The server is not configured as slave; fix in config file or with CHANGE MASTER TO” 解决办法: (1)修改 server_id If your slave server does not accept your my.cnf you can set the server-id like this in your mysql client: SET GLOBAL server_id = 2; (but you should try to fix the problem with your my.cnf :] ) (2)重启slave mysql> stop slave; Query OK, 0 rows affected (0.00 sec) mysql> change master to master_host='192.166.63.42', master_user='repl', master_password='12345678', master_log_file='mysql-bin.000002', master_log_pos=98; Query OK, 0 rows affected (0.06 sec) mysql> start slave; Query OK, 0 rows affected (0.00 sec) mysql> show slave status\\G mysql的could not be resolved: Temporary failure in name resolution处理办法: 在mysql的配置文件my.cnf中 [mysqld] 节点添加下面两行 skip-host-cache skip-name-resolve
