51CTO下载-linux下mysql双机热备配置整理_第1页
51CTO下载-linux下mysql双机热备配置整理_第2页
51CTO下载-linux下mysql双机热备配置整理_第3页
51CTO下载-linux下mysql双机热备配置整理_第4页
51CTO下载-linux下mysql双机热备配置整理_第5页
已阅读5页,还剩4页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、linux下mysql双机热备简单配置-xl 2013.5.22约定环境:服务器两台:主服务器 备服务器linux 环境 centOS 6.3一先将两台服务器的mysql数据库正常装好,安装方式以ICP部署文档的方式就行,以下是具体步骤。1 安装 Mysql 5.5.271.1查询并卸载其他版本rootlocalhost #rpm -qa|grep mysql / 查询已装的 Mysqlrootlocalhost # rpm -qa|grep -i mysql / 忽略大小写查询已装的Mysql如有其他版本的 MySQL按照rpm -qa | gre

2、pmysql查询出来的相关包顺序从下往上逐个删除,使用以下命令rootlocalhost # rpm -e 查询出来的相关包 /rpm 卸载 MySQL注意事项:(1) 如果出现包依赖,使用-n odeps参数强制删除。(2) 如果出现多个匹配,使用-allmatches全部删除。(例如:rpm -e java-1.6.0-openjdk--1.1.el6.x86_64- nodeps)rootlocalhost #yum -y remove 查询出来的相关包 /yum 卸载 MySQL1.2创建相关目录并上传Mysql相关RPM包rootlocalhost #

3、cd /usr/local / 进入 local 下ncslocalhost local# mkdir mysql / 仓U建 mysql 目录ncslocalhost local# cd mysql / 进入 mysql 目录将 MySQL-server-5.5.27-1.rhel5.i386.rpm和 MySQL-client-5.5.27-1.rhel5.i386.rpm上传到/usr/local/ mysql 目录。注:64位系统请安装64位相关软件包,复制命令时请确认版本号。1.3 安装 Mysql-serverrootlocalhostmysql# rpm -ivh MySQL-s

4、erver-5.5.27-1.rhel5.i386.rpm 等待安装过程Please report any problems with the /usr/b in/mysqlbug script!出现以上内容表明 MySQL服务端安装完毕!!!1.4启动Mysql服务rootlocalhostmysql# /etc/init.d/mysql start/ 启动 mysql 服务Starti ng MySQL.确定rootlocalhostmysql# netstatanlt 查看 3306 端口是否已经启动tcp 00 :330:*LISTEN出现以上内容表明 M

5、ysql服务已经启动!!!1.5 安装 Mysql-clientrootlocalhostmysql# rpm -ivh MySQL-clie nt-5.5.27-1.rhel5.i386.rpm|Prepari ng.# 100%1:MySQL-clie nt # 100% 出现以上内容表明Mysql连接程序安装完毕!1.6进入Mysql数据库rootlocalhostmysql# mysql/由于默认密码为空,所以直接可以登陆Type 'help;' or 'h' for help.Type 'c' to clear the curre nt

6、 in put stateme nt. mysql> /在mysql>中依次输入以下命令update mysql.user set password=password("icpdb") where User="root"/修改mysql管理员密码为icpdbdelete from mysql.user where User="/删除mysql自带的匿名帐户delete from mysql.user where Host=''/删除mysql自带的地址delete from mysql.user w

