脚本命令配置mysql_MySQL 自动化部署脚本

一、环境说明

操作系统:CentOS

数据库版本:MySQL 5.7/8.0

参数:buffer pool 会根据系统内存指定、默认双一、GTID、SlowLog

脚本默认安装路径:/usr/local/mysql

脚本默认数据路径:/data/mysql*(根据安装包版本适应 比如 5.7 mysql57)

二、使用方法

服务器上创建一个 /myinstall 临时文件夹

mkdir /myinstall

将本地的 MySQL 安装包 和 mysql_install.py 上传到服务器:

[root@172-16-104-56 test01]# ll

总用量 645732

-rw-r--r--. 1 root root 661214270 1月 11 13:59 mysql-5.7.32-linux-glibc2.12-x86_64.tar.gz

-rw-r--r--. 1 root root 10727 1月 12 18:34 mysql_install.py

运行 mysql_install.py 即可执行

python mysql_install.py mysql-5.7.32-linux-glibc2.12-x86_64.tar.gz

68747470733a2f2f73332e617831782e636f6d2f323032312f30312f31322f73596c73494f2e6d642e706e67

脚本执行完成后会将登入数据库的命令打印出来,直接登入即可

68747470733a2f2f73332e617831782e636f6d2f323032312f30312f31322f73596c557a392e6d642e706e67

接下来修改密码即可:

ALTER USER 'root'@'localhost' IDENTIFIED BY 'YouPassword';

将 MySQL 添加到 .bash_profile 中:

vi /root/.bash_profile

-- 添加到文件中

PATH=$PATH:$HOME/bin:/usr/local/mysql/bin

-- 保存后 source

source /root/.bash_profile

验证:

[root@db01 /]# mysql

mysql mysql_embedded

mysqladmin mysqlimport

mysqlbinlog mysql_install_db

mysqlcheck mysql_plugin

mysql_client_test_embedded mysqlpump

mysql_config mysql_secure_installation

mysql_config_editor mysqlshow

mysqld mysqlslap

mysqld-debug mysql_ssl_rsa_setup

mysqld_multi mysqltest_embedded

mysqld_safe mysql_tzinfo_to_sql

mysqldump mysql_upgrade

mysqldumpslow mysqlxtest

[root@db01 /]#

脚本

import os

import sys

import time

from collections import OrderedDict

class MysqlInstall(object):

def __init__(self, mysql_zip_name):

self.start_instructions()

self.mysql_zip = mysql_zip_name

self.base_path = '/usr/local'

self.data_base = self.verify_package('/data')

def run(self):

"""

Program entrance

:return:

"""

print('loading....')

time.sleep(1)

self.create_sys_mysql_user()

self.unzip()

self.create_data_base()

self.authorization_set()

self.write_conf()

self.init_mysql()

self.start_mysql()

def unzip(self):

"""

unzip mysql

:return:

"""

print('Unpacking the installation package...')

dos_comm_unzip = 'tar -xvf {} -C {}'.format(self.mysql_zip, self.base_path)

os.popen(dos_comm_unzip).read()

temp_path = self.base_path + '/' + self.mysql_zip

now_base_path = temp_path[: temp_path.find('tar') - 1]

dos_comm_mv = 'mv {} {}'.format(now_base_path, self.base_path + '/mysql')

os.popen(dos_comm_mv).read()

print('Successful!')

def create_data_base(self):

"""

Creating a data path.

:return:

"""

print('Creating a data directory...')

dos_create_data_path = 'mkdir -p %s/{data,logs,tmp,run}' % self.data_base

dos_create_error_log = 'touch {}'.format(self.data_base + '/logs/error.log')

os.popen(dos_create_data_path).read()

os.popen(dos_create_error_log).read()

print('Successful!')

def authorization_set(self):

"""

Directory authorization.

:return:

"""

print('Permission setting in progress...')

dos_auth_base_set = 'chown -R mysql:mysql {}/mysql'.format(self.base_path)

dos_auth_data_set = 'chown -R mysql:mysql {}'.format(self.data_base)

os.popen(dos_auth_base_set)

os.popen(dos_auth_data_set)

def write_conf(self):

"""

Write configuration file.

:return:

"""

print('Writing my.conf ...')

client_conf_dict = {

'port': 3306,

'socket': self.data_base + '/data/mysql.sock'

}

mysql_conf_dict = {

'port': 3306,

'socket': self.data_base + '/data/mysql.sock'

}

with open('/etc/my.cnf', 'w') as r1:

r1.truncate()

# write client conf

r1.write('[client]\n')

for x, y in client_conf_dict.items():

temp_write = x + ' = ' + str(y) + '\n'

r1.write(temp_write)

