yum install mysql-community-server yum方式安装mysql(社区版实操)
阅读原文时间:2023年07月09日阅读:32

前言:rpm方式或者这种yum安装时比较简单的方式,但是不推荐,但是确实很着急的话,可以采用这种安装
**这种方式不利于后续对mysql的管理,如果是多实例或者是复杂的一些架构的话,还是推荐利用源码包编译方式进行安装。
**这篇文章看似简单,但是我修正了多次,所以后续还会继续补充和纠正,尽量不能给其他人造成错误的引导。

首先查看操作系统版本,操作系统采用的是centos(非debian等操作系统),其他操作系统命令会不一样,包括配置文件以及默认安装路径等,所以初步了解操作系统很必要
安装之前需要检查一下机器上是否有mysql,同时要对软件本身要所有了解,包括该软件依赖的相关包或系统版本过低都可能造成安装当中的问题. 比如:你安装k8s 二进制版本的时候,如果系统是centos 6 基本你的docker就安装不上,或者遇到各种各样的令人头痛的问题,后续维护成本也会很大,所以最起码安装docker要在centos7.* 以上。

[root@fp-web-118 local]# cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core)

[root@fp-web-118 local]# rpm -qa | grep mysql

执行安装命令

[root@fp-web-118 local]# yum install mysql-community-server
Loaded plugins: fastestmirror, langpacks
Determining fastest mirrors
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
No package mysql-community-server available.
Error: Nothing to do
注意:rpm方式是 rpm -ivh MySQL-server-5.0.9-0.i386.rpm, 他们之间有区别

发现安装提示错误什么也没干,查看yum源,yum源一般在/etc/yum.repos.d中(源的后缀是.repo)

[root@fp-web-118 local]# ll /etc/yum.repos.d/
total 36
-rw-r--r-- 1 root root 2523 Feb 15 2019 CentOS-Base.repo
-rw-r--r--. 1 root root 1309 Dec 9 2015 CentOS-CR.repo
-rw-r--r--. 1 root root 649 Dec 9 2015 CentOS-Debuginfo.repo
-rw-r--r--. 1 root root 290 Dec 9 2015 CentOS-fasttrack.repo
-rw-r--r--. 1 root root 630 Dec 9 2015 CentOS-Media.repo
-rw-r--r--. 1 root root 1331 Dec 9 2015 CentOS-Sources.repo
-rw-r--r--. 1 root root 1952 Dec 9 2015 CentOS-Vault.repo
-rw-r--r-- 1 root root 664 Feb 15 2019 epel.repo
-rw-r--r-- 1 root root 401 Feb 15 2016 zabbix.repo

在CentOS 6.x版本中的确存在MySQL的安装包,但CentOS 7.x之后版本,MySQL的yum源被踢出,取而代之的是MariaDB源 。 但是我们也没有看到,可能系统被改动过,因为用的是云服务。
所以最好你也看下 rpm -qa | grep MariaDB

安装sz和rz命令(注:由于没有mysql.repo,需要上传mysql.repo yum源)

[root@fp-web-118 yum.repos.d]# yum install lrzsz
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
Resolving Dependencies
--> Running transaction check
---> Package lrzsz.x86_64 0:0.12.20-36.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

===================================================================================
Package Arch Version Repository Size
====================================================================================
Installing:
lrzsz x86_64 0.12.20-36.el7 base 78 k

Transaction Summary
=====================================================================================
Install 1 Package

Total download size: 78 k
Installed size: 181 k
Is this ok [y/d/N]: ys^Hes
Is this ok [y/d/N]: yes
Downloading packages:
lrzsz-0.12.20-36.el7.x86_64.rpm | 78 kB 00:00:00 
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : lrzsz-0.12.20-36.el7.x86_64 1/1 
Verifying : lrzsz-0.12.20-36.el7.x86_64 1/1

Installed:
lrzsz.x86_64 0:0.12.20-36.el7

Complete!

利用rz 命令上传yum 源(这个yum源mysql-community.repo是从其他机器上copy过来的) , 看看上传以后的列表

