主从
重置数据库
#service mysqld stop
#rm -rf /usr/local/mysql/data/*
#/usr/local/mysql/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql -datadir=/usr/local/mysql/data
主从库均为刚初始化的数据库
单主多备
ip server-id
master 192.168.0.1 20
slave1 192.168.0.2 50
slave2 192.168.0.3 100
主库
#vim /etc/my.cnf
log-bin=master-bin
server-id=20
#service mysqld start
#mysql
mysql>reset master;
mysql>grant replication slave,replication client on *.*
->to rep@'192.168.0.%' identified by '123456';
mysql>flush privileges;
备库
测试账号
#mysql -h 192.168.0.1 -urep -p123456
#vim /etc/my.cnf
#mysql
mysql>reset master;
mysql>change master to
->master_host='192.168.0.1',
->master_user='rep',
->master_password='123456',
->master_log_file='master-bin.0000001',
->master_log_pos=0;
mysql>show slave status\G
mysql>start slave;
mysql>show slave status\G
测试:
master
mysql>show processlist\G
mysql>create database bbs;
mysql>create database bbs.t1(id int);
mysql>insert into bbs.t1 values(1);
mysql>select * from bbs.t1;
salve
mysql>show processlist\G
mysql>select * from bbs.t1;
针对运行一段时间 来实现主备
IP设置和上面的一样
主库
#vim /etc/my.cnf
log-bin=master-bin
service-id= 20
#service mysqld restart
#mysql
mysql>grant replication slave,replication client on *.*
->to rep@'192.168.0.%' indentified by '123456';
mysql>flush privileges;
初始化备库
主库
mysql>flush tables with read lock;
#mysqldump --all-databases > all.sql
#mysql -e 'show master status'
mysql>unlock tables;
#rsync -va all.sql 192.168.0.2:/
备库
#vim /etc/my.cnf
server-id=50
#service mysqld start
#mysql
mysql>reset master;
mysql>source /all.sql
mysql>change master to master_host='192.168.0.1',master_user='rep',master_password='123456',master_log_file='master-bin.0000001',master_log_pos=699;
mysql>start slave;
mysql>show slave status\G
mysql主主同步
重置数据库
#service mysqld stop
#rm -rf /usr/local/mysql/data/*
#/usr/local/mysql/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
1.mysql1 192.168.1.4
#vim /etc/my.cnf
log-bin=mysql-bin
server-id=4
#service mysqld start
#mysql
mysql>reset master;
mysql>grant replication slave,replication client on *.*
->to rep@'192.168.1,%' identtified by '123456';
mysql>flush privileges;
mysql>change master to
->master_host='192.168.1.251',
->master_user='req',
->master_password='123456'
->master_log_file='mysql-bin.0000001',
->master_log_pos=0;
mysql>show slave status\G
2.mysql2 192.168.1.251
#vim /etc/my.cnf
log-bin=mysql-bin
server-id=251
#service mysqld start
#mysql
mysql>reset master;
mysql>grant replication salve,replication client on *.*
->to rep@'192.168.1.%' identified by '123456';
mysql>flush privileges;
mysql>change master to
->master_host='192.168.1.4',
->master_user='rep',
->master_password='123456',
->master_log_file='mysql-bin.0000001',
->master_log_pos=0;
mysql>show slave status\G
在mysql1和mysql2上
mysql>slave start;
mysql>show slave status\G
测试
建立用户客户连接用户
mysql>grant ALL on *.* to admin@'192.168.1.%' identified by '123456';
mysql>flush privileges;