centos mysql升级

卸载当前的 MySQL

查看当前 MySQL 版本:

[root@coderknock ~]# mysql -V
mysql Ver 14.14 Distrib 5.1.73,forredhat-linux-gnu (i386) using readline 5.1

停止 MySQL 服务

[root@coderknock ~]# service mysqld stop
Stopping mysqld:      [ OK ]

备份数据【数据不重要可以忽略】

备份数据库,升级MySQL通常不会丢失数据,但保险起见,我们需要做这一步。输入命令:

[root@coderknock ~]# mysqldump -u xxx -h xxx -P 3306 -p --all-databases > databases.sql

卸载旧版本 MySQL

[root@coderknock ~]# yum remove mysql mysql-*

这样就卸载完成了,为了保险起见我们检查一下系统中是否还有 MySQL 相关内容没有删除干净

[root@coderknock ~]# yum list installed | grep mysql
[root@coderknock ~]#

上面的情况是没有未删除部分,如果有的话可通过下面的命令删除:

[root@coderknock ~]# yum remove mysql-libs

下载最新版本的 MySQL

查看系统配置信息选择对应 MySQL 包

[root@coderknock ~]# uname -r
3.10.104-1.el6.elrepo.i686

在浏览器打开 sohu提供的MySQL 镜像(官网速度太慢)

centos mysql升级_第1张图片

然后根据自己系统配置选择对应的 rpm 包,右键复制文件链接,我的选择如下:

centos mysql升级_第2张图片下载并解压

[root@coderknock ~]# wgethttp://mirrors.sohu.com/mysql/MySQL-5.7/mysql-5.7.17-1.el6.i686.rpm-bundle.tar
[root@coderknock ~]# tar xvf mysql-5.7.17-1.el6.i686.rpm-bundle.tar
mysql-community-libs-compat-5.7.17-1.el6.i686.rpm
mysql-community-client-5.7.17-1.el6.i686.rpm
mysql-community-embedded-devel-5.7.17-1.el6.i686.rpm
mysql-community-devel-5.7.17-1.el6.i686.rpm
mysql-community-common-5.7.17-1.el6.i686.rpm
mysql-community-test-5.7.17-1.el6.i686.rpm
mysql-community-embedded-5.7.17-1.el6.i686.rpm
mysql-community-libs-5.7.17-1.el6.i686.rpm
mysql-community-server-5.7.17-1.el6.i686.rpm

安装及配置

安装 rpm 【有依赖关系,请按下面的步骤安装,搜索 [root@coderknock ~]# 可跳过安装日志直接查看相关命令】

[root@coderknock ~]# yum localinstall mysql-community-common-5.7.17-1.el6.i686.rpm

[root@coderknock ~]# yum localinstall mysql-community-libs-5.7.17-1.el6.i686.rpm

[root@coderknock ~]# yum localinstall mysql-community-client-5.7.17-1.el6.i686.rpm

[root@coderknock ~]# yum localinstall mysql-community-server-5.7.17-1.el6.i686.rpm

配置

查看版本

[root@coderknock ~]# mysql -V
mysql Ver 14.14 Distrib 5.7.17,forLinux (i686) using EditLine wrapper

启动 MySQL

[root@coderknock ~]# service mysqld start
MySQL Daemon failedtostart.
Starting mysqld:   [FAILED]

上面的情况说明启动失败,这是因为经过上面的步骤,MySQL 最新版已经安装到我们的系统了,但这是还没有初始化数据,所以导致无法启动。

初始化数据库

[root@coderknock ~]# mysqld --initialize

如果遇到错误如下:

2017-01-06T14:53:08.889803Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2017-01-06T14:53:08.892190Z 0 [ERROR] --initialize specified but the data directory has files in it. Aborting.
2017-01-06T14:53:08.892475Z 0 [ERROR] Aborting

可以运行以下命令【无需再执行 mysqld --initialize】:

[root@coderknock ~]# rm -rf /var/lib/mysql
[root@coderknock ~]# service mysqld start
Initializing MySQL database:  [ OK ]
Installing validatepasswordplugin: [ OK ]
Starting mysqld:   [ OK ]

这样我们就启动了 MySQL 服务

MySQL 无法登陆的解决办法

登录时我遇到了一些问题:

[root@coderknock ~]# mysql -u root -p
Enter password:
ERROR 1045 (28000): Access deniedforuser'root'@'localhost'(usingpassword: YES)