[root@fp-web-118 yum.repos.d]# ll
total 40
-rw-r--r-- 1 root root 2523 Feb 15 2019 CentOS-Base.repo
-rw-r--r--. 1 root root 1309 Dec 9 2015 CentOS-CR.repo
-rw-r--r--. 1 root root 649 Dec 9 2015 CentOS-Debuginfo.repo
-rw-r--r--. 1 root root 290 Dec 9 2015 CentOS-fasttrack.repo
-rw-r--r--. 1 root root 630 Dec 9 2015 CentOS-Media.repo
-rw-r--r--. 1 root root 1331 Dec 9 2015 CentOS-Sources.repo
-rw-r--r--. 1 root root 1952 Dec 9 2015 CentOS-Vault.repo
-rw-r--r-- 1 root root 664 Feb 15 2019 epel.repo
-rw-r--r-- 1 root root 1862 May 16 2018 mysql-community.repo
-rw-r--r-- 1 root root 401 Feb 15 2016 zabbix.repo

我们也可以利用其他方式,安装mysql repo源

#下载官方的MySQLyum源,在阿里源中可下载对应的源。

测试,我备份了我的mysql repo源(这块是我在后补这篇文档的时候做了一个测试,就是repo源的安装,不过发现阿里这些源有时候也找不到有些包,比如k8s的基础镜像包)
[root@fp-web-118 yum.repos.d]# ll
total 40
-rw-r--r-- 1 root root 2523 Feb 15 2019 CentOS-Base.repo
-rw-r--r--. 1 root root 1309 Dec 9 2015 CentOS-CR.repo
-rw-r--r--. 1 root root 649 Dec 9 2015 CentOS-Debuginfo.repo
-rw-r--r--. 1 root root 290 Dec 9 2015 CentOS-fasttrack.repo
-rw-r--r--. 1 root root 630 Dec 9 2015 CentOS-Media.repo
-rw-r--r--. 1 root root 1331 Dec 9 2015 CentOS-Sources.repo
-rw-r--r--. 1 root root 1952 Dec 9 2015 CentOS-Vault.repo
-rw-r--r-- 1 root root 664 Feb 15 2019 epel.repo
-rw-r--r-- 1 root root 1862 Jan 30 20:11 mysql-community.repo
-rw-r--r-- 1 root root 401 Feb 15 2016 zabbix.repo
[root@fp-web-118 yum.repos.d]# cp mysql-community.repo mysql-community.repo.bak

[root@fp-web-118 src]# wget https://dev.mysql.com/get/mysql80-community-release-el7-1.noarch.rpm
--2022-02-01 16:56:59-- https://dev.mysql.com/get/mysql80-community-release-el7-1.noarch.rpm
Resolving dev.mysql.com (dev.mysql.com)… 137.254.60.11
Connecting to dev.mysql.com (dev.mysql.com)|137.254.60.11|:443… connected.
HTTP request sent, awaiting response… 302 Found
Location: https://repo.mysql.com//mysql80-community-release-el7-1.noarch.rpm [following]
--2022-02-01 16:56:59-- https://repo.mysql.com//mysql80-community-release-el7-1.noarch.rpm
Resolving repo.mysql.com (repo.mysql.com)… 23.58.112.230
Connecting to repo.mysql.com (repo.mysql.com)|23.58.112.230|:443… connected.
HTTP request sent, awaiting response… 200 OK
Length: 25820 (25K) [application/x-redhat-package-manager]
Saving to: ‘mysql80-community-release-el7-1.noarch.rpm’

100%[=====================================================================================================================================================>] 25,820 129KB/s in 0.2s

2022-02-01 16:57:01 (129 KB/s) - ‘mysql80-community-release-el7-1.noarch.rpm’ saved [25820/25820]

##下载到的/usr/local/src下
[root@fp-web-118 src]# ll | grep mysql
-rw-r--r-- 1 root root 25820 Apr 18 2018 mysql80-community-release-el7-1.noarch.rpm

[root@fp-web-118 src]# rpm -ivh mysql80-community-release-el7-1.noarch.rpm
warning: mysql80-community-release-el7-1.noarch.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing… ################################# [100%]
Updating / installing…
1:mysql80-community-release-el7-1 ################################# [100%]
warning: /etc/yum.repos.d/mysql-community.repo created as /etc/yum.repos.d/mysql-community.repo.rpmnew

