前言
mysql如果正式安装,卸载起来比较麻烦。如果是自己测试用的话,可以用podman拉取一个镜像来使用。
这里使用的是mysql5.7版本,对应的docker镜像是mysql:5.7
(如果拉取较慢,可以选择使用阿里云镜像源:j3m2itm3.mirror.aliyuncs.com)
这篇随笔主要参考博客podman安装mysql
正文
- 拉取mysql镜像
podman pull mysql:5.7
拉取完毕后,可以通过如下指令查看镜像
podman images
# REPOSITORY TAG IMAGE ID CREATED SIZE
# docker.io/library/mysql 5.7 c20987f18b13 9 months ago 454 MB
- 建立本地映射文件
mkdir mysql5.7
cd mysql5.7
mkdir conf
mkdir data
mkdir log
- 新建my.cnf
cd conf
touch my.cnf
编辑my.cnf如下
(无耻抄一下上面的博客!)
# Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# The MySQL Server configuration file.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
secure-file-priv= NULL
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Custom config should go here
!includedir /etc/mysql/conf.d/
default_authentication_plugin= mysql_native_password
- 在mysql5.7中新建启动脚本
mysql_ctl.sh
#!/bin/bash
container_name=mysql5.7
file_path=/home/brian/podman/mysql/mysql5.7
function start()
{
podman run -itd --rm \
-p 3306:3306 \
-e MYSQL_ROOT_PASSWORD=root \
-v $file_path/data:/var/lib/mysql:rw \
-v $file_path/log:/var/log/mysql:rw \
-v $file_path/conf/my.cnf:/etc/mysql/my.cnf:rw \
-v /etc/localtime:/etc/localtime:ro \
--name $container_name \
mysql:5.7
}
function debug()
{
podman run -it --rm \
-p 3306:3306 \
-e MYSQL_ROOT_PASSWORD=root \
-v $file_path/data:/var/lib/mysql:rw \
-v $file_path/log:/var/log/mysql:rw \
-v $file_path/conf/my.cnf:/etc/mysql/my.cnf:rw \
-v /etc/localtime:/etc/localtime:ro \
--name $container_name \
mysql
}
function stop()
{
podman stop $container_name
}
function podman_ps()
{
podman ps -a
}
function login()
{
podman exec -it $container_name bash
}
function usage()
{
echo -e "Usage: $0 start|debug|stop|ps|login"
exit 0
}
if [[ $1 == "start" ]];then
start
elif [[ $1 == "debug" ]];then
debug
elif [[ $1 == "stop" ]];then
stop
elif [[ $1 == "ps" ]];then
podman_ps
elif [[ $1 == "login" ]];then
login
else
usage
fi
- 脚本使用
chmod +x mysql_ctl.sh
# 后台启动
./mysql_ctl.sh start
# 非后台启动(用于debug)
# 停止的话需要新建终端,使用 podman stop mysql5.7, 或脚本的stop
./mysql_ctl.sh debug
# 停止
# (因为启动时添加了参数--rm,容器一停止立马自动删除)
./mysql_ctl.sh stop
# 查看所有启动的容器
./mysql_ctl.sh ps
# 登录进容器
./mysql_ctl.sh login
- 配置root远程登录
这个环境中,mysql的root用户的默认密码为root
# 启动容器后,登录进容器
./mysql_ctl.sh login
# 登录mysql
mysql -uroot -p
# 输入默认密码:root
# 配置root远程登录
alter user 'root'@'%' identified with mysql_native_password by '密码';
参考
[1]. podman安装mysql
标签:容器,stop,podman,sh,file,mysql,ctl From: https://www.cnblogs.com/brian-sun/p/16774154.html