How to install mysql 5.7 with podman in Ubuntu 24.04
概述
今天我们介绍一下如何在Ubuntu server 22.04以Podman方式安装。
配置卷
名称 | 位置 | 用途 |
---|---|---|
mysql-5.7 conf | /data/docker/mysql-5.7/conf/conf.d | mysql-5.7配置目录 |
mysql-5.7 conf | /data/docker/mysql-5.7/conf/mysql.conf.d | mysql-5.7配置目录 |
mysql-5.7 data | /data/docker/mysql5.7/data | mysql-5.7数据目录 |
bash
sudo mkdir -p /data
sudo chown ylzx:ylzx -R /data
mkdir -p /data/docker/mysql-5.7/conf/conf.d
mkdir -p /data/docker/mysql-5.7/conf/mysql.conf.d
mkdir -p /data/docker/gitlab/data
运行
考虑到mysql5.7官方镜像的特殊性,不能直接挂载本地目录做为卷,需要完成以下步骤以后才可以挂载上本地目录卷。
- 步骤 1
运行一个不挂载卷的容器
bash
podman run --detach \
--restart always \
--publish 13306:3306 \
--name mysql-5.7 \
--volume /usr/share/zoneinfo/Asia/Shanghai:/etc/localtime \
--env MYSQL_ROOT_PASSWORD=Gah6kuP7ohfio4 \
mysql:5.7.44
- 步骤 2
用podman cp
命令复制出容器中的目录到本地,命令如下:
bash
podman cp mysql-5.7:/etc/mysql/conf.d /data/docker/mysql-5.7/conf/conf.d
podman cp mysql-5.7:/etc/mysql/mysql.conf.d /data/docker/mysql-5.7/conf/mysql.conf.d
- 步骤3
挂载卷运行,命令如下
bash
podman run --detach \
--restart always \
--publish 13306:3306 \
--name mysql-5.7 \
--volume /data/docker/mysql-5.7/conf/conf.d:/etc/mysql/conf.d \
--volume /data/docker/mysql-5.7/conf/mysql.conf.d:/etc/mysql/mysql.conf.d \
--volume /data/docker/mysql-5.7/data:/var/lib/mysql \
--volume /usr/share/zoneinfo/Asia/Shanghai:/etc/localtime \
--env MYSQL_ROOT_PASSWORD=Gah6kuP7ohfio4 \
mysql:5.7.44
查看
bash
lwk@qwfys:~$ podman ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
191b58ff003f docker.io/gitlab/gitlab-ce:latest /assets/wrapper 2 hours ago Up 2 hours (healthy) 0.0.0.0:10022->22/tcp, 0.0.0.0:19000->80/tcp gitlab
295b77ee2d0f docker.io/library/mysql:5.7.44 mysqld 31 minutes ago Up 31 minutes 0.0.0.0:13306->3306/tcp mysql-5.7
ylzx@scyl043:/data/docker/mysql-5.7/conf$
访问
经过上述安装以后,mysql就安装好了,我们可以直接使用了。
在没有mysql客户端的时候,我们也可以用docker容器做为客户端来验证一下,上述安装有没有成功。示例如下:
bash
lwk@qwfys:~$ podman run -it --rm mysql:5.7.44 mysql \
--host=192.168.100.43 \
--port=13306 \
--user=root \
--password \
--database=mysql
Enter password:
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.7.44 MySQL Community Server (GPL)
Copyright (c) 2000, 2023, Oracle and/or its affiliates.
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 |
| sys |
+--------------------+
4 rows in set (0.00 sec)
mysql> exit
Bye
lwk@qwfys:~$
spring boot项目中使用的话,可以用下述方式访问mysql 5.7数据库
bash
spring:
datasource:
hikari:
jdbc-url: jdbc:mysql://192.168.100.43:13306/mysql?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=Asia/Shanghai
username: root
password: Gah6kuP7ohfio4