#安装刚刚从官方下载的yum源,会在"/etc/yum.repos.d/"下生成相应的mysql社区版的repo配置文件。
warning: mysql80-community-release-el7-1.noarch.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing… ################################# [100%]
Updating / installing…
1:mysql80-community-release-el7-1 ################################# [100%]
[root@fp-web-118 src]# ll /etc/yum.repos.d/ | grep mysql
-rw-r--r-- 1 root root 1862 Jan 30 20:11 mysql-community.repo
-rw-r--r-- 1 root root 1862 Feb 1 16:56 mysql-community.repo.bak   ###这个是我之前做的备份,这篇文章是用的这个repo源
-rw-r--r-- 1 root root 1864 Feb 22 2018 mysql-community.repo.rpmnew
-rw-r--r-- 1 root root 1885 Feb 22 2018 mysql-community-source.repo

重新执行安装mysql rpm命令

[root@fp-web-118 yum.repos.d]# yum install mysql-community-server
Loaded plugins: fastestmirror, langpacks
mysql-connectors-community | 2.6 kB 00:00:00 
mysql-tools-community | 2.6 kB 00:00:00 
mysql56-community | 2.6 kB 00:00:00 
(1/3): mysql-connectors-community/x86_64/primary_db | 76 kB 00:00:00 
(2/3): mysql56-community/x86_64/primary_db | 338 kB 00:00:01 
(3/3): mysql-tools-community/x86_64/primary_db | 73 kB 00:00:01 
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
Resolving Dependencies
--> Running transaction check
---> Package mysql-community-server.x86_64 0:5.6.51-2.el6 will be installed
--> Processing Dependency: mysql-community-common(x86-64) = 5.6.51-2.el6 for package: mysql-community-server-5.6.51-2.el6.x86_64
--> Processing Dependency: mysql-community-client(x86-64) >= 5.6.10 for package: mysql-community-server-5.6.51-2.el6.x86_64
--> Processing Dependency: perl(DBI) for package: mysql-community-server-5.6.51-2.el6.x86_64
--> Processing Dependency: perl(Data::Dumper) for package: mysql-community-server-5.6.51-2.el6.x86_64
--> Running transaction check
---> Package mysql-community-client.x86_64 0:5.6.51-2.el6 will be installed
--> Processing Dependency: mysql-community-libs(x86-64) >= 5.6.10 for package: mysql-community-client-5.6.51-2.el6.x86_64
---> Package mysql-community-common.x86_64 0:5.6.51-2.el6 will be installed
---> Package perl-DBI.x86_64 0:1.627-4.el7 will be installed
--> Processing Dependency: perl(RPC::PlServer) >= 0.2001 for package: perl-DBI-1.627-4.el7.x86_64
--> Processing Dependency: perl(RPC::PlClient) >= 0.2000 for package: perl-DBI-1.627-4.el7.x86_64
---> Package perl-Data-Dumper.x86_64 0:2.145-3.el7 will be installed
--> Running transaction check
---> Package mysql-community-libs.x86_64 0:5.6.51-2.el6 will be installed
---> Package perl-PlRPC.noarch 0:0.2020-14.el7 will be installed
--> Processing Dependency: perl(Net::Daemon) >= 0.13 for package: perl-PlRPC-0.2020-14.el7.noarch
--> Processing Dependency: perl(Net::Daemon::Test) for package: perl-PlRPC-0.2020-14.el7.noarch
--> Processing Dependency: perl(Net::Daemon::Log) for package: perl-PlRPC-0.2020-14.el7.noarch
--> Processing Dependency: perl(Compress::Zlib) for package: perl-PlRPC-0.2020-14.el7.noarch
--> Running transaction check
---> Package perl-IO-Compress.noarch 0:2.061-2.el7 will be installed
--> Processing Dependency: perl(Compress::Raw::Zlib) >= 2.061 for package: perl-IO-Compress-2.061-2.el7.noarch
--> Processing Dependency: perl(Compress::Raw::Bzip2) >= 2.061 for package: perl-IO-Compress-2.061-2.el7.noarch
---> Package perl-Net-Daemon.noarch 0:0.48-5.el7 will be installed
--> Running transaction check
---> Package perl-Compress-Raw-Bzip2.x86_64 0:2.061-3.el7 will be installed
---> Package perl-Compress-Raw-Zlib.x86_64 1:2.061-4.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

