目录

前言

一、仓库安装

二、本地安装

三、容器安装

四、源码安装


前言

博主的配置信息:

Windows版本:Win10

VMware虚拟机版本:Vmware Workstation Pro 17

Linux版本:Red Hat Enterprise Linux 9.1

MySQL版本:MySQL Community Server 8.0.32

保证Linux的正常联网以及配置yum仓库。

可以在MySQL官网地址:https://www.mysql.com/上获取到正规的MySQL。

MySQL 常见版本:

  • MySQL Community Server 社区版本,开源免费,但不提供官方技术支持。
  • MySQL Enterprise Edition 企业版本,需付费,可以试用 30 天。
  • MySQL Cluster 集群版,开源免费。可将几个 MySQL Server 封装成一个 Server。
  • MySQL Cluster CGE 高级集群版,需付费

MySQL数据库存在多种版本,不同的版本在不同的平台上(OS,也就是操作系统上)安装方式可能有所不同,因此安装时最好参照官方文档进行安装。https://dev.mysql.com/doc/

下面记录了博主在Linux上通过仓库安装、本地安装、容器安装、源码安装四种不同的方式安装MySQL的过程:

一、仓库安装

仓库安装就是在官方仓库或本地仓库、网络仓库中,用yum方式安装。推荐使用官方仓库。博主认为仓库安装相比其他安装方式来说是最方便的。

获取MySQL仓库的网站:https://repo.mysql.com/

[root@forDB ~]# cat /etc/redhat-release Red Hat Enterprise Linux release 9.1 (Plow)[root@forDB ~]# rpm -ivhhttps://repo.mysql.com/mysql80-community-release-el9-1.noarch.rpm[root@forDB ~]# ll /etc/yum.repos.d/total 20-rw-r--r--. 1 root root 218 Feb7 20:40 aliyum-9.repo-rw-r--r--. 1 root root 951 Jul62022 mysql-community-debuginfo.repo-rw-r--r--. 1 root root 827 Jul62022 mysql-community.repo-rw-r--r--. 1 root root 871 Jul62022 mysql-community-source.repo-rw-r--r--. 1 root root 358 Feb7 20:41 redhat.repo[root@forDB ~]# yum install mysql-server -y[root@forDB ~]# rpm -qa | grep mysqlmysql80-community-release-el9-1.noarchmysql-community-common-8.0.32-1.el9.x86_64mysql-community-client-plugins-8.0.32-1.el9.x86_64mysql-community-libs-8.0.32-1.el9.x86_64mysql-community-client-8.0.32-1.el9.x86_64mysql-community-icu-data-files-8.0.32-1.el9.x86_64mysql-community-server-8.0.32-1.el9.x86_64[root@forDB ~]# systemctl start mysqld[root@forDB ~]# systemctl status mysqld# MySQL8.0.32版本第一次登录需要临时密码,在mysqld.log文件中[root@forDB ~]# grep password /var/log/mysqld.log 2023-02-09T12:46:59.105801Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: ;/pZjo)lV8>B[root@forDB ~]# mysql -uroot -p';/pZjo)lV8>B'# 登录之后必须先修改用户密码,不然无法正常使用mysql。默认密码策略要求密码至少包含一个大写字母、一个小写字母、一位数字和一个特殊字符,并且密码总长度至少为8个字符。mysql> ALTER USER root@localhost IDENTIFIED BY 'Admin123!';# 此时就可以正常使用mysql了mysql> show databases;+--------------------+| Database |+--------------------+| information_schema || mysql|| performance_schema || sys|+--------------------+4 rows in set (0.00 sec)

卸载MySQL:

[root@forDB ~]# yum remove mysql-server -y[root@forDB ~]# rpm -qa | grep mysqlmysql80-community-release-el9-1.noarch[root@forDB ~]# yum remove mysql80-community-release.noarch -y# 删除相关文件,以防再次安装产生冲突[root@forDB ~]# rm -rf /var/log/mysqld.log /var/lib/mysql/

二、本地安装

本地安装就是下载MySQL的捆绑包到本地,然后解包安装。

