mysql主从复制
mysql主从复制用途:实时灾备,用于故障切换
读写分离,提供查询服务
备份,避免影响业务
主从部署必要条件:
主从mysql版本一致主库开启binlog日志(设置log-bin参数)
主从server-id不同
从库服务器能连通主库
主从复制原理:
主sql上面开启bin_log日志功能。它会记录sql的数据信息,以二进制的方式存储在磁盘内,从服务器通过i/o线程请求读取主服务器的bin_log二进制日志,并将得到的日志信息写到relay log(中继日志)文件中;从库的sql线程读取relay log文件中的日志,解析成具体的sql语句操作,最终实现数据同统一,完成主从复制。
环境介绍:HostNameOSIP作用
mastercentos6.5192.168.100.150担任mysql主服务器
salvecentos6.5192.168.100.151担任mysql从服务器
ftpcentos6.5192.168.100.100担任ftp为主从提供yum源,软件支持(可以使用公网yum源代替此主机)
1:主从安装mysql:[root@master ~]# yum -y install mysql-server
[root@slave ~]# yum -y install msyql-server
2:修改主从的配置文件,以支持bin_log日志记录
[root@master ~]# vi /etc/my.cnf
7 log-bin=mysql-bin ##支持bin-log日志记录,bin-log日志文件名以mysql-bin开头
8 server-id=150 ##服务的唯一标识符号,默认是1,这里方便记忆,我使用了ip最后一段
[root@slave ~]# vi /etc/my.cnf
7 server-id=151
[root@master ~]# /etc/init.d/mysqld start ##重启服务
[root@slave ~]# /etc/init.d/mysqld start
3:在主数据库上面授权给从复制的权限:
登陆服务器授权[root@master ~]# mysqladmin -uroot password 123123
[root@master ~]# mysql -uroot -p123123
mysql> grant replication slave on *.* to 'slave'@"192.168.100.%" identified by '123123';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges; ##刷新权限
Query OK, 0 rows affected (0.00 sec)
mysql>
查看主服务的bin-log日志文件信息:
需要记录file 和position两栏中内容:以查到的为准。mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000003 | 476 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
4:在从服务器上修改自己的master的数据库
登入数据库[root@slave ~]# mysqladmin -uroot password 123123
[root@slave ~]# mysql -uroot -p123123
设置从服务器读取master bin-log的相关信息mysql> change master to
-> master_host='192.168.100.150', ##master的ip
-> master_user='slave', ##授权允许复制的用户名
-> master_password='123123', ##授权允许复制密码
-> master_log_file='mysql-bin.000003', ##bin-log文件名,上一步在master上查到的信息
-> master_log_pos=476; ##偏移量,在master上查到的信息
Query OK, 0 rows affected (0.07 sec)
启动slavemysql> start slave;
Query OK, 0 rows affected (0.00 sec)
插卡slave状态:
##查到的状态这两个为yes,下面没有error错误就正常
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.100.150
Master_User: slave
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos: 706
Relay_Log_File: mysqld-relay-bin.000002
Relay_Log_Pos: 481
Relay_Master_Log_File: mysql-bin.000003
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 706
Relay_Log_Space: 637
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
1 row in set (0.00 sec)
ERROR:
No query specified
mysql>
5:测试:
在主数据库上新建库,查看库mysql>
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| test |
+--------------------+
3 rows in set (0.00 sec)
mysql> create database test_databases;
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| test |
| test_databases |
+--------------------+
4 rows in set (0.00 sec)
在从数据库上查看库:mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| test |
| test_databases |
+--------------------+
4 rows in set (0.00 sec)
(可以看到在主数据库上新建的库,复制过来了)
删除操作也是可以同步的:
主:mysql> drop database test_databases;
Query OK, 0 rows affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| test |
+--------------------+
3 rows in set (0.00 sec)
mysql>
从:mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| test |
+--------------------+
3 rows in set (0.00 sec)
mysql>