===========================================================================
Package Arch Version Repository Size
===========================================================================
Installing:
mysql-community-server x86_64 5.6.51-2.el6 mysql56-community 62 M
Installing for dependencies:
mysql-community-client x86_64 5.6.51-2.el6 mysql56-community 20 M
mysql-community-common x86_64 5.6.51-2.el6 mysql56-community 340 k
mysql-community-libs x86_64 5.6.51-2.el6 mysql56-community 2.2 M
perl-Compress-Raw-Bzip2 x86_64 2.061-3.el7 base 32 k
perl-Compress-Raw-Zlib x86_64 1:2.061-4.el7 base 57 k
perl-DBI x86_64 1.627-4.el7 base 802 k
perl-Data-Dumper x86_64 2.145-3.el7 base 47 k
perl-IO-Compress noarch 2.061-2.el7 base 260 k
perl-Net-Daemon noarch 0.48-5.el7 base 51 k
perl-PlRPC noarch 0.2020-14.el7 base 36 k

Transaction Summary
================================================================================
Install 1 Package (+10 Dependent packages)

Total download size: 86 M
Installed size: 355 M
Is this ok [y/d/N]: yes
Downloading packages:
warning: /var/cache/yum/x86_64/7/mysql56-community/packages/mysql-community-common-5.6.51-2.el6.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY47 kB/s | 280 kB 00:09:58 ETA 
Public key for mysql-community-common-5.6.51-2.el6.x86_64.rpm is not installed
(1/11): mysql-community-common-5.6.51-2.el6.x86_64.rpm | 340 kB 00:00:01 
(2/11): mysql-community-libs-5.6.51-2.el6.x86_64.rpm | 2.2 MB 00:00:02 
(3/11): perl-Compress-Raw-Bzip2-2.061-3.el7.x86_64.rpm | 32 kB 00:00:00 
(4/11): perl-Compress-Raw-Zlib-2.061-4.el7.x86_64.rpm | 57 kB 00:00:00 
(5/11): perl-Data-Dumper-2.145-3.el7.x86_64.rpm | 47 kB 00:00:00 
(6/11): perl-DBI-1.627-4.el7.x86_64.rpm | 802 kB 00:00:00 
(7/11): perl-Net-Daemon-0.48-5.el7.noarch.rpm | 51 kB 00:00:00 
(8/11): perl-PlRPC-0.2020-14.el7.noarch.rpm | 36 kB 00:00:00 
(9/11): perl-IO-Compress-2.061-2.el7.noarch.rpm | 260 kB 00:00:00 
(10/11): mysql-community-client-5.6.51-2.el6.x86_64.rpm | 20 MB 00:00:19 
(11/11): mysql-community-server-5.6.51-2.el6.x86_64.rpm | 62 MB 00:00:36 
------------------------------------------------------------------------------------------
Total 2.1 MB/s | 86 MB 00:00:40 
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

GPG key retrieval failed: [Errno 14] curl#37 - "Couldn't open file /etc/pki/rpm-gpg/RPM-GPG-KEY-mysql"

解决这个问题

gpg key retrieval failed 意思是在/etc/pki/rpm-gpg/中找不到RPM-GPG-KEY-mysql
第一种方案,设置gpgcheck为0,就是不需要检查gpgkey。
# 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=0
#gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

第二种方案,指定外网gpgkey的值
# 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=0 gpgkey=http://springdale.math.ias.edu/data/puias/6/x86_64/os/RPM-GPG-KEY-puias

第三种方案,下载gpgkey
//下载RPM-GPG-KEY-mysql
wget http://repo.mysql.com/RPM-GPG-KEY-mysql -O /etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

//MySQL中gpgkey指定路径
# 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源文件 mysql-community.repo

[root@fp-web-118 yum.repos.d]#  vi mysql-community.repo

# Enable to use MySQL 5.5
[mysql55-community]
name=MySQL 5.5 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.5-community/el/6/$basearch/
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

# 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  ##这里修改为0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

# Enable to use MySQL 5.7
[mysql57-community]
name=MySQL 5.7 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/6/$basearch/
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

[mysql80-community]
name=MySQL 8.0 Community Server
baseurl=http://repo.mysql.com/yum/mysql-8.0-community/el/6/$basearch/
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

[mysql-connectors-community]
name=MySQL Connectors Community
baseurl=http://repo.mysql.com/yum/mysql-connectors-community/el/6/$basearch/
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

[mysql-tools-community]
name=MySQL Tools Community
baseurl=http://repo.mysql.com/yum/mysql-tools-community/el/6/$basearch/
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

