创建卷
podman volume create --label type=mysql-8.0 --label env=dev mysql-8.0-data
podman volume create --label type=mysql-8.0 --label env=dev mysql-8.0-conf
创建用户服务目录
mkdir -p ~/.config/systemd/user
创建用户容器
podman create \
--restart always \
--publish 23306:3306 \
--name mysql-8.0 \
--volume mysql-8.0-data:/var/lib/mysql \
--volume mysql-8.0-conf:/etc/mysql \
--volume /usr/share/zoneinfo/Asia/Shanghai:/etc/localtime \
--env MYSQL_ROOT_PASSWORD=Gah6kuP7ohfio4 \
mysql:8.0.36
如果是启动容器的话,可以用下面的脚本
podman run --detach \
--restart always \
--publish 23306:3306 \
--name mysql-8.0 \
--volume mysql-8.0-data:/var/lib/mysql \
--volume mysql-8.0-conf:/etc/mysql \
--volume /usr/share/zoneinfo/Asia/Shanghai:/etc/localtime \
--env MYSQL_ROOT_PASSWORD=Gah6kuP7ohfio4 \
mysql:8.0.36
如果要将系统变量innodb_strict_mode调整为OFF,需要用下述脚本启动
podman run --detach \
--restart always \
--publish 23306:3306 \
--name mysql-8.0 \
--volume mysql-8.0-data:/var/lib/mysql \
--volume mysql-8.0-conf:/etc/mysql \
--volume /usr/share/zoneinfo/Asia/Shanghai:/etc/localtime \
--env MYSQL_ROOT_PASSWORD=Gah6kuP7ohfio4 \
mysql:8.0.36 --innodb-strict-mode=false
启动以后,可以在mysql的console中用下述脚本验证
lwk@qwfys:~$ mysql -h 127.0.0.1 -u root -P 23306 -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 134
Server version: 8.0.36 MySQL Community Server - GPL
Copyright (c) 2000, 2024, 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 variables like '%innodb_strict_mode%';
+--------------------+-------+
| Variable_name | Value |
+--------------------+-------+
| innodb_strict_mode | OFF |
+--------------------+-------+
1 row in set (0.01 sec)
mysql>
如果要将系统变量lower_case_table_names调整为1,也就是说,让mysql对于大小写不敏感,需要用下述脚本启动
podman run --detach \
--restart always \
--publish 23306:3306 \
--name mysql-8.0 \
--volume mysql-8.0-data:/var/lib/mysql \
--volume mysql-8.0-conf:/etc/mysql \
--volume /usr/share/zoneinfo/Asia/Shanghai:/etc/localtime \
--env MYSQL_ROOT_PASSWORD=Gah6kuP7ohfio4 \
mysql:8.0.36 --lower-case-table-names=1
这里需要说明一下,由于mysql 8.0的限制,采用上述脚本启动的时候,如果挂载了卷,需要在一个干净的卷上操作,否则操作不成功。
验证脚本如下:
lwk@qwfys:~$ mysql -h 127.0.0.1 -u root -P 23306 -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 134
Server version: 8.0.36 MySQL Community Server - GPL
Copyright (c) 2000, 2024, 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 variables like '%lower_case_table_names%';
+------------------------+-------+
| Variable_name | Value |
+------------------------+-------+
| lower_case_table_names | 1 |
+------------------------+-------+
1 row in set (0.01 sec)
mysql>
如果将上述两个启动参数一起添加上,脚本如下
podman run --detach \
--restart always \
--publish 23306:3306 \
--name mysql-8.0 \
--volume mysql-8.0-data:/var/lib/mysql \
--volume mysql-8.0-conf:/etc/mysql \
--volume /usr/share/zoneinfo/Asia/Shanghai:/etc/localtime \
--env MYSQL_ROOT_PASSWORD=Gah6kuP7ohfio4 \
mysql:8.0.36 --innodb-strict-mode=false --lower-case-table-names=1
效果可以通过下而把脚本验证
lwk@qwfys:~$ mysql -h 127.0.0.1 -u root -P 23306 -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 134
Server version: 8.0.36 MySQL Community Server - GPL
Copyright (c) 2000, 2024, 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 variables like '%innodb_strict_mode%';
+--------------------+-------+
| Variable_name | Value |
+--------------------+-------+
| innodb_strict_mode | OFF |
+--------------------+-------+
1 row in set (0.01 sec)
mysql> show variables like '%lower_case_table_names%';
+------------------------+-------+
| Variable_name | Value |
+------------------------+-------+
| lower_case_table_names | 1 |
+------------------------+-------+
1 row in set (0.01 sec)
mysql>
mysql docker启动参数有很多,通常情况下,我们不需要记忆,只需要掌握如果查找这些启动参数即可,依据官方相关说明,我们可以借助如下脚本列出所有启动参数:
podman run -it --rm mysql:8.0.36 --verbose --help
接下来,我们基于podman完成用户服务的创建
podman generate systemd --new mysql-8.0 > ~/.config/systemd/user/mysql-8.0.service
重新加载
systemctl --user daemon-reload
启用开机启动
systemctl --user enable mysql-8.0.service
查看状态
systemctl --user status mysql-8.0.service
启动
systemctl --user start mysql-8.0.service
停服
systemctl --user stop mysql-8.0.service
重启
systemctl --user restart mysql-8.0.service