python之mysql安装

docker安装mysql

主要步骤:
1.安装docker
2.拉取mysql7.5镜像
3.创建并启动mysql容器

安装docker

yum install docker -y
systemctl restart docker 
systemctl enable docker

给docker加上国内镜像站,加快拉取镜像时间:

vim /etc/docker/daemon.json

添加以下内容:

{
        "registry-mirrors": ["https://registry.docker-cn.com"]
}

启动docker

sudo systemctl daemon-reload
sudo systemctl restart docker

sudo docker pull mysql:5.7
sudo docker image ls

创建挂载目录

sudo mkdir -p /data/docker_mysql5.7/{cnf,data}

写入配置文件

cat > /data/docker_mysql5.7/cnf/my.cnf << EOF
[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/
innodb_buffer_pool_size = 1G
skip-host-cache
# 禁用dns解析
skip-name-resolve
EOF

创建mysql容器并启动:

sudo docker run \
    -p 3307:3306 \
    -v /data/docker_mysql5.7/data:/var/lib/mysql \
    -v /data/docker_mysql5.7/cnf:/etc/mysql \
    -v /etc/localtime:/etc/localtime:ro \
    --privileged=true \
    --name docker_mysql5.7 \
    --restart always \
    -e MYSQL_ROOT_PASSWORD=123456 \
    -d mysql:5.7

进入容器

sudo docker exec -it docker_mysql5.7  bash
mysql -u root -p


python安装myql脚本

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import os
import json
import subprocess

mount_path = "/data/docker_mysql5.7"   # 挂载路径

# my.cnf 配置文件
my_cnf = """[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/
innodb_buffer_pool_size = 1G
skip-host-cache
skip-name-resolve    # 禁用dns解析
max_connections=1000 #最大连接数
"""

# 运行容器命令
run_cmd = r"""sudo docker run \
    -p 3307:3306 \
    -v /data/docker_mysql5.7/data:/var/lib/mysql \
    -v /data/docker_mysql5.7/cnf:/etc/mysql \
    -v /etc/localtime:/etc/localtime:ro \
    --privileged=true \
    --name docker_mysql5.7 \
    --restart always \
    -e MYSQL_ROOT_PASSWORD=123456 \
    -d mysql:5.7
"""


# docker 安装
def docker_install():
    docker_daemon = {"registry-mirrors": ["https://registry.docker-cn.com"]}   # daemon.json
    daemon_path = r"/etc/docker/daemon.json"
    #docker检测是否安装
    if not os.system("docker version"):
        print("docker 已安装")
    else:
        print("docker 正在安装...")
        os.system("yum install docker -y")
        os.system("systemctl restart docker")
        os.system("systemctl enable docker")
        os.system("systemctl enable docker")
        with open(daemon_path, "w", encoding="utf-8") as f:
            json.dump(docker_daemon, f, ensure_ascii=False, indent=4)

# mysql 安装
def mysql_install(my_cnf: str, mount_path: str, docker_run: str):
    """
    my_cnf: str  # my.cnf
    mount_path: str   # 挂载的目录
    docker_run: str   #  docker run命令
    """
    os.system("docker pull mysql:5.7")    # 拉取镜像
    os.system("mkdir -p {}/cnf".format(mount_path))
    os.system("mkdir -p {}/data".format(mount_path))
    config_path = "{}/cnf/my.cnf".format(mount_path)
    with open(config_path, "w") as f:
        f.write(my_cnf)
    os.system(docker_run)
    os.system("docker ps -a |grep mysql")

docker_install()
mysql_install(my_cnf, mount_path, run_cmd)

续更

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值