[mysql-tools-preview]
name=MySQL Tools Preview
baseurl=http://repo.mysql.com/yum/mysql-tools-preview/el/6/$basearch/
enabled=0
gpgcheck=1
gpgkey=file:/etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

[mysql-cluster-7.5-community]
name=MySQL Cluster 7.5 Community
baseurl=http://repo.mysql.com/yum/mysql-cluster-7.5-community/el/6/$basearch/
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

[mysql-cluster-7.6-community]
name=MySQL Cluster 7.6 Community
baseurl=http://repo.mysql.com/yum/mysql-cluster-7.6-community/el/6/$basearch/
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

保存,退出

重新执行 mysql安装命令

[root@fp-web-118 yum.repos.d]# yum install mysql-community-server
Loaded plugins: fastestmirror, langpacks
mysql-connectors-community | 2.6 kB 00:00:00 
mysql-tools-community | 2.6 kB 00:00:00 
mysql56-community | 2.6 kB 00:00:00 
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
Resolving Dependencies
--> Running transaction check
---> Package mysql-community-server.x86_64 0:5.6.51-2.el6 will be installed
--> Processing Dependency: mysql-community-common(x86-64) = 5.6.51-2.el6 for package: mysql-community-server-5.6.51-2.el6.x86_64
--> Processing Dependency: mysql-community-client(x86-64) >= 5.6.10 for package: mysql-community-server-5.6.51-2.el6.x86_64
--> Processing Dependency: perl(DBI) for package: mysql-community-server-5.6.51-2.el6.x86_64
--> Processing Dependency: perl(Data::Dumper) for package: mysql-community-server-5.6.51-2.el6.x86_64
--> Running transaction check
---> Package mysql-community-client.x86_64 0:5.6.51-2.el6 will be installed
--> Processing Dependency: mysql-community-libs(x86-64) >= 5.6.10 for package: mysql-community-client-5.6.51-2.el6.x86_64
---> Package mysql-community-common.x86_64 0:5.6.51-2.el6 will be installed
---> Package perl-DBI.x86_64 0:1.627-4.el7 will be installed
--> Processing Dependency: perl(RPC::PlServer) >= 0.2001 for package: perl-DBI-1.627-4.el7.x86_64
--> Processing Dependency: perl(RPC::PlClient) >= 0.2000 for package: perl-DBI-1.627-4.el7.x86_64
---> Package perl-Data-Dumper.x86_64 0:2.145-3.el7 will be installed
--> Running transaction check
---> Package mysql-community-libs.x86_64 0:5.6.51-2.el6 will be installed
---> Package perl-PlRPC.noarch 0:0.2020-14.el7 will be installed
--> Processing Dependency: perl(Net::Daemon) >= 0.13 for package: perl-PlRPC-0.2020-14.el7.noarch
--> Processing Dependency: perl(Net::Daemon::Test) for package: perl-PlRPC-0.2020-14.el7.noarch
--> Processing Dependency: perl(Net::Daemon::Log) for package: perl-PlRPC-0.2020-14.el7.noarch
--> Processing Dependency: perl(Compress::Zlib) for package: perl-PlRPC-0.2020-14.el7.noarch
--> Running transaction check
---> Package perl-IO-Compress.noarch 0:2.061-2.el7 will be installed
--> Processing Dependency: perl(Compress::Raw::Zlib) >= 2.061 for package: perl-IO-Compress-2.061-2.el7.noarch
--> Processing Dependency: perl(Compress::Raw::Bzip2) >= 2.061 for package: perl-IO-Compress-2.061-2.el7.noarch
---> Package perl-Net-Daemon.noarch 0:0.48-5.el7 will be installed
--> Running transaction check
---> Package perl-Compress-Raw-Bzip2.x86_64 0:2.061-3.el7 will be installed
---> Package perl-Compress-Raw-Zlib.x86_64 1:2.061-4.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

