MySQL复制跟性能优化

MySQL复制跟性能优化

MySQL复制和性能优化

复制(Replication):通过复制执行过的语句或者数据集从主服务器上复制到一个或多个从服务器上实现多个数据库服务器之间数据库的同步。

MySQL’s built-in replication capability is the foundation for building large, highperformance applications on top of MySQL.

MySQL supports two kinds of replication: statement-based replication(基于语句的复制) and row-based replication(基于行的复制).

Statement-based (or “logical”) replication has been available since MySQL 3.23, and it’s what most people are using in production today

Row-based replication is new in MySQL 5.1.

Both kinds work by recording changes in the master’s binary log and replaying the log on the slave

基于语句的复制:记录改变数据库的语句,将语句在从服务器上在执行一遍,效率较高。

基于行的复制:将语句执行后的结果包括行的改变或者添加整个复制到从服务器中去。

混合方式的复制:由MySQL自动来判断。

复制过程:

主服务器上线程:mysql dump

从服务器两个线程:I/O thread ,SQL thread。

过程:从服务器的I/O 线程(主服务器的远程客户端)不断尝试连接主服务器,读取其二进制日志,主服务器收到请求后将检查自己的Binary Log并根据从服务器发来的Relay Log的相关信息来确认自从上次复制之后主服务器内容是否有更新,如果有,则主服务器启动mysql dump线程,将对方所请求的数据返回给从服务器,从服务器收到数据后会将数据保存在Relay Log。SQL thread 会不定期的读取Relay Log,如果发现有更新,则读取更新的语句或者行将其保存在从服务器上。

MySQL解决的问题:数据备份、负载均衡、高可用、数据分布(异地容灾)、升级测试。

下面我们来实现基于MySQL主从复制的架构:

Master:192.168.1.11? MySQL已安装完毕

[root@station39 ~]# vim /etc/my.cnf

log-bin=master-bin???? //** update? line 50

log-bin-index=master-bin.index? //** add line 51

server-id?????? = 1?????????? //** line 59

重启服务;

mysql> grant replication slave,replication client on *.* to rep@’192.168.1.%’ identified by ‘RedHat ‘;

Query OK, 0 rows affected (0.05 sec)

mysql> flush privileges;

Query OK, 0 rows affected (0.00 sec)

mysql> show processlistG;

OK!此时可以看到Binlog Dump线程已经启动,创建数据库,表试试:

mysql> create database mydb;

Query OK, 1 row affected (0.03 sec)

mysql> use mydb

Database changed

mysql> create table t1( id int unsigned not null primary key, name char(30));

Query OK, 0 rows affected (0.05 sec)

mysql> insert into t1 values(1,’lucy’),(2,’lily’);

Query OK, 2 rows affected (0.00 sec)

Records: 2? Duplicates: 0? Warnings: 0

mysql> select * from t1;

Slave:192.168.1.13? MySQl已安装完毕

[root@station26 ~]# vim /etc/my.cnf

server-id?????? = 2??? //** line 58

relay-log = slave-relay-bin????? //** line? 59

relay-log-index = slave-relay-bin.index? //** line 60

重启服务;

mysql> change master to

???? > master_host=’192.168.1.11′,

???? >master_port=3306,?????

???? > master_user=’rep’,

???? > master_password=’RedHat ‘;

mysql> start slave;

mysql> show processlistG;

OK,I/O thread,SQL thread 已经启动,等待主服务器数据发生更新……

主服务器上创建了mydb数据库和t1表,我们在从服务器中已经可以看到:

从Binary Log某一个点开始复制主服务器内容:

mysql> change master to

???? > master_host=’192.168.1.11′,

???? >master_port=3306,?????

???? > master_user=’rep’,

???? > master_password=’RedHat ‘,

???? > master_log_file=’master-bin.000001′,

???? >master_log_pos=698;

PS:如果主服务器运行一段时间后才新建一个从服务器做复制,需要先将主服务器上的数据进行备份,然后将备份数据发给从服务器并在从服务器上还原之后再做复制。

主服务器:

mysql> flush tables with read lock;

mysql> flush logs;

mysql> q

[root@station39~]# mysqldump -uroot –all-database –master-data=2 >/roo

MySQL复制跟性能优化

相关文章:

你感兴趣的文章:

标签云: