Mysql下实现主从复制
前言
系统承担大量的web访问量时,增加webserver实现负载均衡,由于增加webserver太多了,导致数据库
的连接资源不够用,而这个时候已经分库分表了,发现数据库读写比很高,这个时候通常用到读写分离
方案,数据库读写分离要求数据库的的主从复制,使用mysql实现主从复制
。
系统环境
在虚拟机中安装两个linux系统,分别安装mysql,实现读写分离。
linux系统使用的是 RedHat 6.2版本
mysql使用的是 mysql-5.5.36-el6.i686版本
linux下安装Mysql
下载linux下mysql安装文件MySQL-5.5.36-1.el6.i686.rpm,
下载地址 http://dev.mysql.com/downloads/file.php?id=450759
安装mysql只需要MySQL-server-5.5.36-1.el6.i686.rpm
MySQL-client-5.5.36-1.el6.i686.rpm
在/usr/local下创建mysql文件,进入mysql目录
root@longdd ~]# cd /usr/local
[root@longdd local]# mkdir mysql
[root@longdd local]# cd mysql
[root@longdd mysql]#
[root@longdd local]# cd mysql
[root@longdd mysql]#
首先安装Mysql服务端
[root@longdd mysql]# rpm -ivh MySQL-server-5.5.36-1.el6.i686.rpm
安装的时候会报包冲突
file /usr/share/mysql/ukrainian/errmsg.sys from install of MySQL-server-5.5.36-1.el6.i686
conflicts with file from package mysql-libs-5.1.66-2.el6_3.i686
卸载系统安装的包,重新安装mysql-server
[root@longdd mysql]# rpm -ev --nodeps mysql-libs-5.1.66-2.el6_3.i686
[root@longdd mysql]# rpm -ivh MySQL-server-5.5.36-1.el6.i686.rpm
Preparing... ########################################### [100%]
1:MySQL-server ########################################### [100%] ( 3%)
Preparing... ########################################### [100%]
1:MySQL-server ########################################### [100%] ( 3%)
安装成功
安装Mysql客户端
[root@longdd mysql]# rpm -ivh MySQL-client-5.5.36-1.el6.i686.rpm
Preparing... ########################################### [100%]
1:MySQL-client ########################################### [100%]
Preparing... ########################################### [100%]
1:MySQL-client ########################################### [100%]
安装成功
启动mysql服务
[root@longdd mysql]# service mysql start
Starting MySQL.. [确定]
Starting MySQL.. [确定]
连接mysql
[root@longdd mysql]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.36 MySQL Community Server (GPL)
Copyright (c) 2000, 2014, 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>
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.36 MySQL Community Server (GPL)
Copyright (c) 2000, 2014, 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>
配置主从服务器
修改主服务器master
#vi /etc/my.cnf
如果/etc下没有my.cnf文件,则从/etc/share/mysql下拷贝到/etc
[root@longdd mysql]# cp my-medium.cnf /etc/my.cnf
log-bin=mysql-bin //[必须]启用二进制日志
server-id=114 //[必须]服务器唯一ID,默认是1,一般取IP最后一段
server-id=114 //[必须]服务器唯一ID,默认是1,一般取IP最后一段