========================================================
Package Arch Version Repository Size
========================================================
Installing:
mysql-community-server x86_64 5.6.51-2.el6 mysql56-community 62 M
Installing for dependencies:
mysql-community-client x86_64 5.6.51-2.el6 mysql56-community 20 M
mysql-community-common x86_64 5.6.51-2.el6 mysql56-community 340 k
mysql-community-libs x86_64 5.6.51-2.el6 mysql56-community 2.2 M
perl-Compress-Raw-Bzip2 x86_64 2.061-3.el7 base 32 k
perl-Compress-Raw-Zlib x86_64 1:2.061-4.el7 base 57 k
perl-DBI x86_64 1.627-4.el7 base 802 k
perl-Data-Dumper x86_64 2.145-3.el7 base 47 k
perl-IO-Compress noarch 2.061-2.el7 base 260 k
perl-Net-Daemon noarch 0.48-5.el7 base 51 k
perl-PlRPC noarch 0.2020-14.el7 base 36 k

Transaction Summary
=======================================================
Install 1 Package (+10 Dependent packages)

Total size: 86 M
Installed size: 355 M
Is this ok [y/d/N]: yes
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : mysql-community-common-5.6.51-2.el6.x86_64 1/11 
Installing : perl-Data-Dumper-2.145-3.el7.x86_64 2/11 
Installing : mysql-community-libs-5.6.51-2.el6.x86_64 3/11 
Installing : mysql-community-client-5.6.51-2.el6.x86_64 4/11 
Installing : perl-Compress-Raw-Bzip2-2.061-3.el7.x86_64 5/11 
Installing : 1:perl-Compress-Raw-Zlib-2.061-4.el7.x86_64 6/11 
Installing : perl-IO-Compress-2.061-2.el7.noarch 7/11 
Installing : perl-Net-Daemon-0.48-5.el7.noarch 8/11 
Installing : perl-PlRPC-0.2020-14.el7.noarch 9/11 
Installing : perl-DBI-1.627-4.el7.x86_64 10/11 
Installing : mysql-community-server-5.6.51-2.el6.x86_64 11/11 
Verifying : perl-Net-Daemon-0.48-5.el7.noarch 1/11 
Verifying : perl-Data-Dumper-2.145-3.el7.x86_64 2/11 
Verifying : mysql-community-server-5.6.51-2.el6.x86_64 3/11 
Verifying : mysql-community-common-5.6.51-2.el6.x86_64 4/11 
Verifying : perl-IO-Compress-2.061-2.el7.noarch 5/11 
Verifying : 1:perl-Compress-Raw-Zlib-2.061-4.el7.x86_64 6/11 
Verifying : mysql-community-libs-5.6.51-2.el6.x86_64 7/11 
Verifying : mysql-community-client-5.6.51-2.el6.x86_64 8/11 
Verifying : perl-DBI-1.627-4.el7.x86_64 9/11 
Verifying : perl-Compress-Raw-Bzip2-2.061-3.el7.x86_64 10/11 
Verifying : perl-PlRPC-0.2020-14.el7.noarch 11/11

Installed:
mysql-community-server.x86_64 0:5.6.51-2.el6

Dependency Installed:
mysql-community-client.x86_64 0:5.6.51-2.el6 mysql-community-common.x86_64 0:5.6.51-2.el6 mysql-community-libs.x86_64 0:5.6.51-2.el6 perl-Compress-Raw-Bzip2.x86_64 0:2.061-3.el7 
perl-Compress-Raw-Zlib.x86_64 1:2.061-4.el7 perl-DBI.x86_64 0:1.627-4.el7 perl-Data-Dumper.x86_64 0:2.145-3.el7 perl-IO-Compress.noarch 0:2.061-2.el7 
perl-Net-Daemon.noarch 0:0.48-5.el7 perl-PlRPC.noarch 0:0.2020-14.el7

Complete!

我们通过配置文件可以看到默认安装的路径是  /var/lib/mysql 
查看 /etc/my.cnf , 默认安装的,我什么也没有动

[root@fp-web-118 yum.repos.d]# cat /etc/my.cnf
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html

[mysqld]

# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M

# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin

# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

# Recommended in standard MySQL setup
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

启动和关闭mysql如下

启动方式一:

[root@fp-web-118 yum.repos.d]# /etc/init.d/mysqld start
Starting mysqld (via systemctl): [ OK ]
注: /etc/init.d/mysqld 实际也是shell文件,它和/usr/sbin/mysqld 是不同的,后者实际是二进制文件。

我们可以通过 /sbin/chkconfig – add mysql 命令加入到添加到你系统的启动服务组里面去  service mysql start 方式启动

启动方式二(linux启动MySQL的命令,相关的mysql管理命令安装在/usr/bin下可以找到:)