7、here Host='localhost.localdoma in:/删除mysqlmysql自带的CREATE USER 'n cs''' IDENTIFIED BY 'icpdb'/添加ncs用户,设置密码GRANT ALL PRIVILEGES ON *.* TO 'root''%' IDENTIFIED BY 'icpdb' WITH GRANT OPTION;/开启远程访问GRANT ALL PRIVILEGES ON *.* TO 'n cs'&

8、#39;%' IDENTIFIED BY 'icpdb' WITH GRANT OPTION;/开启远程访问FLUSH PRIVILEGES;/刷新保存quit/退出注:除了退出每条命令最后都有分号1.7登陆测试rootlocalhostmysql#mysql -u root -p / 设置完密码后登陆测试 Enter password:/ 输入密码 icpdb 回车Welcome to the MySQL mon itor.Comma nds end with ; or g.Your MySQL conn ection id is 2451Server versio

9、n: 5.5.27-log MySQL Commu nity Server (GPL)Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporati on an d/or its affiliates. Other n ames may be trademarks of their respective own ers.Type 'help;' or 'h' for he

10、lp.Type 'c' to clear the curre nt in put stateme nt. mysql> /输入quit退出出现以上内容表明成功进入了mysql,安装结束!!!1.8修改相关配置文件1修改mysql配置文件rootlocalhost # cp /usr/share/mysql/my-medium.c nf /etc/f/ 将 my-f 从/usr/share/mysql/ 复制至U /etc/,改名为 f rootlocalhost # vi /etc/f/ 编辑文件 f在client最后增加default-character-set=utf

11、8在mysqld最后增加lower case table names=1tran sactio n-isolatio n=READ-COMMITTEDeve nt-scheduler=1max_c onnection s=20002启动rootlocalhost # cd /etc/i ni t.drootlocalhost in it.d# /etc/ in it.d/mysql start3停止rootlocalhost # killall mysqld4重新启动rootlocalhost in it.d# /etc/ in it.d/mysql restart/查看是否启动成功rootl

12、ocalhost in it.d# n etstat -an lt|grep 3306|grep -v grep5设置自动启动rootlocalhost # /sb in/chkc onfig mysql on两台服务器都装好后,进行双机热备的配置(主从关系)约定环境:两台服务器的主机名最好不一样,如一个是db1 (主),一个是db2 (从)。可以通过修改/etc/sysconfig/network 实现两台数据库root密码为icpdb1进入主服务器mysql -u root -p密码:icpdbmysql下输入:show master status;/列出以下信息(举例)+| File|

13、Position | Bin log_Do_DB | Bin loggno re_DB |+| mysql-bi n. |107 | opts2| mysql|+1 row in set如何提示为空则需要在 /etc/f下的mysqld中添加log-b in=mysql-b inserver-id=10注意看里面的参数,特别前面两个File和Position,在从服务器(Slave)配置主从关系会有用到的。(飘红部分)创建同步用户grant replication slave on *.* to userl identified by ''/创建用户us

14、er1密码为,指定IP为从服务器可以访问的。flush privileges;修改 /etc/f在mysqld下修改即可:mysqldserver-id = 1log-b in=mysql-b inbinl og-do-db = opts2 binl og-ig no re-db = mysql其中这两行是本来就有的,可以不用动,添加下面两行即可/指疋需要被同步的数据库名称如opts2/指疋不需要被冋步的数据库名称如mysql自带的mysql或其他的数据库,如果不指定这两条默认全部都同步。修改好后service mysql restart /重启mysql服务,不解释。如果成功则没问题。主服务

15、器配置完毕。2进入从服务器首先修改/etc/f配置mysqld在mysqld段后修改server-id = 2修改 1 为 2log-bi n=mysql-bin/默认配置,开启日志记录,不用改replicate-do-db = opts2/指定需要去主服务器发起同步的数据库名如opts2replicate-ig no re-db =mysql,i nformati on _schema,performa nce_schema/执行不需要进行同步的数据库名service mysql restart / 不解释用change mster语句指定同步位置mysql -u root -p密码:icp

16、dbmysql>创建同步用户grant replication slave on *.* to user2 identified by ”;/创建用户user2密码为,指定IP为主服务器可以访问的。其实这句话可以不用打,这句话是给互相备份用的(主-主关系),不过配上也没事,没有设置权限。mysql>stop slave;/先停步slave服务线程,这个是很重要的,如果不这样做会造成以下操作不成功。mysql>cha nge master to>master_host=' ',master_user='

17、 userl ',master_password=",> master_log_file=' mysql-bin.',master_log_pos= 107 ;注:master_log_file, master_log_pos 由主服务器(Master)查出的状态 值中确定。也就是刚刚叫注意的。master_log_file 对应File, master_log_pos 对应 Position 。mysqlb inlog mysql-b in. > O.txt回车后,如成功会有OK提示,如果不成功 则要重新设置slave。指令如下mysql>

18、stop slave;mysql>reset slave;之后停止slave线程重新开始。成功后,则可以开启slave线程了flush privileges;mysql>start slave;/ 重新启动mysql>show slave status'G; / 查看从服务器状态ng: Y&sSlaue_SQL_Rning: Yes这两个参数都是 YES就证明从服务器成功了。| Connect_RetrL|: 60 这个代表同步失败后重试的间隔,默认为 60秒。Master.Host:Master_Ueer!Master-Port!这三个参数列出了主服务器的I

19、P,远程访问的用户名和端口 i . rI;.Maat-er_Log_FlLe;Resd-Master-Log-Pos:列出了日志文件名称和POS位置,一定要以主服务器一致才行,否则不能同步数据。service mysql restart/ 不解释至此从服务器设置完成测试效果。现在在主服务器上创建opts2数据库,然后随便在库中增加删除表或者数据,从服务器都会实时的同步。补充:Slave_IO_Running: No如果这里一直出现血那么就得查看MySQL主服务器的日志,然后再查看mysql-bi n.(默认是查看不了必须先mysqlb inlog mysql-b in. > O.txt然

20、后查看0.txt文档,最下边的at xx (如果是得一次做默认就是4你就直接用4)替换掉 最开始看的的107 )Slave_SQL_Ru nning: Yes做完以上默认就好了 如果在web里添加数据时同步不到MySQL备服务器,那就从mysql>stop slave;mysql> cha nge master to master_log_file='mysql-b in .',master_log_pos=;飘红处是你再次查看状态时报错最后的数字如:Last_SQL_Error: Could not execute Write_rows event on tabl

21、e opts2.opts2_sm_audit; Duplicate entry '3377' for key 'PRIMARY', Error_code: 1062; han dler error HA_ERR_FOUND_DUPP_KEY; the event's master log mysql-b in., en d_log_posMysql>stop slave;最后再查看一遍mysql> show slave statusG;*1. row*Slave_IO_State: Waiti ng for master to send event Master_Host: Master_User: user1 Master_Port: 3306Conn ect_Retry: 60Master_Log_File: mysql-b in.Read_Master_Log_Pos:Relay_Log_File: DB2-relay-b in.Rel

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论