mysql主从服务器配置

server01 192.168.0.1 主服务器

server02 192.168.0.2 从服务器


设置同步数据库centos


server01

#mysqladmin -u root -p create centos

mysql>grant all privileges on *.* to 'centos'@'192.168.0.%' identified by 'centos';

mysql>create table users(id int, name varhar(255));

mysql>insert into users(1, 'test1'),(2,'test2');

mysql>exit

#vi /etc/my.cnf

增加以下信息

server-id=1

log-bin=mysql-bin
binlog-do-db=centos              #需要备份的数据库名,可写多行
binlog-ignore-db=mysql        #不需要备份的数据库名,可写多行


#mysqldump -u root -p centos > centos.sql


server02


#mysqladmin -u root -p create centos

mysql>grant all privileges on *.* to 'centos'@'192.168.0.%' identified by 'centos';

#mysql -u root -p centos < centos.sql

#vi /etc/my.cnf

增加以下信息

server-id=2
log-bin=mysql-bin
master-host=192.168.0.1
master-user=centos
master-password=centos
master-port=3306
replicate-do-db=centos
replicate-ignore-db=mysql
master-connect-retry=60

#/etc/init.d/mysqld restart

#mysql -u root -p

mysql>change master to master_host='192.168.0.1', master_user='centos', master_password='centos', master_port=3306, master_log_file='mysql-bin.000001', master_log_pos=0;

mysql>start slave; //启动

mysql>show slave status \G; //查看状态

Slave_IO_Running: Yes
Slave_SQL_Running: Yes

看见以上2个就说明启动成功



mysql -u root -e "show master status" -s -p | tail -n 1 | awk {'print $1'}


你可能感兴趣的:(数据库)