[root@fp-web-118 yum.repos.d]# mysqld_safe &

关闭方式一:

[root@fp-web-118 yum.repos.d]#/etc/init.d/mysqld stop

关闭方式二 (相关的mysql管理命令安装在/usr/bin下可以找到)

[root@fp-web-118 bin]# echo $PATH
  /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

[root@fp-web-118 bin]# mysqladmin -uroot -p shutdown

查看进程

[root@fp-web-118 yum.repos.d]# ps -efww| grep mysql
root 77993 1 0 20:14 ? 00:00: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 78198 77993 0 20:14 ? 00:00:00 /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

**/usr/sbin/mysqld是二进制文件,mysqld_safe实际是一个shell文件
**所以有必要也要看看mysqld_safe和mysqld的区别。

东西比较分散:
  /usr, /var/lib/mysql,/usr/lib64/mysql,/var/log/ ,/var/run 
  所以这种方式也能看出不利于管理。

登录mysql

[root@fp-web-118 yum.repos.d]# mysql -h localhost -u root -p
Enter password:    ####刚开始安装之后,密码默认为空
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.51 MySQL Community Server (GPL)

Copyright (c) 2000, 2021, 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> select version();
+-----------+
| version() |
+-----------+
| 5.6.51 |
+-----------+
1 row in set (0.00 sec)

以下为后补的内容
这里注意,我多次启动、关闭的时候没有注意密码问题,当我再次登录提示我密码不对,所以我不得不重新执行授权表,当然也有别的办法,因为是新的mysql,我直接用下面的重新设置
[root@fp-web-118 yum.repos.d]# mysql_install_db --datadir=/var/lib/mysql (这里注意没有空格即 --datadir = /var/lib/mysql 这样会出错)

Installing MySQL system tables…2022-02-01 20:38:29 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2022-02-01 20:38:29 0 [Note] Ignoring --secure-file-priv value as server is running with --bootstrap.
2022-02-01 20:38:29 0 [Note] /usr/sbin/mysqld (mysqld 5.6.51) starting as process 75620 …
2022-02-01 20:38:29 75620 [Note] InnoDB: Using atomics to ref count buffer pool pages
2022-02-01 20:38:29 75620 [Note] InnoDB: The InnoDB memory heap is disabled
2022-02-01 20:38:29 75620 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
….. ##其他部分我删除掉了,留下提示
/usr/bin/mysqladmin -u root password 'jinzs'
/usr/bin/mysqladmin -u root -h fp-web-118 password 'jinzs'

你可以运行
/usr/bin/mysql_secure_installation
他会给你一些选项去删除test数据库或者是匿名的用户,通过默认方式安装得时候

cd /usr ; /usr/bin/mysqld_safe &

你可以测试你的mysql,用这个脚本 mysql-test-run.pl

cd mysql-test ; perl mysql-test-run.pl

WARNING: 默认使用的是/etc/my.cnf,如果不想使用这个默认文件你也可以通过 --defaults-file 这个参数进行设置mysql,在开始命令里面

现在就可以登录了

[root@fp-web-118 bin]# mysql -u root -h fp-web-118 -pjinzs
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.6.51 MySQL Community Server (GPL)

Copyright (c) 2000, 2021, 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>

安装的版本可以看到是5.6.51 ,当然想删除也很简单 首先查看一下系统内安装的mysql信息

[root@fp-web-118 yum.repos.d]# rpm -qa | grep mysql
mysql-community-common-5.6.51-2.el6.x86_64 组件
mysql-community-client-5.6.51-2.el6.x86_64 客户端
mysql-community-libs-5.6.51-2.el6.x86_64 依赖库
mysql-community-server-5.6.51-2.el6.x86_64 服务端

删除利用yum安装的mysql

yum -y remove mysql-community-common-5.6.51-2.el6.x86_64

我们在看看rpm方式安装之后,安装的位置情况

1、数据库目录

/var/lib/mysql/

2、/usr/share/mysql(mysql.server命令及配置文件)

[root@fp-web-118 mysql]# ll /usr/share/mysql | grep mysql
-rw-r--r-- 1 root root 844 Jan 5 2021 mysql-log-rotate
-rw-r--r-- 1 root root 2121 Jan 5 2021 mysql_security_commands.sql
-rw-r--r-- 1 root root 3972 Jan 5 2021 mysql_system_tables_data.sql
-rw-r--r-- 1 root root 93667 Jan 5 2021 mysql_system_tables.sql
-rw-r--r-- 1 root root 10834 Jan 5 2021 mysql_test_data_timezone.sql