捆绑包地址:https://cdn.mysql.com//Downloads/MySQL-8.0/mysql-8.0.32-1.el9.x86_64.rpm-bundle.tar

或在 https://dev.mysql.com/downloads/mysql/ 中获取对应操作系统的捆绑包(这不能在Linux中下载,只能先下载到Windows上再传到Linux上)

[root@forDB ~]# mkdir mysql[root@forDB ~]# cd mysql/[root@forDB mysql]# wgethttps://cdn.mysql.com//Downloads/MySQL-8.0/mysql-8.0.32-1.el9.x86_64.rpm-bundle.tar[root@forDB mysql]# tar xvf mysql-8.0.32-1.el9.x86_64.rpm-bundle.tar [root@forDB mysql]# rpm -ivh mysql-community-server-8.0.32-1.el9.x86_64.rpm \> mysql-community-client-8.0.32-1.el9.x86_64.rpm \> mysql-community-common-8.0.32-1.el9.x86_64.rpm \> mysql-community-icu-data-files-8.0.32-1.el9.x86_64.rpm \> mysql-community-client-plugins-8.0.32-1.el9.x86_64.rpm \> mysql-community-libs-8.0.32-1.el9.x86_64.rpm[root@forDB mysql]# systemctl start mysqld.service [root@forDB mysql]# systemctl status mysqld.service [root@forDB mysql]# grep password /var/log/mysqld.log 2023-02-09T13:52:16.691395Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: d+41ucpK_)IN[root@forDB mysql]# mysql -uroot -pEnter password: mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'Admin123!';mysql> show databases;mysql> exit

卸载MySQL:

[root@forDB mysql]# rpm -qa | grep mysqlmysql-community-client-plugins-8.0.32-1.el9.x86_64mysql-community-common-8.0.32-1.el9.x86_64mysql-community-libs-8.0.32-1.el9.x86_64mysql-community-client-8.0.32-1.el9.x86_64mysql-community-icu-data-files-8.0.32-1.el9.x86_64mysql-community-server-8.0.32-1.el9.x86_64[root@forDB mysql]# rpm -evh mysql-community-client-plugins \> mysql-community-common \> mysql-community-libs \> mysql-community-client \> mysql-community-icu-data-files \> mysql-community-server[root@forDB mysql]# rm -rf /var/log/mysqld.log /var/lib/mysql/

本地安装的缺点就是安装时的依赖很烦人,卸载时也比较麻烦。我的评价是不如仓库安装。

三、容器安装

容器安装方式首先要安装容器,再在容器上安装MySQL。

#删除之前的容器[root@forDB ~]# yum remove docker \docker-client \docker-client-latest \docker-common \docker-latest \docker-latest-logrotate \docker-logrotate \docker-engine#安装容器的依赖包[root@forDB ~]# yum install -y yum-utils device-mapper-persistent-data lvm2#添加阿里云的容器仓库[root@forDB ~]# yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo#更改仓库文件中的下载网站为阿里云的网站,因为国内的网站会快一些[root@forDB ~]# sed -i 's+download.docker.com+mirrors.aliyun.com/docker-ce+' /etc/yum.repos.d/docker-ce.repo#安装docker-ce,这里要用--allowerasing选项来替换rhel9中冲突的软件包[root@forDB ~]# yum install docker-ce --allowerasing -y[root@forDB ~]# systemctl start docker[root@forDB ~]# systemctl status docker[root@forDB ~]# docker version#测试docker能否正常运行[root@forDB ~]# docker run hello-world#安装mysql[root@forDB ~]# docker run --name mysqltest -e MYSQL_ROOT_PASSWORD=123456 -d mysql#查看容器信息[root@forDB ~]# docker ps#为mysqltest建立一个bash接口[root@forDB ~]# docker exec -it mysqltest /bin/bash#登录测试bash-4.4# mysql -uroot -pEnter password: mysql> show databases;+--------------------+| Database |+--------------------+| information_schema || mysql|| performance_schema || sys|+--------------------+4 rows in set (0.00 sec)mysql> exitByebash-4.4# exitexit[root@forDB ~]# 

