栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python-paramiko的初步使用(二)mysql部署

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python-paramiko的初步使用(二)mysql部署

一、背景

自动化部署mysql

二、初步实现功能的脚本
import paramiko
import time
# 检验接收到的数据,来决定是否恶意进行下一步
def check_end(channel, end_str):
    buff = ''
    while not buff.endswith(end_str):
        resp = channel.recv(9999) # 获取数据,最大9999字节
        buff += resp.decode('utf-8')

def install_mysql():
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect(hostname=server_host, port=server_port, username=server_user, password=server_password)
    transport = ssh.get_transport()
    # 实例化一个 sftp对象,指定连接的通道
    sftp = paramiko.SFTPClient.from_transport(transport)
    time.sleep(0.1)
    # 检查程序及对应目录
    stdin,stdout,stderr = ssh.exec_command('ps -ef|grep mysql')
    if "mysqld" in stdout.read().decode('utf-8'):
        print("mysql程序已经存在,请确认")
        return
    stdin,stdout,stderr = ssh.exec_command('[ ! -d "%s" ] || echo yes' % base_dir)
    if "yes" in stdout.read().decode('utf-8'):
        print("mysql的base目录已经存在,请确认或更改目录")
        return
    stdin,stdout,stderr = ssh.exec_command('[ ! -d "%s" ] || echo yes' % mysql_dir)
    if "yes" in stdout.read().decode('utf-8'):
        print("mysql目录已经存在,请确认或更改目录")
        return
    # 上传配置文件及安装包
    sftp.put('%s' % cnf_file, '%s' % server_cnf_file)
    print(server_tar_file)
    stdin,stdout,stderr = ssh.exec_command('[ ! -f "%s" ] || echo yes' % server_tar_file)
    if "yes" in stdout.read().decode('utf-8'):
        print("安装包已经存在")
    else:
        sftp.put('%s' % tar_file, '%s' % server_tar_file)

    # 检查基本环境及用户
    stdin,stdout,stderr = ssh.exec_command('cat /etc/group|grep "^%s:"' % mysql_user)
    if mysql_user  in stdout.read().decode('utf-8'):
        print("该用户组已经存在")
    else:
        print("用户组未创建")
        ssh.exec_command('groupadd %s' % mysql_user)
    stdin,stdout,stderr = ssh.exec_command('id %s' % mysql_user)
    if "uid=" in stdout.read().decode('utf-8'):
        print("该用户已经存在")
    else:
        print("用户未创建i123")
        ssh.exec_command('useradd -r -g %s -s /bin/false %s' % (mysql_group, mysql_user))
    channel = ssh.invoke_shell()
    channel.send("mkdir -p %s n" % data_dir)
    check_end(channel, '# ')
    channel.send("mkdir -p %s n" % tmp_dir)
    check_end(channel, '# ')
    channel.send(" chown -R %s:%s %s n" % (mysql_group, mysql_user,mysql_dir))
    check_end(channel, '# ')
    print('开始搭建')
    bin_dir = base_dir + "/bin"
    channel.send("cd %s  n" % bin_dir)
    check_end(channel, '# ')
    channel.send("./mysqld --initialize-insecure --user=%s --basedir=%s --datadir=%s  n" % (mysql_user, base_dir, data_dir))
    check_end(channel, '# ')
    print('初始化完成,开始加密')
    ssh.exec_command("/usr/local/mysql/bin/mysql_ssl_rsa_setup --datadir=%s" % data_dir)
    print('开始启动')
    ssh.exec_command("/usr/local/mysql/bin/mysqld_safe  --defaults-file=%s --user=%s & n " % (server_cnf_file, mysql_user))
    channel.close()
    ssh.close()

server_host = '127.0.0.1'
server_port = 22
server_user = 'user'
server_password = 'password'
mysql_user = 'mysql'
mysql_group = 'mysql'
base_dir = '/usr/local/mysql'
mysql_dir = '/data/mysql2'
data_dir = mysql_dir + '/data'
tmp_dir = mysql_dir + '/tmp'
file_path = '/data/it_tool'
cnf_file = file_path +  '/my.conf'
tar_file = file_path +  '/mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz'
server_tar_file = '/data/mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz'
server_cnf_file = '/etc/my2.cnf'
install_mysql()
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/269459.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号