MySQL Replication(Master与Slave基本原理及配置)mysql
4:搭建mysql主从服务器 (1)实验环境 操做系统:Red Hat Enterprise Linux Server release 6.4 数据库版本: 5.6.21 (2)主机地址: master:192.168.10.130 slave:192.168.10.120 (3)安装数据mysql数据库 略 (4)配置master的my.cnf文件 [mysqld] log_bin = mysql-bin #开启binlog日志 basedir = /usr/local/mysql #指定mysql的安装目录 datadir = /data/mysql #指定mysql数据库的数据存放位置 port = 3306 #指定端口,默认是3306,若是一个主机有两个数据库需指定不一样端口 server_id = 1 #指定server-id,必须与slave端的server-id不一样 bind-address = 0.0.0.0 #默认是127.0.0.1,更改成0.0.0.0,不然Slave将没法连接到 Master expire_logs_days = 10 #终止日志的时间,以天为单位,默认是30天 max_binlog_size = 100M #存放日志最大容量 sync-binlog=1 #容许日志同步 (5)重启mysql服务 [root@localhost ~]# service mysqld restart (6)本地登陆mysql数据库,向slave受权并查看当前使用的binlog日志与偏移量 mysql> grant replication client,replication slave on *.* to 'repluser'@'192.168.10.120' identified by '123456'; 语意:容许用户repluser以密码123456在192.168.10.120主机,登录本机数据库同步数据; (7)查看master状态 mysql> show master status \G; *************************** 1. row *************************** File: mysql-bin.000015 #binlog日志 Position: 211 #偏移量 Binlog_Do_DB: Binlog_Ignore_DB: Executed_Gtid_Set: 1 row in set (0.00 sec) ERROR: No query specified 错误显示: mysql>show master status; Empty set (0.02 sec) 查询若是出现以上提示表示配置有问题; (8)配置slave服务器my.cnf文件 [mysqld] basedir = /usr/local/mysql datadir = /data/mysql port = 3306 server_id = 2 #指定server-id,必须与master端的server-id不一样 sync-binlog=1 #容许日志同步 read-only=1 #设置只读 (9)重启mysql服务 [root@localhost ~]# service mysqld restart (10)本地登陆mysql数据库,指定从服务器的主服务器并开启slave功能 执行如下操做必须关闭slave功能 mysql> stop slave; Query OK, 0 rows affected (0.00 sec) mysql> change master to -> master_host='192.168.10.130', #指定主服务器地址 -> master_port=3306, #指定端口,默认是3306,不须要指定 -> master_user='repluser', #指定同步时使用的用户名 -> master_password='123456', #指定同步用户的密码 -> master_log_file='mysql-bin.000015', #指定当前的主服务器使用的binlog日志 -> master_log_pos=120; #指定当前主服务器上的位偏移量 Query OK, 0 rows affected (0.00 sec) mysql> start slave; Query OK, 0 rows affected (0.00 sec) (11)查看slave是否正常工做 mysql> show slave status \G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.10.130 Master_User: repluser Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000015 Read_Master_Log_Pos: 120 Relay_Log_File: localhost-relay-bin.000002 Relay_Log_Pos: 283 Relay_Master_Log_File: mysql-bin.000015 Slave_IO_Running: Yes Slave_SQL_Running: Yes (12)测试 在master建立wql数据 mysql> create database wql; Query OK, 1 row affected (0.00 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | | wordpress | | wql | +--------------------+ 6 rows in set (0.00 sec) 在slave数据查看是否有该数据库 mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | | wql | +--------------------+ 5 rows in set (0.00 sec)
Mysql主从同步搭建完成且能够正常工做,但其中有几点须要注意:sql
(1)主从单向复制,从服务器只是实时的保存了主服务器的一个副本。当主服务器发生故障时,能够切换到从服务器继续作查询,但不能更新。数据库
(2)若是采用双向复制,即两台mysql服务器即做为主服务器,又做为从服务器。那么二者均可以执行更新操做并能实现负载均衡,当一方出现故障时,另外一方不受影响。可是,除非能保证任何更新操做顺序都是安全的,不然双向复制会致使失败。安全