Centos MySQL 5.7安装、升级教程
mysql 5.7安装、升级笔记分享:
卸载当前的 mysql
查看当前 mysql 版本:
[root@coderknock ~]# mysql -v mysql ver 14.14 distrib 5.1.73, for redhat-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-* loaded plugins: security setting up remove process resolving dependencies --> running transaction check ---> package mysql.i686 0:5.1.73-7.el6 will be erased ---> package mysql-libs.i686 0:5.1.73-7.el6 will be erased --> processing dependency: libmysqlclient.so.16 for package: 2:postfix-2.6.6-6.el6_7.1.i686 --> processing dependency: libmysqlclient.so.16 for package: perl-dbd-mysql-4.013-3.el6.i686 --> processing dependency: libmysqlclient.so.16(libmysqlclient_16) for package: 2:postfix-2.6.6-6.el6_7.1.i686 --> processing dependency: libmysqlclient.so.16(libmysqlclient_16) for package: perl-dbd-mysql-4.013-3.el6.i686 --> processing dependency: mysql-libs for package: 2:postfix-2.6.6-6.el6_7.1.i686 ---> package mysql-server.i686 0:5.1.73-7.el6 will be erased --> running transaction check ---> package perl-dbd-mysql.i686 0:4.013-3.el6 will be erased ---> package postfix.i686 2:2.6.6-6.el6_7.1 will be erased --> processing dependency: /usr/sbin/sendmail for package: cronie-1.4.4-16.el6_8.2.i686 --> processing dependency: /usr/sbin/sendmail for package: redhat-lsb-core-4.0-7.el6.centos.i686 --> running transaction check ---> package cronie.i686 0:1.4.4-16.el6_8.2 will be erased --> processing dependency: cronie = 1.4.4-16.el6_8.2 for package: cronie-anacron-1.4.4-16.el6_8.2.i686 ---> package redhat-lsb-core.i686 0:4.0-7.el6.centos will be erased --> running transaction check ---> package cronie-anacron.i686 0:1.4.4-16.el6_8.2 will be erased --> processing dependency: /etc/cron.d for package: crontabs-1.10-33.el6.noarch --> processing dependency: /etc/cron.d for package: sysstat-9.0.4-31.el6.i686 --> restarting dependency resolution with new changes. --> running transaction check ---> package crontabs.noarch 0:1.10-33.el6 will be erased ---> package sysstat.i686 0:9.0.4-31.el6 will be erased --> finished dependency resolution dependencies resolved ======================================================================================================================================================================================================= package arch version repository size ======================================================================================================================================================================================================= removing: mysql i686 5.1.73-7.el6 @base 2.3 m mysql-libs i686 5.1.73-7.el6 @base 3.9 m mysql-server i686 5.1.73-7.el6 @base 24 m removing for dependencies: cronie i686 1.4.4-16.el6_8.2 @updates 169 k cronie-anacron i686 1.4.4-16.el6_8.2 @updates 38 k crontabs noarch 1.10-33.el6 @anaconda-centos-201311291201.i386/6.5 2.4 k perl-dbd-mysql i686 4.013-3.el6 @base 341 k postfix i686 2:2.6.6-6.el6_7.1 @base 9.3 m redhat-lsb-core i686 4.0-7.el6.centos @anaconda-centos-201311291201.i386/6.5 22 k sysstat i686 9.0.4-31.el6 @base 804 k transaction summary ======================================================================================================================================================================================================= remove 10 package(s) installed size: 41 m is this ok [y/n]: y downloading packages: running rpm_check_debug running transaction test transaction test succeeded running transaction erasing : mysql-server-5.1.73-7.el6.i686 1/10 warning: /var/log/mysqld.log saved as /var/log/mysqld.log.rpmsave erasing : redhat-lsb-core-4.0-7.el6.centos.i686 2/10 /var/tmp/rpm-tmp.y5qgjy: line 1: lsb_release: command not found erasing : mysql-5.1.73-7.el6.i686 3/10 erasing : perl-dbd-mysql-4.013-3.el6.i686 4/10 erasing : sysstat-9.0.4-31.el6.i686 5/10 erasing : crontabs-1.10-33.el6.noarch 6/10 erasing : cronie-anacron-1.4.4-16.el6_8.2.i686 7/10 erasing : cronie-1.4.4-16.el6_8.2.i686 8/10 erasing : 2:postfix-2.6.6-6.el6_7.1.i686 9/10 erasing : mysql-libs-5.1.73-7.el6.i686 10/10 verifying : redhat-lsb-core-4.0-7.el6.centos.i686 1/10 verifying : perl-dbd-mysql-4.013-3.el6.i686 2/10 verifying : mysql-server-5.1.73-7.el6.i686 3/10 verifying : crontabs-1.10-33.el6.noarch 4/10 verifying : sysstat-9.0.4-31.el6.i686 5/10 verifying : cronie-anacron-1.4.4-16.el6_8.2.i686 6/10 verifying : cronie-1.4.4-16.el6_8.2.i686 7/10 verifying : mysql-libs-5.1.73-7.el6.i686 8/10 verifying : mysql-5.1.73-7.el6.i686 9/10 verifying : 2:postfix-2.6.6-6.el6_7.1.i686 10/10 removed: mysql.i686 0:5.1.73-7.el6 mysql-libs.i686 0:5.1.73-7.el6 mysql-server.i686 0:5.1.73-7.el6 dependency removed: cronie.i686 0:1.4.4-16.el6_8.2 cronie-anacron.i686 0:1.4.4-16.el6_8.2 crontabs.noarch 0:1.10-33.el6 perl-dbd-mysql.i686 0:4.013-3.el6 postfix.i686 2:2.6.6-6.el6_7.1 redhat-lsb-core.i686 0:4.0-7.el6.centos sysstat.i686 0:9.0.4-31.el6 complete!
这样就卸载完成了,为了保险起见我们检查一下系统中是否还有 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 镜像(官网速度太慢)
然后根据自己系统配置选择对应的 rpm 包,右键复制文件链接,我的选择如下:
下载并解压
[root@coderknock ~]# wget http://mirrors.sohu.com/mysql/mysql-5.7/mysql-5.7.17-1.el6.i686.rpm-bundle.tar --2017-01-06 22:46:15-- http://mirrors.sohu.com/mysql/mysql-5.7/mysql-5.7.17-1.el6.i686.rpm-bundle.tar resolving mirrors.sohu.com... 119.188.36.70 connecting to mirrors.sohu.com|119.188.36.70|:80... connected. http request sent, awaiting response... 200 ok length: 448163840 (427m) [application/octet-stream] saving to: “mysql-5.7.17-1.el6.i686.rpm-bundle.tar” 100%[=============================================================================================================================================================>] 448,163,840 9.98m/s in 45s 2017-01-06 22:47:00 (9.54 mb/s) - “mysql-5.7.17-1.el6.i686.rpm-bundle.tar” saved [448163840/448163840] [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 loaded plugins: security setting up local package process examining mysql-community-common-5.7.17-1.el6.i686.rpm: mysql-community-common-5.7.17-1.el6.i686 marking mysql-community-common-5.7.17-1.el6.i686.rpm to be installed resolving dependencies --> running transaction check ---> package mysql-community-common.i686 0:5.7.17-1.el6 will be installed --> finished dependency resolution dependencies resolved ======================================================================================================================================================================================================= package arch version repository size ======================================================================================================================================================================================================= installing: mysql-community-common i686 5.7.17-1.el6 /mysql-community-common-5.7.17-1.el6.i686 2.5 m transaction summary ======================================================================================================================================================================================================= install 1 package(s) total size: 2.5 m installed size: 2.5 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.7.17-1.el6.i686 1/1 verifying : mysql-community-common-5.7.17-1.el6.i686 1/1 installed: mysql-community-common.i686 0:5.7.17-1.el6 complete! [root@coderknock ~]# yum localinstall mysql-community-libs-5.7.17-1.el6.i686.rpm loaded plugins: security setting up local package process examining mysql-community-libs-5.7.17-1.el6.i686.rpm: mysql-community-libs-5.7.17-1.el6.i686 marking mysql-community-libs-5.7.17-1.el6.i686.rpm to be installed resolving dependencies --> running transaction check ---> package mysql-community-libs.i686 0:5.7.17-1.el6 will be installed --> finished dependency resolution dependencies resolved ======================================================================================================================================================================================================= package arch version repository size ======================================================================================================================================================================================================= installing: mysql-community-libs i686 5.7.17-1.el6 /mysql-community-libs-5.7.17-1.el6.i686 7.8 m transaction summary ======================================================================================================================================================================================================= install 1 package(s) total size: 7.8 m installed size: 7.8 m is this ok [y/n]: y downloading packages: running rpm_check_debug running transaction test transaction test succeeded running transaction installing : mysql-community-libs-5.7.17-1.el6.i686 1/1 verifying : mysql-community-libs-5.7.17-1.el6.i686 1/1 installed: mysql-community-libs.i686 0:5.7.17-1.el6 complete! [root@coderknock ~]# yum localinstall mysql-community-client-5.7.17-1.el6.i686.rpm loaded plugins: security setting up local package process examining mysql-community-client-5.7.17-1.el6.i686.rpm: mysql-community-client-5.7.17-1.el6.i686 marking mysql-community-client-5.7.17-1.el6.i686.rpm to be installed resolving dependencies --> running transaction check ---> package mysql-community-client.i686 0:5.7.17-1.el6 will be installed --> finished dependency resolution dependencies resolved ======================================================================================================================================================================================================= package arch version repository size ======================================================================================================================================================================================================= installing: mysql-community-client i686 5.7.17-1.el6 /mysql-community-client-5.7.17-1.el6.i686 87 m transaction summary ======================================================================================================================================================================================================= install 1 package(s) total size: 87 m installed size: 87 m is this ok [y/n]: ^[[a^hy is this ok [y/n]: y downloading packages: running rpm_check_debug running transaction test transaction test succeeded running transaction installing : mysql-community-client-5.7.17-1.el6.i686 1/1 verifying : mysql-community-client-5.7.17-1.el6.i686 1/1 installed: mysql-community-client.i686 0:5.7.17-1.el6 complete! [root@coderknock ~]# yum localinstall mysql-community-server-5.7.17-1.el6.i686.rpm loaded plugins: security setting up local package process examining mysql-community-server-5.7.17-1.el6.i686.rpm: mysql-community-server-5.7.17-1.el6.i686 marking mysql-community-server-5.7.17-1.el6.i686.rpm to be installed resolving dependencies --> running transaction check ---> package mysql-community-server.i686 0:5.7.17-1.el6 will be installed --> finished dependency resolution dependencies resolved ======================================================================================================================================================================================================= package arch version repository size ======================================================================================================================================================================================================= installing: mysql-community-server i686 5.7.17-1.el6 /mysql-community-server-5.7.17-1.el6.i686 695 m transaction summary ======================================================================================================================================================================================================= install 1 package(s) total size: 695 m installed size: 695 m is this ok [y/n]: y downloading packages: running rpm_check_debug running transaction test transaction test succeeded running transaction installing : mysql-community-server-5.7.17-1.el6.i686 1/1 verifying : mysql-community-server-5.7.17-1.el6.i686 1/1 installed: mysql-community-server.i686 0:5.7.17-1.el6 complete!
配置
查看版本
[root@coderknock ~]# mysql -v mysql ver 14.14 distrib 5.7.17, for linux (i686) using editline wrapper
启动 mysql
[root@coderknock ~]# service mysqld start mysql daemon failed to start. 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 validate password plugin: [ ok ] starting mysqld: [ ok ]
这样我们就启动了 mysql 服务
mysql 无法登陆的解决办法
登录时我遇到了一些问题:
[root@coderknock ~]# mysql -u root -p enter password: error 1045 (28000): access denied for user 'root'@'localhost' (using password: yes)
这是因较新版本的 mysql 因为安全的考虑,会设置默认密码
[root@coderknock ~]# grep 'temporary password' /var/log/mysqld.log 2017-01-06t14:53:33.490540z 1 [note] a temporary password is generated for root@localhost: ei(glix_p6/1
这样就找到了密码,如果上面方法不可行可以看看是否有 /root/.mysql_secret 文件
或者通过以下方法直接修改密码:
[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 logging to '/var/log/mysqld.log'. 2017-01-06t15:02:18.666764z mysqld_safe logging to '/var/log/mysqld.log'. 2017-01-06t15:02:18.693025z mysqld_safe starting mysqld daemon with databases from /var/lib/mysql ^c [root@coderknock ~]# mysql -u root mysql reading table information for completion of table and column names you can turn off this feature to get a quicker startup with -a welcome to the mysql monitor. commands end with ; or \g. your mysql connection id is 3 server version: 5.7.17 mysql community server (gpl) copyright (c) 2000, 2016, 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>update mysql.user set authentication_string=password('你的密码.') where user='root'; query ok, 1 row affected, 1 warning (0.00 sec) rows matched: 1 changed: 1 warnings: 1 mysql> flush privileges; mysql> update mysql.user set host='%' where user='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,可以按需执行
重置密码
无论是获取到了默认密码还是强行修改了密码。新版 mysql 在第一次通过密码登录后会无法执行sql:
mysql> select host from mysql.user where user='root'; error 1820 (hy000): you must reset your password using alter user statement before executing this statement.
这是因为 mysql 为了安全会要求修改初始密码。
mysql> alter user 'root'@'%' identified by '你的密码';
如果遇到error 1819 (hy000): your password does not satisfy the current policy requirements说明你的密码太简单,需要设置复杂点的密码,有大小写字母、有数字、有标点基本上就可以通过。
注意,如果你没有修改过 root 用户的host或者修改为非 % 的字符,需要对上面的语句进行对应的修改,例如: alter user 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) - no such 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
精彩专题分享:mysql不同版本安装教程 mysql5.7各版本安装教程
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。