3、相关命令 /usr/bin

[root@fp-web-118 bin]# ll | grep mysql
-rwxr-xr-x 1 root root 1956 Jan 5 2021 msql2mysql
-rwxr-xr-x 1 root root 9373167 Jan 5 2021 mysql
-rwxr-xr-x 1 root root 112528 Jan 5 2021 mysqlaccess
-rw-r--r-- 1 root root 1702 Jan 5 2021 mysqlaccess.conf
-rwxr-xr-x 1 root root 8339464 Jan 5 2021 mysqladmin
-rwxr-xr-x 1 root root 9183394 Jan 5 2021 mysqlbinlog
-rwxr-xr-x 1 root root 11602 Jan 5 2021 mysqlbug
-rwxr-xr-x 1 root root 8323384 Jan 5 2021 mysqlcheck
-rwxr-xr-x 1 root root 8411442 Jan 5 2021 mysql_config_editor
-rwxr-xr-x 1 root root 4716 Jan 5 2021 mysql_convert_table_format
-rwxr-xr-x 1 root root 26919 Jan 5 2021 mysqld_multi
-rwxr-xr-x 1 root root 26630 Jan 5 2021 mysqld_safe
-rwxr-xr-x 1 root root 8608337 Jan 5 2021 mysqldump
-rwxr-xr-x 1 root root 7825 Jan 5 2021 mysqldumpslow
-rwxr-xr-x 1 root root 3791 Jan 5 2021 mysql_find_rows
-rwxr-xr-x 1 root root 1732 Jan 5 2021 mysql_fix_extensions
-rwxr-xr-x 1 root root 35339 Jan 5 2021 mysqlhotcopy
-rwxr-xr-x 1 root root 8345555 Jan 5 2021 mysqlimport
-rwxr-xr-x 1 root root 35067 Jan 5 2021 mysql_install_db
-rwxr-xr-x 1 root root 6796176 Jan 5 2021 mysql_plugin
-rwxr-xr-x 1 root root 10525 Jan 5 2021 mysql_secure_installation
-rwxr-xr-x 1 root root 8316240 Jan 5 2021 mysqlshow
-rwxr-xr-x 1 root root 8404798 Jan 5 2021 mysqlslap
-rwxr-xr-x 1 root root 9119043 Jan 5 2021 mysqltest
-rwxr-xr-x 1 root root 4487695 Jan 5 2021 mysql_tzinfo_to_sql
-rwxr-xr-x 1 root root 8488273 Jan 5 2021 mysql_upgrade
-rwxr-xr-x 1 root root 6730715 Jan 5 2021 mysql_waitpid
-rwxr-xr-x 1 root root 4359 Jan 5 2021 mysql_zap
4、相关启动脚本
 /etc/rc.d/init.d/(启动脚本文件mysql的目录) 
5、配置文件所在位置 /etc/my.cnf

data目录重新设置(对yum方式安装的mysql进行调整,因为可能你分区时/var/并没有多大的空间,所以需要进行转移)

假如要把核心的数据目录移到其他目录 例如 /usr/local/mysql/data下需要进行下面几步:
1、把MySQL服务进程停掉:
   mysqladmin -u root -p shutdown 或 service mysql stop 或 /etc/rc.d/init.d/mysql stop

这里可以把mysql添加至服务列表:
  # chkconfig --add mysqld
  # chkconfig mysqld on
  # service mysqld start/stop/restart等
2、/usr/local/mysql目录下建立data目录
   cd /usr/local/mysql
   mkdir data
3、把/var/lib/mysql整个目录移到/usr/local/mysql/data (这个位置实际只是想集中管理而已,我们也可以放在其他位置,我这里想在/usr/local/mysql下集中做管理)
   mv /var/lib/mysql /usr/local/mysql/data/
   这样就把MySQL的数据文件移动到了/usr/local/mysql/data/mysql下
   4、找到my.cnf配置文件
   如果/etc/目录下没有my.cnf配置文件,命令如下:
[mysqld]
datadir=/usr/local/mysql/data ##注意更改data目录的所属 chown -R mysql:mysql /usr/local/mysql