# write mysql conf

r1.write('[mysql]\n')

for x, y in mysql_conf_dict.items():

temp_write = x + ' = ' + str(y) + '\n'

r1.write(temp_write)

# write mysqld conf

r1.write('[mysqld]\n')

for x, y in self.set_mysqld_conf().items():

temp_write = x + ' = ' + str(y) + '\n'

r1.write(temp_write)

print('Successful!')

def init_mysql(self):

"""

Initialize the MySQL.

:return:

"""

print('Initializing MySQL...')

dos_init_mysql = '{}/mysql/bin/mysqld --initialize --user=mysql --basedir={}/mysql/ --datadir={}/data'.format(

self.base_path, self.base_path, self.data_base)

os.popen(dos_init_mysql).read()

if len(os.listdir(self.data_base + '/data')) > 8:

print('Successful!')

else:

print('=' * 100)

print('Error: An exception occurred during initialization.')

print('=' * 100)

sys.exit(0)

def start_mysql(self):

"""

Start the database.

:return:

"""

print('Starting MySQL...')

dos_start_mysql = '/usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf --user=mysql &'

os.system(dos_start_mysql)

print('\n')

if self.check_mysql():

print('Successful! Please enter to exit the program.')

self.get_password()

else:

print('Error: Database startup failure.')

sys.exit(0)

@staticmethod

def create_sys_mysql_user():

"""

Create system users or user groups.

:return:

"""

print('Checking system permissions...')

# if os.popen('echo $LANG $LANGUAGE').read() != 'zh_CN.UTF-8':

# print('[Warning]: A system supported language other than zh_CN.UTF-8 may fail.')

dos_group = os.popen('cat /etc/group | grep mysql').read()

dos_user = os.popen('cat /etc/passwd | grep mysql').read()

if dos_group == '':

os.popen('groupadd mysql').read()

print('User group creation successful!')

else:

print('50% System Permission Detection!')

if dos_user == '':

os.popen('useradd -r -g mysql -s /bin/false mysql').read()

print('User created successfully')

else:

print('100% System Permission Detection!')

@staticmethod

def check_mysql():

"""

Verify that MySQL started successfully.

:return:

"""

response_dos = os.popen('ps -ef | grep mysql').read()

if response_dos.find('mysqld'):

return True

else:

return False

@staticmethod

def get_buffer_size():

"""

To calculate buffer pool size.

:return:

"""

sys_mem_size = 128

dos_response = os.popen('free -m |grep Mem').read()

for i in dos_response.split(' '):

if i.isdigit():

if int(i) > 1024:

# 3788 * 0.7 = int(2651 // 1024 = 2)

