文章目录
在linux上使用yum 仓库安装MySQL
翻译官方文档地址:https://dev.mysql.com/doc/refman/5.6/en/linux-installation-yum-repo.html
MySQL的yum仓库 提供了Oracle linux发型版本,红帽企业linux发型版本(RHEL)以及CentOS发型版上的一些rpm包,这些rpm可以包含MySQL 服务端的安装包、MySQL客户端的安装包,MySQL Workbench,MySQL组件,MySQL路由(MySQL Router),MySQL shell,连接器/ODBC(Connector/ODBC),连接器/Python等等。
MySQL提供的yum仓库不包含所有linux发型版本,具体可以参考 使用yum仓库安装其他MySQL的产品以及组建。
自行补充翻译之外内容:大部分时候,我们关心的最多的就是MySQL server,MySQL client以及MySQL Utilities相关的rpm包;
开始之前
MySQL作为一个流行的、开源的软件,它的源代码和预安装包形式广泛的安装在来自各种源的系统之上,包括不同的软件下载站点,软件仓库等等。接下来的步骤是假设你的系统并没有使用第三方发型的RPM包管理器安装过MySQL产品。如果你已经使用的第三方rpm包管理器安装了MySQL产品,可以参考以下章节:
新安装MySQL的步骤
以下部分会从MySQL的yum 仓库 安装MySQL最新的GA 版本(目前主流的、最新的系列是MySQL 5.7的系列版本):
1、添加MySQL的yum仓库
首先把MySQL提供的yum仓库配置配置到你的yum仓库配置文件列表,这个操作是一次性的操作(只需要添加一次即可),这样你的系统才能访问到MySQL的yum仓库地址。接下来做以下步骤:
a. 在MySQL的开发区(in the MySQL Developer Zone)找到MySQL的yum仓库下载页面,(https://dev.mysql.com/downloads/repo/yum/)
b. 根据你直接的平台下载合适的软件包;
c. 使用以下命令来安装下载下来的软件包,样例中的platform-and-version-specific-package-name 要替换成你下载的rpm包名:
shell> sudo yum localinstall platform-and-version-specific-package-name.rpm
对于EL6系统,命令应该是以下类似的形式:
shell> sudo yum localinstall mysql57-community-release-el6-{version-number}.noarch.rpm
对于EL7系统,命令应该是以下类似的形式:
shell> sudo yum localinstall mysql57-community-release-el7-{version-number}.noarch.rpm
PS: 额外说明,其实CentOS 6之后(包括RHEL 6),上面localinstall可以直接替换成install。不过为了兼容性考虑,localinstall是有保留支持的。
安装命令会添加MySQL的yum仓库到你的系统yum仓库配置列表中,并且会下载安装GnuPG key,用于对安装软件包时候的软件包的一致性做校验检测。关于GnuPG key,可以参考 使用GnuPG做签名检测
可以使用以下命令检测是否软件包提供的yum仓库配置文件是否安装成功:
shell> yum repolist enabled | grep "mysql.*-community.*"
注意:
如果你安装了MySQL的yum仓库,你是用yum update这种全局更新命令将会升级MySQL仓库能升级的软件包。
具体可以参考 使用MySQL的yum仓库升级MySQL 以及 升级共享客户端库
2、选择系列版本
当使用MySQL的yum仓库的时候,默认会选择最新的GA系列(目前是MySQL 5.7版本是主流)来安装。如果当前主流版本就是你想要安装的,你可以直接跳过以下步骤,直接去 安装MySQL的步骤
在MySQL的yum仓库内,不同发型系列的MySQL社区版本是请求不同的子库的路由。最新的GA 系列(默认是MySQL 5.7系列)是启用的,其他所有系列(比如MySQL 5.6系列)默认都是关闭的。可以使用以下命令去查看所有MySQL yum仓库的字库,看看哪些是启用的哪些是禁用的:
shell> yum repolist all | grep mysql
从最近的GA系列中去安装最近的版本,去需要去额外配置。从最近的AG系列中去安装指定版本,要在安装安装命令前,禁止掉最近的子仓库,并且启用指定系列的子仓库。如果你的平台支持yum-config-manager,你可以执行这些命令,命令主要是禁止5.7系列的子仓库并且启用5.6系列的子仓库:
shell> sudo yum-config-manager --disable mysql57-community
shell> sudo yum-config-manager --enable mysql56-community
除了使用yum-config-manager命令之外,可以手动编辑/etc/yum.repos.d/mysql-community.repo文件,并且选择添加指定的系列的yum仓库连接配置。模板如下:
[mysql57-community]
name=MySQL 5.7 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/6/$basearch/
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
找到你要配置的子仓库配置,并且编辑enabled选项。如果要禁止指定子配置,可以设置enabled=0,如果要启用指定子配置,可以通过设置enabled=1。例如要使用5.6的系列,就要先禁用5.7的系列,启用5.6的系列如下:
# Enable to use MySQL 5.6
[mysql56-community]
name=MySQL 5.6 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.6-community/el/6/$basearch/
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
在任何时候,都应该只启用一个子仓库。如果有多个版本的yum子配置启用,默认yum会去选择最新版本。可以通过一下命令检查启用的子配置:
shell> yum repolist enabled | grep mysql
3、安装MySQL
用以下命令安装:
shell> sudo yum install mysql-community-server
上面安装会安装MySQL服务端需要的软件包(mysql-community-server),并且会安装运行服务端所需要的的组件包,包括客户端的包(mysql-community-client),服务端和客户端与公共错误消息和字符集相关的包(mysql-community-common),以及共享的客户端库(mysql-community-libs).
4、启动MySQL服务端
使用以下命令来启动MySQL的服务端:
shell> sudo service mysqld start
上面命令如果成功,会简单输出以下信息:
Starting mysqld:[ OK ]
可以使用以下的命令来检测MySQL服务端的运行状态:
shell> sudo service mysqld status
上面的命令会输出以下信息(一个样例):
mysqld (pid 3066) is running.
5、保护MySQL的安装(Securing the MySQL Installation)
mysql_secure_installation 程序允许你去执行诸如 给root用户设置密码(MySQL默认管理员权限用户是root),删除匿名用户(可以通过空密码登录的)等等操作。建议总是在安装后执行以下命令去加固你MySQL的安装:
shell> mysql_secure_installation
千万要记住自己设置的root用户的密码。具体可以参考: 优化MySQL安装的安全 和 安装后步骤 。
注意:
基于EL7平台的兼容信息:平台的本机软件存储库中的以下RPM包与安装MySQL Server的MySQL yum存储库中的包不兼容。一旦你是用MySQL yum仓库安装了它们,你讲不能使用安装本机存储库中的RPM包,反之亦然。
- akonadi-mysql
6、使用MySQL yum仓库安装其他MySQL的产品和组件
你可以使用yum去安装和管理特定的MySQL组件。其中的一些组件路由请求道MySQLyum仓库的子库中:例如MySQL 连接器(Connectors)可以在MySQL Connectors Community子仓库中找到,而MySQL Workbench在MySQL Tools Community中。可以使用以下命令来列出MySQL Yum仓库中符合你平台的所有的MySQL相关的可用组件:
shell> sudo yum --disablerepo=\* --enablerepo='mysql*-community*' list available
如果想要安装任意你自行选择的软件包,可以使用以下命令:
shell> sudo yum install package-name
例如,安装MySQL Workbench:
shell> sudo yum install mysql-workbench-community
去安装客户端共享库:
shell> sudo yum install mysql-community-libs
7、使用Yum升级MySQL
除了安装之外,你可以使用MySQL的yum仓库来执行对MySQL产品和组件的升级操作。具体可以参考:
8、CentOS 6上演示实例
8.1、实验环境
[[email protected] ~]# uname -a
Linux localhost.localdomain 2.6.32-754.el6.x86_64 #1 SMP Tue Jun 19 21:26:04 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
[[email protected] ~]# cat /etc/redhat-release
CentOS release 6.10 (Final)
8.2、下载最新的包含yum仓库安装文件的rpm包
https://dev.mysql.com/downloads/repo/yum/
8.3、使用rpm看上传的rpm文件
#查看安装rpm的摘要信息:
[[email protected] ~]# ls -l mysql80-community-release-el6-2.noarch.rpm
-rw-r--r--. 1 root root 25872 Mar 28 14:54 mysql80-community-release-el6-2.noarch.rpm
[[email protected] ~]# rpm -pqi mysql80-community-release-el6-2.noarch.rpm
warning: mysql80-community-release-el6-2.noarch.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Name : mysql80-community-release Relocations: (not relocatable)
Version : el6 Vendor: MySQL
Release : 2 Build Date: Thu 17 Jan 2019 09:04:24 PM CST
Install Date: (not installed) Build Host: vitro47
Group : System Environment/Base Source RPM: mysql80-community-release-el6-2.src.rpm
Size : 31571 License: GPLv2
Signature : DSA/SHA1, Fri 18 Jan 2019 12:30:58 PM CST, Key ID 8c718d3b5072e1f5
Packager : MySQL Release Engineering <[email protected]>
URL : http://dev.mysql.com
Summary : MySQL repository configuration for yum
Description :
Package for installation of setup/configuration files required for
installation of MySQL packages by yum.
# 查看安装文件列表:
[[email protected] ~]# rpm -pql mysql80-community-release-el6-2.noarch.rpm
warning: mysql80-community-release-el6-2.noarch.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
/etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
/etc/yum.repos.d/mysql-community-source.repo
/etc/yum.repos.d/mysql-community.repo
8.4、直接安装并查看内容
[[email protected] ~]# rpm -ivh mysql80-community-release-el6-2.noarch.rpm
warning: mysql80-community-release-el6-2.noarch.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing... ########################################### [100%]
1:mysql80-community-relea########################################### [100%]
[[email protected] yum.repos.d]# ls -l mysql-community*
-rw-r--r--. 1 root root 1862 Jan 17 21:04 mysql-community.repo
-rw-r--r--. 1 root root 1885 Jan 17 21:04 mysql-community-source.repo
[[email protected] yum.repos.d]# pwd
/etc/yum.repos.d
#上文中,/etc/yum.repos.d目录下,rpm包安装的两个yum的配置。
其中mysql-community.repo是指向mysql社区版的二进制包的yum配置,
mysql-community-source.repo是指向mysql的rpm包的源码包的yum配置。
#查看所有的和mysql相关的repo
[[email protected] ~]# yum repolist all | grep mysql
mysql-cluster-7.5-community MySQL Cluster 7.5 Community disabled
mysql-cluster-7.5-community-source MySQL Cluster 7.5 Community - disabled
mysql-cluster-7.6-community MySQL Cluster 7.6 Community disabled
mysql-cluster-7.6-community-source MySQL Cluster 7.6 Community - disabled
mysql-connectors-community MySQL Connectors Community enabled: 83
mysql-connectors-community-source MySQL Connectors Community - S disabled
mysql-tools-community MySQL Tools Community enabled: 74
mysql-tools-community-source MySQL Tools Community - Source disabled
mysql-tools-preview MySQL Tools Preview disabled
mysql-tools-preview-source MySQL Tools Preview - Source disabled
mysql55-community MySQL 5.5 Community Server disabled
mysql55-community-source MySQL 5.5 Community Server - S disabled
mysql56-community MySQL 5.6 Community Server disabled
mysql56-community-source MySQL 5.6 Community Server - S disabled
mysql57-community MySQL 5.7 Community Server disabled
mysql57-community-source MySQL 5.7 Community Server - S disabled
mysql80-community MySQL 8.0 Community Server enabled: 72
mysql80-community-source MySQL 8.0 Community Server - S disabled
#查看默认启用的和mysql相关的repo
[[email protected] ~]# yum repolist enabled | grep "mysql.*-community.*"
mysql-connectors-community MySQL Connectors Community 83
mysql-tools-community MySQL Tools Community 74
mysql80-community MySQL 8.0 Community Server 72
8.5、设置启用MySQL 5.6的repo并安装
#设置启用5.6的repo,禁止5.7和8.0的
#要用到yum-config-manager这个工具,或者知道yum客户端配置原理的,可以手动修改/etc/yum.repos.d目录下对应的yum 仓库配置
#yum-config-manager工具属于yum-utils软件包,默认最小化安装系统没有安装
[[email protected] ~]# rpm -qf /usr/bin/yum-config-manager
yum-utils-1.1.30-42.el6_10.noarch
[[email protected] ~]# yum-config-manager --help #查看帮助信息
##省略......
[[email protected] ~]# yum-config-manager --disable mysql80-community
#默认会导出修改后的yum仓库配置内容到标准输出,这里省略了。
[[email protected] ~]# yum-config-manager --enable mysql56-community
#同理
#再次查看启用的repo,mysql56-community这个repo显示出来了。
[[email protected] ~]# yum repolist enabled | grep "mysql.*-community.*"
mysql-connectors-community MySQL Connectors Community 83
mysql-tools-community MySQL Tools Community 74
mysql56-community MySQL 5.6 Community Server 493
###yum查看一下mysql的安装包
[[email protected] ~]# yum list|grep '^mysql'
mysql-libs.x86_64 5.1.73-8.el6_8 @anaconda-CentOS-201806291108.x86_64/6.10
mysql80-community-release.noarch el6-2 installed
mysql.x86_64 5.1.73-8.el6_8 base
mysql-bench.x86_64 5.1.73-8.el6_8 base
mysql-community-bench.i686 5.6.42-2.el6 mysql56-community
mysql-community-bench.x86_64 5.6.43-2.el6 mysql56-community
mysql-community-client.i686 5.6.43-2.el6 mysql56-community
mysql-community-client.x86_64 5.6.43-2.el6 mysql56-community
mysql-community-common.i686 5.6.43-2.el6 mysql56-community
mysql-community-common.x86_64 5.6.43-2.el6 mysql56-community
mysql-community-devel.i686 5.6.43-2.el6 mysql56-community
mysql-community-devel.x86_64 5.6.43-2.el6 mysql56-community
mysql-community-embedded.i686 5.6.43-2.el6 mysql56-community
mysql-community-embedded.x86_64 5.6.43-2.el6 mysql56-community
mysql-community-embedded-devel.i686 5.6.43-2.el6 mysql56-community
mysql-community-embedded-devel.x86_64 5.6.43-2.el6 mysql56-community
mysql-community-libs.i686 5.6.43-2.el6 mysql56-community
mysql-community-libs.x86_64 5.6.43-2.el6 mysql56-community
mysql-community-libs-compat.i686 5.6.43-2.el6 mysql56-community
mysql-community-libs-compat.x86_64 5.6.43-2.el6 mysql56-community
mysql-community-release.noarch el6-5 mysql-connectors-community
mysql-community-server.i686 5.6.42-2.el6 mysql56-community
mysql-community-server.x86_64 5.6.43-2.el6 mysql56-community
mysql-community-test.x86_64 5.6.43-2.el6 mysql56-community
mysql-connector-c++.x86_64 8.0.15-1.el6 mysql-connectors-community
mysql-connector-c++-devel.x86_64 8.0.15-1.el6 mysql-connectors-community
mysql-connector-c++-jdbc.x86_64 8.0.15-1.el6 mysql-connectors-community
mysql-connector-java.noarch 1:5.1.17-6.el6 base
mysql-connector-odbc.x86_64 8.0.15-1.el6 mysql-connectors-community
mysql-connector-odbc-debuginfo.x86_64 8.0.15-1.el6 mysql-connectors-community
mysql-connector-odbc-setup.x86_64 8.0.15-1.el6 mysql-connectors-community
mysql-connector-python.noarch 2.0.4-1.el6 mysql-connectors-community
mysql-connector-python.x86_64 2.1.7-1.el6 mysql-connectors-community
mysql-connector-python-cext.x86_64 2.1.7-1.el6 mysql-connectors-community
mysql-connector-python-debuginfo.x86_64 2.1.7-1.el6 mysql-connectors-community
mysql-devel.i686 5.1.73-8.el6_8 base
mysql-devel.x86_64 5.1.73-8.el6_8 base
mysql-embedded.i686 5.1.73-8.el6_8 base
mysql-embedded.x86_64 5.1.73-8.el6_8 base
mysql-embedded-devel.i686 5.1.73-8.el6_8 base
mysql-embedded-devel.x86_64 5.1.73-8.el6_8 base
mysql-libs.i686 5.1.73-8.el6_8 base
mysql-ref-manual-5.6-en-html-chapter.noarch
mysql-ref-manual-5.6-en-pdf.noarch 1-20181222 mysql56-community
mysql-router.x86_64 8.0.12-1.el6 mysql-tools-community
mysql-router-community.x86_64 8.0.15-1.el6 mysql-tools-community
mysql-router-debuginfo.x86_64 8.0.12-1.el6 mysql-tools-community
mysql-server.x86_64 5.1.73-8.el6_8 base
mysql-shell.x86_64 8.0.15-1.el6 mysql-tools-community
mysql-shell-debuginfo.x86_64 8.0.15-1.el6 mysql-tools-community
mysql-test.x86_64 5.1.73-8.el6_8 base
mysql-utilities.noarch 1.6.5-1.el6 mysql-tools-community
mysql-utilities-extra.noarch 1.5.6-1.el6 mysql-tools-community
mysql-workbench-community.x86_64 6.3.8-1.el6 mysql-tools-community
mysql-workbench-community-debuginfo.x86_64 6.3.8-1.el6 mysql-tools-community
#直接安装
yum install mysql-community-server
PS:如果忍受不了在线安装的速度,下面给出笔者演示时指定MySQL 5.6版本的最新的资源链接(建议自行使用专门下载工具下载)
http://repo.mysql.com/yum/mysql-5.6-community/el/6/x86_64/mysql-community-client-5.6.43-2.el6.x86_64.rpm
http://repo.mysql.com/yum/mysql-5.6-community/el/6/x86_64/mysql-community-common-5.6.43-2.el6.x86_64.rpm
http://repo.mysql.com/yum/mysql-5.6-community/el/6/x86_64/mysql-community-devel-5.6.43-2.el6.x86_64.rpm
http://repo.mysql.com/yum/mysql-5.6-community/el/6/x86_64/mysql-community-embedded-devel-5.6.43-2.el6.x86_64.rpm
http://repo.mysql.com/yum/mysql-5.6-community/el/6/x86_64/mysql-community-libs-5.6.43-2.el6.x86_64.rpm
http://repo.mysql.com/yum/mysql-5.6-community/el/6/x86_64/mysql-community-libs-compat-5.6.43-2.el6.x86_64.rpm
http://repo.mysql.com/yum/mysql-5.6-community/el/6/x86_64/mysql-community-server-5.6.43-2.el6.x86_64.rpm
上述文件下载到本地后,可以上传到某个linux文件系统目录下,然后使用yum通配安装所有rpm包,会自行解决依赖关系的。
#查看安装的软件包
[[email protected] ~]# rpm -qa |grep mysql
mysql80-community-release-el6-2.noarch
mysql-community-common-5.6.43-2.el6.x86_64
mysql-community-client-5.6.43-2.el6.x86_64
mysql-community-server-5.6.43-2.el6.x86_64
mysql-community-libs-5.6.43-2.el6.x86_64
mysql-community-libs-compat-5.6.43-2.el6.x86_64
[[email protected] ~]# mysql -V
mysql Ver 14.14 Distrib 5.6.43, for Linux (x86_64) using EditLine wrapper
然后第一次启动服务器会初始化,由于内容过多,不一一列出:
[[email protected] ~]# ls -l /etc/init.d/mysqld
[[email protected] ~]# service mysqld start
Initializing MySQL database: 2019-03-28 15:52:50 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2019-03-28 15:52:50 0 [Note] Ignoring --secure-file-priv value as server is running with --bootstrap.
2019-03-28 15:52:50 0 [Note] /usr/sbin/mysqld (mysqld 5.6.43) starting as process 1770 ...
2019-03-28 15:52:50 1770 [Note] InnoDB: Using atomics to ref count buffer pool pages
2019-03-28 15:52:50 1770 [Note] InnoDB: The InnoDB memory heap is disabled
2019-03-28 15:52:50 1770 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
......
[[email protected] ~]# service mysqld status
mysqld (pid 2022) is running...
[[email protected] ~]# ss -tnl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 80 :::3306 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 128 *:22 *:*
LISTEN 0 100 ::1:25 :::*
LISTEN 0 100 127.0.0.1:25 *:*
[[email protected] ~]# ps aux|grep mysqld
root 1818 0.0 0.1 108316 1644 pts/0 S 15:52 0:00 /bin/sh /usr/bin/mysqld_safe --datadir=/var/lib/mysql --socket=/var/lib/mysql/mysql.sock --pid-file=/var/run/mysqld/mysqld.pid --basedir=/usr --user=mysql
mysql 2022 2.6 45.4 1406720 455480 pts/0 Sl 15:52 0:01 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --user=mysql --log-error=/var/log/mysqld.log --pid-file=/var/run/mysqld/mysqld.pid --socket=/var/lib/mysql/mysql.sock
root 2091 0.0 0.0 103320 884 pts/0 S+ 15:53 0:00 grep mysqld
[[email protected] ~]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.43 MySQL Community Server (GPL)
Copyright (c) 2000, 2019, 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> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)
上面最新一步的安装过程,如果网络不好或者测试时段不好,可能会非常慢.可以直接访问下面截图那边,去对应的版本去下载需要的包(比如利用迅雷这样的下载工具,速度应该会非常快):