实验环境:
主服务器IP:192.168.1.165
从服务器IP:192.168.1.166
mysql内建的复制功能是mysql应用的“水平扩展”的架构,是mysql高性能运用的基础,为一个服务器配置一个或多个备库来进行数据的同步 (准备的说不应该说是“同步”,因为主从服务器的数据根本不可能做同步)。在这样一个分布式的架构中各个服务器间时间应该同步才是,能为后期日志的统计分 析提供更为准备的信息,这样才能准确反应出某个时间点我们系统的访问情况。
主从复制是一种异步复制的模型,说成“主从同步”是不精确的,应说是“主从复制”,先有主,再有从。因mysql主从复制原理的限定,在从库上只有一个 sql线程(即使在mysql5.6中支持多线程,但也是针对库级别的)来重放中继日志中的事件,这种在主服务器能并发的查询请求在从服务器就成了串行化 来执行,这是一个从服务器会慢于主服务器的一个原因。
对主从服务器在硬件选择上的建议:如果在规划主从时考虑到有可能会把从服务器提升为主服务器,那主从服务器硬件配置建议完全相同,应选择核多、主频高的 CPU,频率高、总容量大的内存,多块高速的SAS硬盘(或SSD)及带有BBU的RAID卡,在RAID级别上建议选择raid10或raid50;如 果从库只是用来作为主库的一个副本,以便于数据的备份,那从服务器的CPU没必要与主服务一个规格,但硬盘的IO这一块还是不要太差,IO高有助于事务的 重放效率,在一定程度上能缩小与主库的时间差。
2、主从时间同步
主服务器配置成一ntpd服务器,自身与外网的ntpd服务进行时间校准,而又对从服务器提供时间校准服务。在生产环境中最好不要用ntpdate来强行 调整服务器的时间,这样会对让服务器的时间产生空白区,注意,ntpdate工具是调整时间,而不是像ntpd工具一样是校准时间,两者的差别是挺大的。
安装配置主服务器成为ntpd时间服务器:
yum -y install ntp
service ntpd start
ntpq -p
[root@master ~]# yum -y install ntp
[root@master ~]# service ntpd start #启用服务后等个1-3分钟,就可用ntpq -p命令查看是否能与远程ntpd服务器连接
[root@master ~]# vi /etc/ntp.conf #配置ntp配置文件,使其成为从服务器的时间服务器
……略……
# Hosts on local network are less restricted.
#restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
restrict 192.168.0.0 mask 255.255.255.0 nomodify notrap #复制上边一行并启用,把ip修改成本地地址段
#外部时间服务器不可用时,以本地时间作为时间服务
server 127.127.0.0
fudge 127.127.0.0 stratum 10
……略……
[root@master ~]# service ntpd restart #重启服务
[root@master ~]# chkconfig --level 235 ntpd on
在从服务器上安装配置ntp:
[root@slave ~]
# yum -y install ntp
[root@slave ~]
# vim /etc/ntp.comf #注释掉默认的时间服务器,加上本地时间服务器
……略……
# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
server 192.168.0.201
#server 0.centos.pool.ntp.org iburst
#erver 1.centos.pool.ntp.org iburst
#server 2.centos.pool.ntp.org iburst
#server 3.centos.pool.ntp.org iburst
……略……
[root@slave ~]
# service ntpd restart
[root@slave ~]
# chkconfig ntpd on
[root@slave ~]
# ntpq -p #监控时间校准的状态信息
测试时可以试着把从服务器的时间调慢几分钟,然后观察从服务器的时间是否会慢慢的赶上来。
一、安装MySQL由于centos6.5系统默认安装了MySQL,所以需要卸载掉自带的mysql,主,从服务器都要执行相同操作
[root@localhost ~]# yum remove mysql ##卸载mysql
[root@localhost ~]# cd /opt/ ##将mysql源码包下载到/opt目录下
[root@localhost opt]# ls
mysql-5.5.44.tar.gz
[root@localhost opt]# yum install gcc gcc-c++ ncurses-devel perl cmake ##安装依赖包,由于MySQL5.5版本以后需要用cmake编译,所以需要安装cmake
[root@localhost opt]# tar xf mysql-5.5.44.tar.gz ##解压mysql
[root@localhost opt]# cd mysql-5.5.44 ##cd到mysql解压目录
####使用cmake编译MySQL,指定安装目录为/opt/mysql,数据目录为/opt/mydata/data,配置文件目录为/opt/mysql
[root@localhost mysql-5.5.44]# cmake . -DCMAKE_INSTALL_PREFIX=/opt/mysql \
-DMYSQL_DATADIR=/opt/mydata/data \
-DSYSCONFDIR=/opt/mysql \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_READLINE=1 \
-DWITH_SSL=system \
-DWITH_ZLIB=system \
-DWITH_LIBWRAP=0 \
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci
cmake . -DCMAKE_INSTALL_PREFIX=/opt/mysql -DMYSQL_DATADIR=/opt/mydata/data -DSYSCONFDIR=/opt/mysql -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DWITH_SSL=system -DWITH_ZLIB=system -DWITH_LIBWRAP=0 -DMYSQL_UNIX_ADDR=/tmp/mysql.sock -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci
[root@localhost mysql-5.5.44]# make
[root@localhost mysql-5.5.44]# make install
##创建程序用户,程序组。创建mysql数据目录,将/opt/mysql目录的属主和属组改为MySQL用户MySQL组
[root@localhost mysql-5.5.44]# groupadd -r mysql ##创建mysql程序组
[root@localhost mysql-5.5.44]# useradd -g mysql -r -d /opt/mydata/data/ mysql ##创建mysql用户
[root@localhost mysql-5.5.44]# mkdir -p /opt/mydata/data ##创建数据目录
[root@localhost mysql-5.5.44]# chown -R mysql:mysql /opt/mysql ##修改mysql安装目录的属组属主
[root@localhost mysql-5.5.44]# cp support-files/my-large.cnf /opt/mysql/my.cnf ##为mysql提供配置文件
[root@localhost mysql-5.5.44]# vi /opt/mysql/my.cnf ##在[mysqld]段内添加datadir= /opt/mydata/data
###为mysql提供启动脚本
[root@localhost mysql-5.5.44]# cp support-files/mysql.server /etc/rc.d/init.d/mysqld
[root@localhost mysql-5.5.44]# chmod +x /etc/rc.d/init.d/mysqld ##添加执行权限
##初始化数据库,出现两个OK就初始化成功啦
[root@localhost mysql-5.5.44]# cd /opt/mysql
[root@localhost mysql]# ./scripts/mysql_install_db --user=mysql --datadir=/opt/mydata/data/
[root@localhost mysql]# ls /opt/mydata/data/ ##初始化后就可以看到如下内容
mysql mysql-bin.000001 mysql-bin.000002 mysql-bin.index performance_schema test
[root@localhost mysql]# service mysqld start ##启动mysql
Starting MySQL... [确定]
[root@localhost mysql]# netstat -an | grep 3306 ##查看3306端口是否开启
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN
[root@localhost ~]# vi /etc/profile.d/mysql.sh ##为mysql配置环境变量,添加如下内容
export PATH=/usr/bin:/usr/sbin:/bin:/sbin:/usr/X11R6/bin:/opt/mysql/bin/
[root@localhost ~]# source /etc/profile.d/mysql.sh ##重新识别,至此MySQL就安装结束了
二、配置MySQL主从
1、首先配置主服务器
[root@localhost ~]# service mysqld stop
[root@localhost ~]# vi /opt/mysql/my.cnf ##在[mysqld]段内添加如下内容
[mysqld]
log-bin=mysql-bin ###########启用二进制日志#############
server-id=1 ###########服务器唯一ID###########
[root@localhost ~]# service mysqld start
Starting MySQL.. [确定]
[root@localhost ~]# mysqladmin -uroot -p password "123" #默认root用户密码为空,修改密码为123
Enter password:
[root@localhost ~]# mysql -uroot -p ##登陆mysql终端,授权给从服务器,如下面绿色内容
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.5.44-log Source distribution
Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
####授权给从服务器####
mysql> grant replication slave on *.* to 'root'@'192.168.1.189' identified by '123';
Query OK, 0 rows affected (0.02 sec)
mysql> flush privileges; ###刷新
Query OK, 0 rows affected (0.00 sec)
mysql> show master status; ##查询主数据库状态
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000005 | 333 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
###这里需要记录 File 以及 Position 的值,在操作从服务器时会用到,以你自身服务器的为准###
2、配置从服务器
[root@localhost ~]# service mysqld stop ##停止mysql数据库
Shutting down MySQL. [确定]
[root@localhost ~]# vi /opt/mysql/my.cnf ##在[mysqld]段内添加如下内容
[mysqld]
log-bin=mysql-bin ###########启用二进制日志#############
server-id=2 ###########服务器唯一ID###########
[root@localhost ~]# service mysqld start ##启动数据库
Starting MySQL.. [确定]
[root@localhost ~]# mysqladmin -uroot -p password "123" #默认root用户密码为空,修改密码为123
Enter password:
[root@localhost ~]# mysql -uroot -p ###登陆mysql终端,执行如下绿色字体SQL语句
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.44-log Source distribution
Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
###执行同步SQL语句###
mysql> change master to
-> master_host='192.168.1.188',
-> master_user='root',
-> master_password='123',
-> master_log_file='mysql-bin.000005',
-> master_log_pos=333;
change master to master_host='192.168.1.165',master_user='root',master_password='123',master_log_file='mysql-bin.000004',master_log_pos=471;
Query OK, 0 rows affected (0.17 sec)
###启动Slave 同步进程####
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
####主从同步检查####
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State:
Master_Host: 192.168.1.188
Master_User: root
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000005
Read_Master_Log_Pos: 333
Relay_Log_File: localhost-relay-bin.000002
Relay_Log_Pos: 4
Relay_Master_Log_File: mysql-bin.000005
Slave_IO_Running: No ###这里应当为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: 333
Relay_Log_Space: 107
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: NULL
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 1593
Last_IO_Error: Fatal error: The slave I/O thread stops because master and slave have equal MySQL server ids; these ids must be different for replication to work (or the --replicate-same-server-id option must be used on slave but this does not always make sense; please check the manual before using it).
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
1 row in set (0.00 sec)
#####出现上方红色内容错误,由于主和从的server-id相同导致,解决办法如下########
mysql> show variables like 'server_id'; ###查看server-id
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id | 1 |
+---------------+-------+
1 row in set (0.00 sec)
mysql> stop slave; ###停止slave
Query OK, 0 rows affected (0.00 sec)
mysql> set global server_id=2; ###手动修改server-id为2
Query OK, 0 rows affected (0.00 sec)
mysql> slave start; ###再次启动slave
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status\G ####再次主从同步检查,发现两个都为yes####
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.188
Master_User: root
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000005
Read_Master_Log_Pos: 333
Relay_Log_File: localhost-relay-bin.000003
Relay_Log_Pos: 253
Relay_Master_Log_File: mysql-bin.000005
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: 333
Relay_Log_Space: 413
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:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
1 row in set (0.00 sec)
#################################到这里整个MYSQL主从的复制就完成了############################
三、测试主从服务器是否能够同步,在主服务器test数据库上创建一张名为data的表,看从服务器是否能够同步过去。
[root@localhost ~]# mysql -uroot -p ###这里为主服务器上的操作
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.5.44-log Source distribution
Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> use test; ##切换数据库到test
Database changed
##创建data表,字段name,address,phone
mysql> create table data(name varchar(20),address varchar(20),phone varchar(20));
Query OK, 0 rows affected (0.05 sec)
###插入内容,name=jerry,address=beijing,phone=13566666666
mysql> insert into data(name,address,phone) values ('jerry','beijing','13566666666');
Query OK, 1 row affected (0.03 sec)
mysql> select * from data; ##执行select语句查询data表中的内容
+-------+---------+-------------+
| name | address | phone |
+-------+---------+-------------+
| jerry | beijing | 13566666666 |
+-------+---------+-------------+
1 row in set (0.00 sec)
####################切换到从服务器验证是否同步############################
[root@localhost ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.5.44-log Source distribution
Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> use test; ##切换到test数据库
Database changed
mysql> show tables; ##查看表
+----------------+
| Tables_in_test |
+----------------+
| data |
+----------------+
1 row in set (0.00 sec)
mysql> select * from data; ##执行select语句查看已经同步
+-------+---------+-------------+
| name | address | phone |
+-------+---------+-------------+
| jerry | beijing | 13566666666 |
+-------+---------+-------------+
1 row in set (0.00 sec)
mysql>
###########################至此MySQL安装+主从复制已经全部结束##############################