Linux(CentOS7)下rpm方式安装mysql5.6.29

前言

这几天准备尝试一下数据备份,想起来在本机虚拟机上装一下mysql,都说很简单。但装起来也遇到了各种问题,也花了几个小时吧。现在总结一下,把过程贴出来,让自己以后比较熟悉,也给大家参考一下。

环境

过程

  1. 检查系统是否已经安装了mysql mariadb
rpm -qa | grep -i mysql
rpm -qa | grep -i mariadb

由于CentOS默认的数据库是mariadb,这个会和mysql冲突,所以一般都会先卸载一下
rpm -e mariadb-libs-1:5.5.44-2.el7.centos.x86_64 --nodeps
另外如果显示出来安装了的mysql,用以下命令卸载,如有多个用空格隔开多个rpm,如

rpm -e --nodeps  MySQL-devel-5.6.29-1.rhel5.x86_64 MySQL-client-5.6.29-1.rhel5.x86_64 MySQL-server-5.6.29-1.rhel5.x86_64

如果在安装过程中有提示找不到Perl或者net-tools等,按提示yum安装即可

yun install Perl
yum install net-tools
yum install -y perl-Module-Install.noarch 
  1. 安装mysql头文件,服务器及客户端
rpm -ivh MySQL-devel-5.6.29-1.rhel5.x86_64.rpm --nosignature
rpm -ivh MySQL-server-5.6.29-1.rhel5.x86_64.rpm --nosignature
rpm -ivh MySQL-client-5.6.29-1.rhel5.x86_64.rpm --nosignature

正常情况下这样命令不会出问题,但如果出现

警告:MySQL-server-5.5.46-1.linux2.6.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY

可以尝试:

rpm -ivh MySQL-devel-5.6.29-1.rhel5.x86_64.rpm --force --nodeps
  1. 启动mysql服务
service mysql start

但貌似就是这个过程没有那么容易一次成功,先贴出来正常启动成功的tips:

Starting MySQL SUCCESS! 

同样需要注意的是常见的错误提示:

  • error1
Failed to issue method call: Unit mysql.service failed to load: No such file or directory

可以尝试一下命令:

systemctl start mysql.service

但很有可能继续报错,输入一下命令:

systemctl enable mysql.service
  • error2
Starting MySQL. ERROR! The server quit without updating PID file (/var/lib/...)

出现这个问题的原因特别多,网上也有很多总结,贴一个看看
MySQL提示:The server quit without updating PID file问题的解决办法
我逐一的去试了一下,都没什么用。。可能和初装用户的报错点还是不太一样吧。
但是解决办法最重要的是要有思路,而不是一头雾水的乱撞,那合理的途径是什么呢!就是看日志啊!!
经检查,日志所在目录为vi /var/lib/mysql/localhost.localdomain.err
贴一下错误日志

