CentOS 7 安装 MySQL 的几种方式
huty
2022年11月11日 · 阅读 1,078
YUM安装
1、卸载默认的 MariaDB
rpm -qa | grep -i mariadb
rpm -e --nodeps mariadb-xxx
2、卸载 MySQL 残余
rpm -qa | grep mysql
rpm -e --nodeps mysql-xxx
3、下载 MySQL 仓库
官网:https://repo.mysql.com
下载对应版本的 .rpm 文件,如:
- 在 CentOS 7 中安装 MySQL5.7
wget https://repo.mysql.com/mysql57-community-release-el7-11.noarch.rpm
- 在 CentOS 7 中安装 MySQL8.0
wget https://repo.mysql.com/mysql80-community-release-el7-6.noarch.rpm
4、安装 MySQL 仓库
安装上一步中下载的 MySQL 仓库,如
- 安装 MySQL5.7 仓库
yum install -y mysql57-community-release-el7-11.noarch.rpm
- 安装 MySQL8.0 仓库
yum install -y mysql80-community-release-el7-6.noarch.rpm
5、安装 MySQL
yum install -y mysql-community-server
注意:安装时若提示 “GPG 密钥不存在/不合适” 则需要先执行以下命令,然后再重新安装 MySQL
rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
6、启动 MySQL 服务
systemctl start mysqld.service
7、查看 MySQL 默认密码
cat /var/log/mysqld.log | grep password
8、登录 MySQL
mysql -u root -p
RPM安装
1、在官网找到完整的 rpm 包
官网下载地址:https://dev.mysql.com/downloads/mysql/
2、下载完整的 RPM 包
方式一:直接下载到服务器
wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.38-1.el7.x86_64.rpm-bundle.tar
方式二:先下载到本机,再上传到服务器
3、解压 TAR 包
tar xvf mysql-5.7.38-1.el7.x86_64.rpm-bundle.tar
4、依次安装 RPM 包
- 必须安装
rpm -ivh mysql-community-common-5.7.38-1.el7.x86_64.rpm
rpm -ivh mysql-community-libs-5.7.38-1.el7.x86_64.rpm
rpm -ivh mysql-community-libs-compat-5.7.38-1.el7.x86_64.rpm
rpm -ivh mysql-community-client-5.7.38-1.el7.x86_64.rpm
rpm -ivh mysql-community-server-5.7.38-1.el7.x86_64.rpm
- 可选安装
## 开发库
rpm -ivh mysql-community-devel-5.7.38-1.el7.x86_64.rpm
## 嵌入式
rpm -ivh mysql-community-embedded-5.7.38-1.el7.x86_64.rpm
rpm -ivh mysql-community-embedded-compat-5.7.38-1.el7.x86_64.rpm
rpm -ivh mysql-community-embedded-devel-5.7.38-1.el7.x86_64.rpm
## 测试组件
rpm -ivh mysql-community-test-5.7.38-1.el7.x86_64.rpm
5、检查安装情况
rpm -qa | grep mysql
6、启动 MySQL 服务
systemctl start mysqld.service
7、查看 MySQL 默认密码
cat /var/log/mysqld.log | grep password
8、登录 MySQL
mysql -u root -p
Docker 安装
1、安装 Docker
见:https://hty1024.com/archives/centos7-an-zhuang-docker-de-ji-zhong-fang-shi
2、获取 MySQL 镜像
官网:https://hub.docker.com/_/mysql
方式一:直接拉取镜像
docker pull mysql:5.7.38
方式二:导入本地镜像
docker load -i 镜像TAR文件名称
3、运行 MySQL 容器
docker run /
-d -p 3306:3306 /
--restart always /
--privileged=true /
--name mysql /
-e MYSQL_USER="username" /
-e MYSQL_PASSWORD="username_pasword" /
-e MYSQL_ROOT_PASSWORD="root_password" /
-v=/data/apps/mysql_docker/5.7.38/config/my.cnf:/etc/my.cnf /
-v=/data/apps/mysql_docker/5.7.38/data:/var/lib/mysql mysql:5.7.38
参数说明:
- –restart always:开机启动
- –privileged=true:提升容器内权限
- -e MYSQL_USER=“username”:添加用户username
- -e MYSQL_PASSWORD=“username_pasword”:设置username的密码为username_pasword
- -e MYSQL_ROOT_PASSWORD=“root_password”:设置root的密码为root_password
- -v=/data/apps/mysql_docker/5.7.38/config/my.cnf:/etc/my.cnf:映射配置文件
- -v=/data/apps/mysql_docker/5.7.38/data:/var/lib/mysql:映射数据目录
4、查看容器运行情况
docker ps
5、登录 MySQL
docker exec -it mysql mysql -u root -p
Docker Compose 安装
1、安装 Docker 和 Docker Compose
见:https://hty1024.com/archives/centos7-an-zhuang-docker-de-ji-zhong-fang-shi
2、创建相关目录
mkdir -pv /opt/mysql/{data,initdb,conf}
此处使用的数据目录为:/opt/mysql/data
,配置文件目录为:/opt/mysql/conf
,初始化数据文件目录为:/opt/mysql/initdb
3、编辑 docker-compose.yml 文件
vim /opt/mysql/docker-compose.yml
version: '3'
services:
mysql:
image: mysql:5.7.40
container_name: mysql
restart: always
dns_search: .
network_mode: host
volumes:
- /etc/localtime:/etc/localtime:ro
- /opt/mysql/data:/var/lib/mysql
- /opt/mysql/initdb:/docker-entrypoint-initdb.d
- /opt/mysql/conf/my.cnf:/etc/mysql/my.cnf:ro
- /opt/mysql/conf/my_custom.cnf:/etc/mysql/conf.d/my_custom.cnf:ro
environment:
- MYSQL_ROOT_USER=root
- MYSQL_ROOT_PASSWORD=root_password
- MYSQL_USER=username
- MYSQL_PASSWORD=username_pasword
- BITNAMI_DEBUG=true
4、启动 Docker 容器
cd /opt/mysql
docker-compose up -d
5、查看 MySQL 容器状态和日志
cd /opt/mysql
docker-compose ps
docker-compose logs -f
评论已关闭