Mysql主从同步+mysql-proxy读写分离

Mysql主从同步+mysql-proxy读写分离

如果对运维课程感兴趣,可以在b站上、csdn或微信视频号 上搜索我的账号: 运维实战课程,可以关注我,学习更多免费的运维实战技术视频

一、配置mysql服务器的主从结构 (开始时主从数据不一致时候需要找到临界点的主从同步

结果使得:在主mysql服务器创建一个库赋值,则在从mysql服务器上同时把数据同步过来。如果在从mysql建一个库和赋值,在主mysql上不会自动数据同步过来

在192.168.4.5(主

[root@localhost 桌面]# yum -y install mysql-server mysql

[root@localhost 桌面]# /etc/init.d/mysqld start

[root@localhost 桌面]# mysql

mysql> set password for root@"localhost"=password("123");

mysql> quit

[root@localhost 桌面]# mysql -uroot -p123           //主从同步前,建立初始数据(同步前数据不一致建立)

mysql> show databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| mysql              |

| test               |

+--------------------+

mysql> create database ku;

mysql> create table ku.biao (test varchar(5));

mysql> insert into ku.biao values ("test");

mysql> show databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| ku                 |

| mysql              |

| test               |

+--------------------+

mysql> show tables from ku;

+--------------+

| Tables_in_ku |

+--------------+

| biao         |

+--------------+

1 row in set (0.00 sec)

mysql> select * from ku.biao;

+------+

| test |

+------+

| test |

+------+

mysql> quit

[root@localhost 桌面]# vim /etc/my.cnf

server_id=5                                 //主从编号不能一致

log-bin                                       //开启bin-log

wq

[root@localhost 桌面]# mysql -uroot -p123 -e "show variables like 'log_bin'"

+---------------+-------+

| Variable_name | Value |

+---------------+-------+

| log_bin       | OFF   |                              //还没重启服务,配置文件没生效,重启后会开启

+---------------+-------+

[root@localhost 桌面]# ls /var/lib/mysql/

ibdata1  ib_logfile0  ib_logfile1  ku  mysql  mysql.sock  test             //没重启时没有bin-log日志

[root@localhost 桌面]# /etc/init.d/mysqld restart

[root@localhost 桌面]# mysql -uroot -p123 -e "show variables like 'log_bin'"

+---------------+-------+

| Variable_name | Value |

+---------------+-------+

| log_bin       | ON    |

+---------------+-------+

[root@localhost 桌面]# ls /var/lib/mysql/

ibdata1      ib_logfile1  mysql              mysqld-bin.index  test

ib_logfile0  ku           mysqld-bin.000001  mysql.sock

[root@localhost 桌面]# mysql -uroot -p123

mysql> grant replication slave on *.* to shi@"192.168.4.205" identified by "456";

mysql> flush privileges;

mysql> flush tables with read lock;        //把主库的数据先锁上,此时窗口不能断,另开一窗口,备份数据

[root@localhost 桌面]# mysql -uroot -p123                          //拿到那个临界点

mysql> show master status;

+-------------------+----------+--------------+------------------+

| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB |

+-------------------+----------+--------------+------------------+

| mysqld-bin.000001 |      329 |              |                  |

+-------------------+----------+--------------+------------------+

mysql> show master logs;

+-------------------+-----------+

| Log_name          | File_size |

+-------------------+-----------+

| mysqld-bin.000001 |       329 |

+-------------------+-----------+

mysql> quit

                                            //下面的:-A,备份所有,-B,有create语句

[root@localhost 桌面]# mysqldump -uroot -p123 -A -B --events --master-data=2 > /tmp/mysql.bak.sql

                                       //也可加-X,自动锁,就不需上面专门锁了,--master-data=2,取临界点

[root@localhost 桌面]# ls /tmp/

mysql.bak.sql

[root@localhost 桌面]# scp /tmp/mysql.bak.sql root@192.168.4.205:/tmp/

在192.168.4.205(从

[root@localhost 桌面]# yum -y install mysql-server mysql

[root@localhost 桌面]# vim /etc/my.cnf

log-bin                                             //单纯做从,也可不开启,如果做主必须开启

server_id=205

[root@localhost 桌面]# /etc/init.d/mysqld start

[root@localhost 桌面]# mysql

mysql> set password for root@"localhost"=password("123");

mysql> quit

[root@localhost 桌面]# mysql -uroot -p123

mysql> show databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| mysql              |

| test               |

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

运维实战课程

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值