160826 20:52:49 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
2016-08-26 20:52:49 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2016-08-26 20:52:49 0 [Note] /usr/sbin/mysqld (mysqld 5.6.29) starting as process 7276 ...
2016-08-26 20:52:49 7276 [Note] Plugin 'FEDERATED' is disabled.
/usr/sbin/mysqld: Table 'mysql.plugin' doesn't exist
2016-08-26 20:52:49 7276 [ERROR] Can't open the mysql.plugin table. Please run mysql_upgrade to create it.
2016-08-26 20:52:49 7276 [Note] InnoDB: Using atomics to ref count buffer pool pages
2016-08-26 20:52:49 7276 [Note] InnoDB: The InnoDB memory heap is disabled
2016-08-26 20:52:49 7276 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2016-08-26 20:52:49 7276 [Note] InnoDB: Memory barrier is not used
2016-08-26 20:52:49 7276 [Note] InnoDB: Compressed tables use zlib 1.2.3
2016-08-26 20:52:49 7276 [Note] InnoDB: Using Linux native AIO
2016-08-26 20:52:49 7276 [Note] InnoDB: Using CPU crc32 instructions
2016-08-26 20:52:49 7276 [Note] InnoDB: Initializing buffer pool, size = 128.0M
2016-08-26 20:52:49 7276 [Note] InnoDB: Completed initialization of buffer pool
2016-08-26 20:52:49 7276 [Note] InnoDB: Highest supported file format is Barracuda.
2016-08-26 20:52:49 7276 [Note] InnoDB: 128 rollback segment(s) are active.
2016-08-26 20:52:49 7276 [Note] InnoDB: Waiting for purge to start
2016-08-26 20:52:50 7276 [Note] InnoDB: 5.6.29 started; log sequence number 1600647
2016-08-26 20:52:50 7276 [Note] Server hostname (bind-address): '*'; port: 3306
2016-08-26 20:52:50 7276 [Note] IPv6 is available.
2016-08-26 20:52:50 7276 [Note]   - '::' resolves to '::';
2016-08-26 20:52:50 7276 [Note] Server socket created on IP: '::'.
2016-08-26 20:52:50 7276 [ERROR] Fatal error: Can't open and lock privilege tables: Table 'mysql.user' doesn't exist

错误主要有两条:

[ERROR] Can't open the mysql.plugin table. Please run mysql_upgrade to create it.
[ERROR] Fatal error: Can't open and lock privilege tables: Table 'mysql.user' doesn't exist

我的处理方法:

#1.需要初始化数据库user表权限关系
find / -name mysql_install_db
#执行命令得到mysql服务地址,如/usr/bin/mysql_install_db,执行:
/usr/bin/mysql_install_db --user=mysql
#2.编辑my.cnf
vi /etc/my.cnf
#在[mysqld]下面加上
explicit_defaults_for_timestamp
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
#3.编辑my-default.cnf 
vi /usr/share/mysql/my-default.cnf
#同样在[mysqld]下面加上
explicit_defaults_for_timestamp
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

提示:
在调试错误的过程中,建议多开几个Session,一边看错误日志,一边执行命令,还可以留一个来不断执行开启mysql服务命令看效果

调试

安装成功之后,执行mysql进入

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.6.29 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> 

这个时候mysql是没有密码的,所以可以直接访问。

  1. 设置开机启动
#查看是否有MySQL开机启动:
chkconfig --list | grep mysql*  
#设置开机启动:
chkconfig --add mysql
#再次运行查看mysql开机启动命令,可以看到下面显示即开启成功
chkconfig --list | grep mysql*  
mysqld          0:off   1:off   2:on    3:on    4:on    5:on    6:off 
  1. 设置mysql密码
mysql_secure_installation

根据提示按照自己的需求设置好密码以及相关配置即可,重启生效:

service mysql restart

登陆:

mysql -u root -p
  1. 设置utf-8编码格式
    查看mysql编码格式
mysql> SHOW VARIABLES LIKE 'character%';

存在默认的latin1编码格式时:

#编辑my.cnf
vi /etc/my.cnf
#在[mysqld]下面加上
port = 3306
character-set-server=utf8 

设置成功后重启再次查看编码:

service mysql restart
mysql -u root -p
mysql> SHOW VARIABLES LIKE 'character%';

显示如下:

mysql> SHOW VARIABLES LIKE 'character%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | utf8                       |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | utf8                       |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)

表示成功!
好了~就介绍到这里啦,支持的点个喜欢吧。

参考

http://bbs.csdn.net/topics/390369836?page=1
http://my.oschina.net/wangrikui/blog/688756
http://blog.csdn.net/a2011480169/article/details/51912771
http://www.ithao123.cn/content-10734293.html
http://blog.csdn.net/superchanon/article/details/8546254/
http://www.th7.cn/db/mysql/201408/69235.shtml
http://blog.csdn.net/intergameover/article/details/48128939
http://blog.csdn.net/u012689588/article/details/14127621

推荐阅读更多精彩内容