sys_mem_size = (int(int(i) * 0.7) // 1024) * 1024

break

return str(sys_mem_size) + 'M'

@staticmethod

def start_instructions():

print('*' * 66)

print('Developer email: wenruo@dtstack.com')

print('*' * 66)

time.sleep(2)

def get_password(self):

"""

Get the password from the error log.

:return:

"""

dos_grep_password = 'grep "password" {}/logs/error.log'.format(self.data_base)

response_dos = os.popen(dos_grep_password).read()

temp_password = response_dos[response_dos.rfind(' ') + 1:]

dos_login_mysql = "{}/mysql/bin/mysql -uroot -p'{}' -S {}/data/mysql.sock".format(self.base_path,

temp_password, self.data_base)

print('=' * 100)

print(dos_login_mysql.replace('\n', '').replace('\r', ''))

print('=' * 100)

def set_mysqld_conf(self):

"""

Set up the configuration file associated with mysqld

:return: mysqld_conf_odict obj

"""

mysqld_conf_odict = OrderedDict()

# base

mysqld_conf_odict['user'] = 'mysql'

mysqld_conf_odict['port'] = 3306

mysqld_conf_odict['basedir'] = self.base_path + '/mysql'

mysqld_conf_odict['datadir'] = self.data_base + '/data'

mysqld_conf_odict['socket'] = self.data_base + '/data/mysql.sock'

mysqld_conf_odict['tmpdir'] = self.data_base + '/tmp'

# server_id

mysqld_conf_odict['server-id'] = 1000

# binlog

mysqld_conf_odict['log-bin'] = self.data_base + '/logs/mysql-bin'

mysqld_conf_odict['sync_binlog'] = 1

mysqld_conf_odict['binlog_cache_size'] = '4M'

mysqld_conf_odict['max_binlog_cache_size'] = '2G'

mysqld_conf_odict['max_binlog_size'] = '1G'

mysqld_conf_odict['expire_logs_days'] = 7

# GTID

mysqld_conf_odict['gtid_mode'] = 'on'

mysqld_conf_odict['enforce_gtid_consistency'] = 1

# level

mysqld_conf_odict['transaction_isolation'] = 'READ-COMMITTED'

# innodb

mysqld_conf_odict['innodb_buffer_pool_size'] = self.get_buffer_size()

mysqld_conf_odict['innodb_buffer_pool_instances'] = 4

mysqld_conf_odict['innodb_buffer_pool_load_at_startup'] = 1

mysqld_conf_odict['innodb_buffer_pool_dump_at_shutdown'] = 1

mysqld_conf_odict['innodb_data_file_path'] = 'ibdata1:500M:autoextend'

mysqld_conf_odict['innodb_temp_data_file_path'] = 'ibtmp1:200M:autoextend'

mysqld_conf_odict['innodb_flush_log_at_trx_commit'] = 1

mysqld_conf_odict['innodb_log_buffer_size'] = '32M'

mysqld_conf_odict['innodb_log_file_size'] = '128MB'

# error_log

mysqld_conf_odict['log-error'] = self.data_base + '/logs/error.log'

# Slow log

mysqld_conf_odict['slow_query_log'] = 1

mysqld_conf_odict['slow_query_log_file'] = self.data_base + '/logs/error.log'

mysqld_conf_odict['long_query_time'] = 1

mysqld_conf_odict['log_queries_not_using_indexes'] = 1

mysqld_conf_odict['log_throttle_queries_not_using_indexes'] = 60

mysqld_conf_odict['min_examined_row_limit'] = 0

mysqld_conf_odict['log_slow_admin_statements'] = 1

# connections

mysqld_conf_odict['max_connections'] = 1000

mysqld_conf_odict['max_user_connections'] = 64

# other

mysqld_conf_odict['pid-file'] = 'mysql1.pid'

mysqld_conf_odict['innodb_doublewrite'] = 1

mysqld_conf_odict['character-set-server'] = 'utf8mb4'

mysqld_conf_odict['skip_name_resolve'] = 1

mysqld_conf_odict['open_files_limit'] = 65535

mysqld_conf_odict['autocommit'] = 1

mysqld_conf_odict['innodb_sort_buffer_size'] = 1048576

mysqld_conf_odict['explicit_defaults_for_timestamp'] = 1

# How do I need to add a custom configuration in the following code format

# mysqld_conf_odict[''] = 'xxx'

return mysqld_conf_odict

def verify_package(self, data_path):

print('Validating the installation package...')

dos_md5 = 'md5sum ' + self.mysql_zip

md5_response = os.popen(dos_md5).read()

if md5_response == '':

print(md5_response)

print('Error: Please specify the correct installation package.')

sys.exit(0)

else:

print(md5_response)

sp_name = self.mysql_zip.split('-')[1]

db_version = int(sp_name[0:1] + sp_name[2:3])

return data_path + '/mysql_' + str(db_version)

if __name__ == '__main__':

try:

zip_name = sys.argv[1]

except IndexError:

print('Please specify the MySQL installation package path.')

sys.exit(0)

install_obj = MysqlInstall(zip_name)

install_obj.run()

摘自:https://github.com/DooBeDooBa/mysql_lib

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/459184.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

STL 整理(map、set、vector、list、stack、queue、deque、priority_queue)

*************************************************** 更多精彩,欢迎进入:http://shop115376623.taobao.com STL视频教程: http://item.taobao.com/item.htm?spma1z10.5-c.w4002-9510581626.21.y9vLuz&id43055362725 ************…

第2章 数字之魅——快速寻找满足条件的两个数

快速寻找满足条件的两个数 问题描述 能否快速找出一个数组中的两个数字,让这两个数字之和等于一个给定的数字,为了简化起见,我们假设这个数组中肯定存在这样一组或以上符合要求的解。 分析与解法 【解法一】 代码如下: 1 package …

eigen 列向量转矩阵_快速入门矩阵运算——开源库Eigen

矩阵是数学中一个重要的工具,广泛应用于各种场景下的数值分析,例如,数字信号处理,图像处理等。我们如何在程序中使用矩阵进行运算呢?本文将为大家介绍一个开源的矩阵运算工具——Eigen。Eigen is a C template library…

优化android studio编译的apk大小

默认生成的apk(release版)太大(只打印helloworld的apk大小就有1MB多),需要优化编译,如下,修改build.gradlebuildTypes { release {minifyEnabled true zipAlignEnabled true s…

STL set和multiset

*************************************************** 更多精彩,欢迎进入:http://shop115376623.taobao.com STL视频教程: http://item.taobao.com/item.htm?spma1z10.5-c.w4002-9510581626.21.y9vLuz&id43055362725 ***************…

cassandra 环境搭建

1 下载安装包 http://www.planetcassandra.org/cassandra/?dlinkhttp://downloads.datastax.com/community/dsc-cassandra-2.1.5-bin.tar.gz 用tar包的形式安装,解压缩之后就可以用;选择2.1.5,因为golang 客户端gocql的git 首页上支持最新的…

mysql raid_DBA们应该知道的RAID卡知识_MySQL

bitsCN.com对于数据库这种特殊应用IOphotoshop/ target_blank classinfotextkey>PS往往会成为瓶颈,突破的这个瓶颈的有效方法不多,软件方面主要是读写分离,垂直拆分,分区表技术,cluster。硬件方面主要是raid&#x…

基于Maven的SSH框架搭建

2019独角兽企业重金招聘Python工程师标准>>> 1.工程介绍 工程是结合了Springstruts2hibernate,实现了一个简单的form表单提交的功能,可能需要对spring,struts2,hibernate有一个基础的了解才好理解。 2.工程结构图 首先…

交通警察手势信号(动画演示)

*************************************************** 更多精彩,欢迎进入:http://shop115376623.taobao.com *************************************************** 一、交通警察手势信号-停止信号 二、交通警察手势信号-直行信…

==与equal在java中应用的感悟

今天又算是长见识了。了解了下平时不注意的equal和的区别。 不管是又或是equal都是用来比较相同与否。当问题就在这里了,比较什么相同呢? 我的在日常的比较无非也就是两种:1、基本数据类型之间的比较,2、引用类型数据之间的比较。…

mysql和mybatis面试题_BATJ面试题汇总详解:MyBatis+MySQL+Spring+Redis+多线程

SpringSpring 概述什么是spring?使用Spring框架的好处是什么?Spring由哪些模块组成?解释AOP模块Spring配置文件什么是Spring IOC 容器?依赖注入什么是Spring的依赖注入?有哪些不同类型的IOC(依赖注入)方式?哪种依赖注…

Codeblocks和gdb调试 (转)

*************************************************** 更多精彩,欢迎进入:http://shop115376623.taobao.com *************************************************** 使用C::B和gdb调试是一件简单的事情。下面,让我们调试一个简单的循环&…

Git的安装和使用(Linux)【转】

转自:http://my.oschina.net/fhd/blog/354685 Git诞生于Linux平台并作为版本控制系统率先服务于Linux内核,因此在Linux上安装Git是非常方便的。可以通过两种不同的方式在Linux上安装Git:一种方法是通过Linux发行版的包管理器安装已经编译好的…

Magento--修改已存在的订单的运费

遇到一种情况,需要在下单后再由管理员添加订单运费,然后顾客再付款。那么问题来了,如何给订单添加运费呢?下面是一段代码,可以实现该功能: $orderId your order id;$order Mage::getModel(sales/order)-&…

mysql5.7.17 win7_win7下mysql5.7.17安装配置方法图文教程

win7下安装mysql5.7.17图文教程,分享给大家。1.下载安装包请在以下有zip包和msi两种类型包,建议新手选择zip包下载,有助于熟习mysql2.解压mysql压缩包下载完成后解压,将其放在要安装的目录下面,如:e:\mysql…

华为软件研发面试题1

from:http://blog.csdn.net/eddy_0825/article/details/1875574 1、局部变量能否和全局变量重名? 答:能,局部会屏蔽全局。要用全局变量,需要使用"::" 局部变量可以与全局变量同名,在函数内引用这…

停一下

15年过去已半载有余,回头看年初定下的目标,有种管中窥豹的感觉。之前和肉山讨论的时候,他对我想要发展的方向并没有表示赞同。 现在认为他是对的,发展的方向太靠前了,ui,canvas,svg,…

which 命令

我们经常在linux要查找某个文件,但不知道放在哪里了,可以使用下面的一些命令来搜索: which 查看可执行文件的位置。 whereis 查看文件的位置。 locate 配合数据库查看文件位置。 find 实际搜寻硬盘查询文件名…

18ch

18.2 线程和进程 18.2.1 什么是进程? 18.2.1 什么是进程? 计算机程序只不过是磁盘中可执行的,二进制的数据。它们只有在被读取到内存中,被操作系统调用的时候才开始它们的生命周期。进程(重量级进程)是程序…

java sql分页_mysql、sqlserver、oracle分页,java分页统一接口实现

定义&#xff1a;pageStart 起始页&#xff0c;pageEnd 终止页,pageSize页面容量oracle分页&#xff1a;select * from ( select mytable.*,rownum num from (实际传的SQL) where rownum<pageEnd) where num>pageStartsqlServer分页&#xff1a;select * from ( select …