podman安装mysql容器

前言


mysql如果正式安装,卸载起来比较麻烦。如果是自己测试用的话,可以用podman拉取一个镜像来使用。

这里使用的是mysql5.7版本,对应的docker镜像是mysql:5.7
(如果拉取较慢,可以选择使用阿里云镜像源:j3m2itm3.mirror.aliyuncs.com)

这篇随笔主要参考博客podman安装mysql

正文


  1. 拉取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
  1. 建立本地映射文件
mkdir mysql5.7
cd mysql5.7
mkdir conf
mkdir data
mkdir log
  1. 新建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/

# !!! 如果使用mysql8 要把下面这行注释掉
# mysql  Ver 8.1.0 for Linux on x86_64 (MySQL Community Server - GPL) 不用注释
# mysql 8.4 需要注释
default_authentication_plugin= mysql_native_password

mysql8.4 新特性 MySQL 8.4新特性速览

# 从 MySQL 8.4.0 开始,mysql_native_password 认证插件默认不再启用。

# 若要启用,需要在MySQL启动的时候,添加--mysql-native-password=ON 参数;

# 或在配置文件中设置 mysql_native_password=ON。
  1. 在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:5.7
}

function stop()
{
	podman stop $container_name
}

function podman_ps()
{
	podman ps -a
}

function login()
{
	podman exec -it $container_name bash
}

function mysql_in()
{
        podman exec -it mysql8 mysql -uroot -p
}

function usage()
{
	echo -e "Usage: $0 start|debug|stop|ps|login|mysql_in"
	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
elif [[ $1 == "mysql_in" ]];then
        mysql_in
else
	usage
fi

  1. 脚本使用
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
  1. 配置root远程登录
    这个环境中,mysql的root用户的默认密码为root
# 启动容器后,登录进容器
./mysql_ctl.sh login

# 登录mysql
mysql -uroot -p
# 输入默认密码:root

# 如果是mysql:5.7配置root远程登录
alter user 'root'@'%' identified with mysql_native_password by '密码';
FLUSH PRIVILEGES;

# 如果是mysql8, 好像mysql8.4改为使用插件:caching_sha2_password
ALTER USER 'root'@'%' IDENTIFIED BY '密码';
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;
FLUSH PRIVILEGES;

参考


[1]. podman安装mysql

[2]. MySQL 8.4新特性速览

posted @ 2022-10-09 23:43  BrianSun  阅读(1246)  评论(0编辑  收藏  举报