说明:
配置后,主MySQL与从MySQL二者的数据会实时同步,保证一致。
一、MySQL主从配置:
1.配置主MySQL(141):
(1)修改my.cnf(cd /etc,vi my.cnf),保存并需重启MySQL:
log-bin=my_mysql #配置log-bin,指定文件的名字 server-id=1 #配置主server-id,默认为1
重启mysql:
[root@localhost etc]# service mysqld restart
(2)创建备份账户(登录:mysql -uroot -p):
mysql> create user 'yyh'@'%' identified by 'root123456';
(3)授权replication slave:
mysql> grant replication slave on *.* to 'yyh'@'%';
(4)刷新权限:
mysql> flush privileges;
(5)锁表:
mysql> flush tables with read lock;
(6)显示log-bin位置(File为my_mysql.000001,Position为155):
mysql> show master status;
(7)备份主数据到/root目录(另起一个窗口执行,执行完成后配置从MySQL):
[root@localhost ~]# mysqldump --all-databases --master-data > dbdump.db -uroot -p Enter password:
(8)释放锁表(等从MySQL加载dbdump.db后执行):
mysql> unlock tables;
2.配置从MySQL(142):
(1)修改my.cnf(cd /etc,vi my.cnf),需重启:
server-id=2 #从配置从server-id,与主不能重复
重启mysql:
[root@localhost etc]# service mysqld restart
(2)将141主机下的主MySQL的dbdump.db拷贝到当前root目录下:
[root@localhost ~] scp root@192.168.233.141:~/dbdump.db . Are you sure you want to continue connecting (yes/no)? yes root@192.168.233.141's password:
(3)加载dbdump.db:
[root@localhost ~] mysql < dbdump.db -uroot -p Enter password:
(4)在从MySQL上设置主MySQL的配置(登录:mysql -uroot -p):
mysql> change master to -> master_host='192.168.233.141', -> master_user='yyh', -> master_password='root123456', -> master_log_file='my_mysql.000001', -> master_log_pos=155;
(5)启动从模式:
mysql> start slave;