http://www.cnblogs.com/blackpuppy/p/upgrade_mysql_from_55_to_56_on_centos.html (原文)
1. 概述
在我做的一个项目中,最近我对生产服务器上的一系列系统软件进行了升级,包括Git、Nginx、MySQL和PHP。这篇文章讲的是升级MySQL的过程,其他软件的升级,可见下面列出的文章。
- 在CentOS上把Git从1.7.1升级到1.7.12.4
- 在CentOS上把Nginx从1.2.4升级到1.6.0
- 在CentOS上把MySQL从5.5升级到5.6 (本文)
- 在CentOS上把PHP从5.4升级到5.5
在我加入这个项目之前,服务器上的MySQL已经安装设置好了,我只是正常使用而已。现在过去1年了,应该适当升级服务器上的软件了。升级这种事情是应当经常做的,倒不是为了追最新版本,而是当正式版本发放出来的时候,应该及时更新, 以便获得最新的更正、补丁,避免服务器上的漏洞,减少安全隐患。
升级是在今年6月11-12日进行的,到今天才有空做个记录,便于日后参考。
动手之前,查找了很多资料,因为MySQL要比之前升级的Git和Nginx要复杂多了。我在参考资料中列出了我所能找到作为依据的文档,可供参考。
4. 升级MySQL的计划
不好意思,yum同学,编剧说你还要等下一幕。
升级MySQL时,又做了两个计划:
- 当然,首先要备份所有的数据库数据
- 计划A: 使用参考资料[6]的方法,添加安装库,用yum update升级
- 计划B: 用yum卸载MySQL 5.5,再安装MySQL 5.6
5.2 查看MySQL的版本
用下面的命令查看MySQL的版本:
# mysql --version mysql Version 14.14 Distrib 5.5.28, for Linux (x86_64) using reeadline 5.1
可见,当前的MySQL的版本是5.5.28。
5.3 添加MySQL的yum安装库
这是参照参考资料[7]来做的,后来发现[6]的方法更好:
# wget http://dev.mysql.com/get/mysql-community-release-el6-5.noarch.rpm
这样很多更新的MySQL安装包就都有了。
5.4 用yum查看MySQL安装相关信息
用下面的命令查看安装了什么:
# yum list installed | grep ^mysql mysql-community-release.noarch
其中,^mysql是指以mysql开头的安装包。
用下面的命令查看安装了的和可用的安装包:
# yum info mysql-server Installed Packages Name : MySQL-Server Arch : x86_64 Version : 5.5.28 Release : 1.linux2.6 Repo : installed Available Packages Name : mysql-server Arch : x86_64 Version : 5.1.73 Release : 3.el6_5 Repo : updates
这里不知道为啥可用的安装包没有显示MySQL 5.6的。
检查可用的安装包:
# yum check-update mysql-server Obsoleting Packages mysql-community-client.x86_64 5.6.19-2.el6 mysql56-community MySQL-client.x86_64 5.5.28-1.linux2.6 installed mysql-community-devel.x86_64 5.6.19-2.el6 mysql56-community MySQL-devel.x86_64 5.5.28-1.linux2.6 installed mysql-community-server.x86_64 5.6.19-2.el6 mysql56-community MySQL-server.x86_64 5.5.28-1.linux2.6 installed
可见,MySQL 5.6的安装包都算community版本了。
5.5 升级MySQL及其组件
运行下面的命令:
# yum update mysql-server Dependencies Resolved ================================================================================ Package Arch Version Repository Size ================================================================================ Installing: mysql-community-client x86_64 5.6.19-2.el6 mysql56-community 18 M replacing MySQL-client.x86_64 5.5.28-1.linux2.6 mysql-community-server x86_64 5.6.19-2.el6 mysql56-community 52 M replacing MySQL-server.x86_64 5.5.28-1.linux2.6 Installing for dependencies: mysql-community-common x86_64 5.6.19-2.el6 mysql56-community 298 k mysql-community-libs x86_64 5.6.19-2.el6 mysql56-community 1.9 M Transaction Summary ================================================================================ Install 4 Package(s)
后面的输出省略了。可见有4个安装包(共70多MB)要下载、安装。结果,下载用了80分钟。
但安装遇到问题了:
Running rpm_check_debug Running Transaction Test Transaction Check Error: file /usr/bin/mysql_config from install of mysql-community-client-5.6.19-2.el6 .x86_64 conflicts with file from package MySQL-devel-5.5.28-1.linux2.6.x86)64 Error Summary -------------
可以看到出错了,搜索了一阵儿,也没搞明白。随即停止A计划(和成龙的电影没关系),改用B计划。
6. 计划B: 用yum卸载再安装
可以预料,这个计划要多折腾一些。仍然需要在超级管理员root的权限下运行下面的命令。
6.1 停止web服务器
这时为了避免用户访问网站时,看到数据库连接失败这样的错误信息。到这时我才想起这一点来,还是对用户关怀不够,应该在有可能宕掉网站的操作之前就做的。
因为使用的是Nginx,所以运行下面的命令:
# service nginx stop
Stopping nginx: [ OK ]
6.2 卸载MySQL 5.5.28
一共有3个包要卸载。
先卸载mysql-server包:
# yum remove mysql mysql-server Dependencies Resolved ================================================================================ Package Arch Version Repository Size ================================================================================ Removing: MySQL-server x86_64 5.5.28-1.linux2.6 installed 153 M Transaction Summary ================================================================================ Remove 1 Package(s) Installed size: 153 M Is this ok [y/N]: y Downloading Packages: Running rpm_check_debug Running Transaction Test Transaction Test Succeeded Running Transaction Warning: RPMDB altered outside of yum. Erasing : MySQL-server-5.5.28-1.linux2.6.x86_64 1/1 Verifying : MySQL-server-5.5.28-1.linux2.6.x86_64 1/1 Removed: MySQL-server.x86_64 0:5.5.28-1.linux2.6 Complete!
再卸载mysql-client:
# yum remove mysql-client Dependencies Resolved ================================================================================ Package Arch Version Repository Size ================================================================================ Removing: MySQL-client x86_64 5.5.28-1.linux2.6 installed 57 M Transaction Summary ================================================================================ Remove 1 Package(s) Installed size: 57 M Is this ok [y/N]: y Downloading Packages: Running rpm_check_debug Running Transaction Test Transaction Test Succeeded Running Transaction Erasing : MySQL-client-5.5.28-1.linux2.6.x86_64 1/1 Verifying : MySQL-client-5.5.28-1.linux2.6.x86_64 1/1 Removed: MySQL-client.x86_64 0:5.5.28-1.linux2.6 Complete!
最后卸载mysql-devel:
# yum remove mysql-devel Dependencies Resolved ================================================================================ Package Arch Version Repository Size ================================================================================ Removing: MySQL-devel x86_64 5.5.28-1.linux2.6 installed 20 M Transaction Summary ================================================================================ Remove 1 Package(s) Installed size: 20 M Is this ok [y/N]: y Downloading Packages: Running rpm_check_debug Running Transaction Test Transaction Test Succeeded Running Transaction Erasing : MySQL-devel-5.5.28-1.linux2.6.x86_64 1/1 Verifying : MySQL-devel-5.5.28-1.linux2.6.x86_64 1/1 Removed: MySQL-devel.x86_64 0:5.5.28-1.linux2.6 Complete!
3个包的卸载都顺利完成。
6.3 安装MySQL 5.6.19
安装比卸载更简单,因为yum会自动检查依赖安装包:
# yum install mysql-server Dependencies Resolved ================================================================================ Package Arch Version Repository Size ================================================================================ Installing: mysql-community-server x86_64 5.6.19-2.el6 mysql56-community 52 M Installing for dependencies: mysql-community-client x86_64 5.6.19-2.el6 mysql56-community 18 M mysql-community-common x86_64 5.6.19-2.el6 mysql56-community 298 k mysql-community-libs x86_64 5.6.19-2.el6 mysql56-community 1.9 M Transaction Summary ================================================================================ Install 4 Package(s) Total size: 72 M Installed size: 323 M Is this ok [y/N]: y Downloading Packages: Running rpm_check_debug Running Transaction Test Transaction Test Succeeded Running Transaction Installing : mysql-community-common-5.6.19-2.el6.x86_64 1/4 Installing : mysql-community-libs-5.6.19-2.el6.x86_64 2/4 Installing : mysql-community-client-5.6.19-2.el6.x86_64 3/4 Installing : mysql-community-server-5.6.19-2.el6.x86_64 4/4 warning: /etc/my.cnf created as /etc/my.cnf.rpmnew /bin/chmod: cannot access `': No such file or directory Verifying : mysql-community-common-5.6.19-2.el6.x86_64 1/4 Verifying : mysql-community-libs-5.6.19-2.el6.x86_64 2/4 Verifying : mysql-community-client-5.6.19-2.el6.x86_64 3/4 Verifying : mysql-community-server-5.6.19-2.el6.x86_64 4/4 Installed: mysql-community-server.x86_64 0:5.6.19-2.el6 Dependency Installed: mysql-community-client.x86_64 0:5.6.19-2.el6 mysql-community-common.x86_64 0:5.6.19-2.el6 mysql-community-libs.x86_64 0:5.6.19-2.el6 Complete!
由于篇幅关系,输出有所缩略。一切顺利,继续!
预告:繁琐的事情这才开始。
6.4 更改MySQL配置,来使用数据硬盘
更改\etc\my.cnf,下面只列出更改的相关设置:
[mysqld]
如果你也更改了数据目录,最后一行的 datadir = /data/mysql 切不可遗漏,我就是因为这一行实际上没有及时改,还是指向旧的数据目录,导致数据升级失败,直到升级数据之后才发现,在下文中有详细论述。
6.5 启动MySQL服务
启动服务:
# service mysql start mysql: unrecognized service
居然出错了!只好直接运行MySQL的daemon:
# /etc/init.d/mysqld start Starting mysqld: [ OK ]