登录时也可以用主机直接连容器的MySQL:

#安装一个MySQL的客户端[root@forDB ~]# yum install mysql -y#查看容器的ip[root@forDB ~]# docker inspect mysqltest..."IPAddress": "172.17.0.2",...#连接容器的MySQL[root@forDB ~]# mysql -uroot -p123456 -h 172.17.0.2mysql> 

四、源码安装

源码安装最为费时费力,但用源码安装的软件是最适合本机器的。

MySQL源码安装可以参考官方文档:https://dev.mysql.com/doc/refman/8.0/en/installing-source-distribution.html

MySQL8.0.32源码压缩包地址:https://cdn.mysql.com//Downloads/MySQL-8.0/mysql-boost-8.0.32.tar.gz

也可在官网手动下载压缩包,再传到Linux上:

https://dev.mysql.com/downloads/mysql/

[root@server ~]# wgethttps://cdn.mysql.com//Downloads/MySQL-8.0/mysql-boost-8.0.32.tar.gz#添加mysql系统用户和组[root@server ~]# groupadd mysql[root@server ~]# useradd -r -g mysql -s /bin/false mysql#解压解包,生成了mysql-8.0.32的文件,这就是源码包的所有内容[root@server ~]# tar xvf mysql-boost-8.0.32.tar.gz#安装编译器[root@server ~]# yum install gcc gcc-c++ cmake -y#安装编译过程中需要的库[root@server ~]# yum install gcc-toolset-12-gcc gcc-toolset-12-gcc-c++ gcc-toolset-12-binutils openssl-devel ncurses-devel libtirpc-1.3.3-0.el9.x86_64 rpcgen git -y[root@server ~]# rpm -ivh https://mirrors.tuna.tsinghua.edu.cn/centos-stream/9-stream/CRB/x86_64/os/Packages/libtirpc-devel-1.3.3-0.el9.x86_64.rpm#创建一个编译目录[root@server ~]# cd mysql-8.0.32/[root@server mysql-8.0.32]# mkdir bld[root@server mysql-8.0.32]# cd bld#准备编译[root@server bld]# cmake .. -DWITH_BOOST=../boost/boost_1_77_0#开始编译(建议将虚拟机内存调整为至少4G,以提高编译速度)[root@server bld]# make########该过程很久,大概两三个小时,请耐心等待#编译完成后,开始安装[root@server bld]# make install#安装好的文件在/usr/local/mysql中[root@server bld]# cd /usr/local/mysql/#创建一个存储数据文件的目录[root@server mysql]# mkdir mysql-files#修改所属者和所属组[root@server mysql]# chown mysql:mysql mysql-files/#修改权限[root@server mysql]# chmod 750 mysql-files/#初始化数据库(临时密码在最后的输出中:dU/sA5uleFrk)[root@server mysql]# ./bin/mysqld --initialize --user=mysql2023-02-10T14:59:13.708471Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: dU/sA5uleFrk#开启数据库的ssl加密功能[root@server mysql]# ./bin/mysql_ssl_rsa_setup #启动mysqld服务[root@server mysql]# ./bin/mysqld_safe --user=mysql &#mysqld_safe防止误杀mysql服务,&表示后台运行#用初始化产生的临时密码登录mysql[root@server mysql]# ./bin/mysql -uroot -pdU/sA5uleFrkmysql> #更改密码mysql> ALTER USER root@localhost IDENTIFIED BY 'Admin123!';mysql> exit大功告成!
#停止mysql服务[root@server mysql]# ./bin/mysqladmin -uroot -pAdmin123! shutdown
#若想用systemctl命令快捷启动mysql,可以操作如下:[root@server mysql]# cp support-files/mysql.server /etc/init.d/mysql.server(若没有init.d文件,可以yum install chkconfig -y,就有了)[root@server mysql]# systemctl daemon-reload[root@server mysql]# systemctl start mysql[root@server mysql]# systemctl status mysql[root@server mysql]# systemctl stop mysql

over~