这是因较新版本的 MySQL 因为安全的考虑,会设置默认密码

[root@coderknock ~]# grep'temporary password'/var/log/mysqld.log
2017-01-06T14:53:33.490540Z 1 [Note] Atemporarypasswordisgenerated forroot@localhost: Ei(GLix_p6/1
密码为
Ei(GLix_p6/1

这样就找到了密码,如果上面方法不可行可以看看是否有 /root/.mysql_secret 文件

重置密码

无论是获取到了默认密码还是强行修改了密码。新版 MySQL 在第一次通过密码登录后会无法执行SQL:

mysql> selecthostfrommysql.userwhereUser='root';
ERROR 1820 (HY000): You must reset yourpasswordusingALTER USERstatement before executing this statement.

这是因为 MySQL 为了安全会要求修改初始密码。

mysql> ALTER USER 'root'@'%' IDENTIFIED BY '你的密码';(1820 直接SET PASSWORD = PASSWORD('123456');)

如果遇到ERROR 1819 (HY000): Your password does not satisfy the current policy requirements说明你的密码太简单,需要设置复杂点的密码,有大小写字母、有数字、有标点基本上就可以通过。
注意,如果你没有修改过 root 用户的Host或者修改为非 % 的字符,需要对上面的语句进行对应的修改,例如: ALTER USER‘root'@'localhost' IDENTIFIED BY ‘你的密码'

如果遇到ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement 则需要退出安全模式,正常启动MySQL后登陆再进行以上操作:

mysql> quit
[root@coderknock ~]# ps -ef|grep mysql
root 14208 12686 0 23:02 pts/0 00:00:00 /bin/sh /usr/bin/mysqld_safe--user=mysql --skip-grant-tables --skip-networking
mysql 14381 14208 0 23:02 pts/0 00:00:00 /usr/sbin/mysqld--basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib/mysql/plugin --user=mysql --skip-grant-tables --skip-networking --log-error=/var/log/mysqld.log --pid-file=/var/run/mysqld/mysqld.pid --socket=/var/lib/mysql/mysql.sock
root 15148 12686 0 23:23 pts/0 00:00:00 grep mysql
[root@coderknock ~]# kill -9 114208
-bash: kill: (114208) -Nosuch process
[root@coderknock ~]# kill -9 14208
[1]+ Killed   mysqld_safe--user=mysql --skip-grant-tables --skip-networking
[root@coderknock ~]# kill -9 1438
[root@coderknock ~]# service mysqld start
[root@coderknock ~]# mysql -u root mysql

-----------------------------或者通过以下方法直接修改密码:

[root@coderknock ~]# service mysqld stop
Stopping mysqld:      [ OK ]
[root@coderknock ~]# mysqld_safe--user=mysql --skip-grant-tables --skip-networking &
[1] 14208
[root@coderknock ~]# 2017-01-06T15:02:18.663496Z mysqld_safe Loggingto'/var/log/mysqld.log'.
2017-01-06T15:02:18.666764Z mysqld_safe Loggingto'/var/log/mysqld.log'.
2017-01-06T15:02:18.693025Z mysqld_safe Starting mysqld daemonwithdatabasesfrom /var/lib/mysql
^C
[root@coderknock ~]# mysql -u root mysql
Reading tableinformationforcompletion of tableandcolumnnames
You can turn off this feature to get a quicker startupwith-A
 
Welcome tothe MySQL monitor. Commandsendwith; or\g.
Your MySQL connection id is3
Server version: 5.7.17 MySQL Community Server (GPL)
 
Copyright (c) 2000, 2016, Oracleand/orits affiliates.Allrights reserved.
 
Oracle isa registered trademarkofOracle Corporationand/orits
affiliates. Other names may be trademarksoftheir respective
owners.
 
Type 'help;'or'\h'for help. Type '\c'toclear thecurrentinput statement.
 
mysql>updatemysql.usersetauthentication_string=password('你的密码.')whereUser='root';
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 1
mysql> flush privileges;
mysql> updatemysql.usersetHost='%'whereUser='root'
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 1
mysql> flush privileges;

注意SQL在Linux下要注意大小写,执行之后Query OK, 1 row affected, 1 warning (0.00 sec),row 之前是1才代表执行成功了。update mysql.user set Host='%' where User='root' 这句是为了别的 IP 的机器也可以登录我们的MySQL,可以按需执行

 
  

你可能感兴趣的:(centos mysql升级)