Carry の Blog Carry の Blog
首页
  • Nginx
  • Prometheus
  • Iptables
  • Systemd
  • Firewalld
  • Docker
  • Sshd
  • DBA工作笔记
  • MySQL
  • Redis
  • TiDB
  • Elasticsearch
  • Python
  • Shell
  • MySQL8-SOP手册
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

Carry の Blog

好记性不如烂键盘
首页
  • Nginx
  • Prometheus
  • Iptables
  • Systemd
  • Firewalld
  • Docker
  • Sshd
  • DBA工作笔记
  • MySQL
  • Redis
  • TiDB
  • Elasticsearch
  • Python
  • Shell
  • MySQL8-SOP手册
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • MySQL

    • MySQL8一键安装配置优化
      • install_mysql8.py
      • my.cnf
      • mysql
      • 使用方法
    • MySQL导出CSV格式解决乱码
    • MySQL 角色管理
    • MySQL网络抓包审计
    • MySQL性能压测
    • MySQL配合Consul读写分离
    • Gh-ost重建表,清除表碎片率
    • MySQL MGR配合MySQL-router实现innodb-cluster
    • MySQL 快速分析binlog定位问题
    • MySQL执行计划分析
    • DBA常用SQL和命令整理备查
    • mysqldump实时同步数据
    • MySQL的事务隔离级别
    • MySQL存储过程批量生成数据
    • MySQL insert on duplicate key update,replace into , insert ignore的理解
    • MySQL不同字符集之间的区别和选择
    • MySQL为什么有时候会选错索引
    • MySQL死锁问题
    • MySQL使用SQL语句查重去重
    • MySQLdump逻辑备份
    • MySQL主从跳过异常GITD
    • MySQL8设置slowlog记录所有语句
    • MySQL8快速克隆插件使用指南
    • MySQL8双1设置保障安全
    • MySQL锁
    • innodb cluster安装
    • MySQL里的left join 和right join以及inner join
    • optimize table和 analyze table的区别
    • MySQL 字段的区分度计算公式
    • MySQLReplicaSet 安装
    • 脚本实现MySQL ReplicaSet 高可用
    • MySQL 的Left join,Right join和Inner join 的区别
    • MySQL45讲学习笔记
  • Redis

  • Keydb

  • TiDB

  • MongoDB

  • Elasticsearch

  • Kafka

  • victoriametrics

  • BigData

  • Sqlserver

  • 数据库
  • MySQL
Carry の Blog
2022-03-10
目录

MySQL8一键安装配置优化

# Mysql安装脚本

# install_mysql8.py

# -*- coding: utf-8 -*-

import sys

reload(sys)
import getopt
import commands
import subprocess
import fileinput
import os

__author__ = 'carry'

INFO = "\033[1;33;40m%s\033[0m"
ERROR = "\033[1;31;40m%s\033[0m"
NOTICE = "\033[1;32;40m%s\033[0m"
LINE = "#" * 50
LINE_ERROR = "-" * 50


def CheckAndHelp(project_name, option):
    usage_help = '''Usage: python %s install [OPTION] [arg...]''' % project_name
    example = '''Example:

            Simple: python mysql_install.py install --instance-ports=3366
            Multiple instances: python mysql_install.py install --instance-ports=3366,3399,4466


            If you know enough to the MySQL, you can use configure area:

            Simple:

                python %s install --instance-ports=3366 --innodb-buffer-pool-size=1G

            '''% project_name
    configure_usage = '''
            '''

    install_usage = '''install:
            custom  install:
                    --instance-ports            default:3306
                    --mysql-user                default:mysql
                    --base-prefix               default:/usr/local/mysql
                    --data-prefix               default:/data/db
            '''

    if option == "install" or option is None:
        usage = usage_help + "\n" + example + "\n" + install_usage + "\n" + configure_usage
        return usage

    else:
        usage = usage_help + "\n" + example + "\n" + install_usage + "\n" + configure_usage
        return usage

def CheckArgv(argvs):
    check_start_message = "Check Argument Start . . ."
    print LINE
    print INFO % check_start_message
    print LINE

    invalid_list = []
    result_dic = {}
    #print argvs,"???"
    try:
        opts, args = getopt.getopt(argvs, "v",
                                   ["instance-ports=", "mysql-user=", "base-prefix=", "data-prefix=", "log-prefix=",
                                    "max-allowed-packet=", "max-binlog-size=", "binlog-cache-size=",
                                    "binlog_expire_logs_seconds=",
                                    "slow-query-log=", "long-query-time=", "log-queries-not-using-indexes=",
                                    "key-buffer-size=",
                                    "innodb-data-file-path=", "innodb-buffer-pool-size=", "innodb-log-file-size=",
                                    "innodb-log-buffer-size=",
                                    "innodb-thread-concurrency=", "innodb-write-io-threads=", "innodb-read-io-threads=",
                                    "innodb_io_capacity=",
                                    "max-connections=", "read-buffer-size=", "read-rnd-buffer-size=", "tmp-table-size=",
                                    "max-heap-table-size=",
                                    "thread-cache-size=", "wait-timeout="])
        #print opts, "opts++"
        #print args, "args--"
        result_dic = {'result_state': 'true', 'result': opts}
    except getopt.GetoptError, err:
        # print err
        err_msg = "     use -h or -help check"
        result_dic = {'result_state': 'false', 'result': str(err) + "\n\n" + err_msg}

    # print result_dic
    return result_dic


def CreateConfigurationFiles(configure):
    # print configure

    instance_ports = '3306'
    mysql_user = 'mysql'
    base_prefix = '/usr/local/mysql'
    data_prefix = '/data/db'
    log_prefix = '/data/dblogs'
    max_allowed_packet = '64M'
    max_binlog_size = '1024M'
    binlog_cache_size = '4M'
    binlog_expire_logs_seconds = '2592000'
    slow_query_log = '0'
    long_query_time = '1'
    log_queries_not_using_indexes = '0'
    key_buffer_size = '64M'
    innodb_data_file_path = 'ibdata1:1G:autoextend'
    innodb_log_file_size = '256M'
    innodb_log_buffer_size = '2M'
    innodb_io_capacity = '200'
    max_connections = '5000'
    read_buffer_size = '8M'
    read_rnd_buffer_size = '16M'
    tmp_table_size = '128M'
    max_heap_table_size = '128M'
    wait_timeout = '3600'
    system_free_memory = commands.getstatusoutput("cat /proc/meminfo | grep MemFree|awk '{print $(NF-1)}'")[1]
    # system_total_memory = '1048576'
    system_total_memory = commands.getstatusoutput("cat /proc/meminfo | grep MemTotal|awk '{print $(NF-1)}'")[1]
    sort_buffer_size = '16M'
    # innodb_buffer_pool_size = '128M'
    innodb_buffer_pool_size = '%sM' % int((int(system_free_memory) / 1024 - int(key_buffer_size[:-1]) - int(
        max_connections) * (int(sort_buffer_size[:-1]) + int(read_buffer_size[:-1]) + int(
        binlog_cache_size[:-1])) - int(max_connections) - int(max_binlog_size[:-1]) - int(tmp_table_size[:-1])) * 0.95)
    if int(innodb_buffer_pool_size[:-1]) < 0:
        innodb_buffer_pool_size = '512M'
    cpu_core = commands.getstatusoutput("cat /proc/cpuinfo | grep processor | wc -l")[1]
    # innodb_thread_concurrency = '8'
    # innodb_write_io_threads = '4'
    # innodb_read_io_threads = '4'
    r1 = commands.getstatusoutput(""" ip a|grep 'scope global'|awk -F[:' '/]+ '{print $3}'|grep -v ^127|tail -1 """)[1].split('.')[2:]
    server_id = "".join(r1)
    innodb_thread_concurrency = '%s' % (int(cpu_core) * 2)
    innodb_write_io_threads = '%s' % (int(cpu_core))
    innodb_read_io_threads = '%s' % (int(cpu_core))
    thread_cache_size = '%s' % int(int(max_connections) * 0.1)
    default_argv = {'--server-id': server_id, '--instance-ports': instance_ports, '--mysql-user': mysql_user, '--base-prefix': base_prefix,
                    '--data-prefix': data_prefix,
                    '--log-prefix': log_prefix, '--max-allowed-packet': max_allowed_packet,
                    '--max-binlog-size': max_binlog_size, '--binlog-cache-size': binlog_cache_size,
                    '--binlog_expire_logs_seconds': binlog_expire_logs_seconds, '--slow-query-log': slow_query_log,
                    '--long-query-time': long_query_time,
                    '--log-queries-not-using-indexes': log_queries_not_using_indexes,
                    '--key-buffer-size': key_buffer_size, '--innodb-data-file-path': innodb_data_file_path,
                    '--innodb-buffer-pool-size': innodb_buffer_pool_size,
                    '--innodb-log-file-size': innodb_log_file_size, '--innodb-log-buffer-size': innodb_log_buffer_size,
                    '--innodb-thread-concurrency': innodb_thread_concurrency,
                    '--innodb-write-io-threads': innodb_write_io_threads,
                    '--innodb-read-io-threads': innodb_read_io_threads,
                    '--innodb_io_capacity': innodb_io_capacity, '--max-connections': max_connections,
                    '--read-buffer-size': read_buffer_size, '--read-rnd-buffer-size': read_rnd_buffer_size,
                    '--tmp-table-size': tmp_table_size, '--max-heap-table-size': max_heap_table_size,
                    '--thread-cache-size': thread_cache_size,
                    '--wait-timeout': wait_timeout
                    }

    for k, v in configure:
        if k == "--max-allowed-packet":
            if int(v[:-1]) < 1000:
                default_argv[k] = v
                continue
            else:
                default_argv[k] = max_allowed_packet
                msg = "%s   The Value Is Unavailable, Change Default Value" % k
                print LINE
                print NOTICE % msg
                print LINE
                continue

        elif k == "--max-binlog-size":
            if int(v[:-1]) < 2000:
                default_argv[k] = v
                continue
            else:
                default_argv[k] = max_binlog_size
                msg = "%s   The Value Is Unavailable, Change Default Value" % k
                print LINE
                print NOTICE % msg
                print LINE
                continue

        elif k == "--binlog-cache-size":
            if 2 <= int(v[:-1]) <= 4:
                default_argv[k] = v
                continue
            else:
                default_argv[k] = binlog_cache_size
                msg = "%s   The Value Is Unavailable, Change Default Value" % k
                print LINE
                print NOTICE % msg
                print LINE
                continue

        elif k == "--key-buffer-size":
            if int(v[:-1]) <= int(int(system_total_memory) / 1024 * 0.2):
                default_argv[k] = v
                continue
            else:
                default_argv[k] = key_buffer_size
                msg = "%s   The Value Is Unavailable, Change Default Value" % k
                print LINE
                print NOTICE % msg
                print LINE
                continue

        elif k == "--tmp-table-size":
            if int(v[:-1]) <= int(int(system_total_memory) / 1024 * 0.1):
                default_argv[k] = v
                continue
            else:
                default_argv[k] = tmp_table_size
                msg = "%s   The Value Is Unavailable, Change Default Value" % k
                print LINE
                print NOTICE % msg
                print LINE
                continue

        elif k == "--max-heap-table-size":
            if int(v[:-1]) <= int(int(system_total_memory) / 1024 * 0.1):
                default_argv[k] = v
                continue
            else:
                default_argv[k] = max_heap_table_size
                msg = "%s   The Value Is Unavailable, Change Default Value" % k
                print LINE
                print NOTICE % msg
                print LINE
                continue
        else:
            default_argv[k] = v

    #default_argv['--server-id'] = "".join(server_id) + instance_ports
    #print default_argv
    check_finish_message = "Check Argument Finish"
    print LINE
    print INFO % check_finish_message
    print LINE
    return default_argv


def CheckReplaceFile(configure_dic):
    check_environment_start_message = "Check Environment Start . . ."
    print LINE
    print INFO % check_environment_start_message
    print LINE

    data_file_path = "%s/mysql%s" % (configure_dic['--data-prefix'], configure_dic['--instance-ports'])
    log_file_path = "%s/mysql%s" % (configure_dic['--log-prefix'], configure_dic['--instance-ports'])
    local_file = 'my.cnf'
    # if os.path.exists(configure_dic['--base-prefix']):
    #     pass
    # else:
    #     commands.getstatusoutput("mkdir -p %s" % configure_dic['--base-prefix'])
    #     print "The %s create success" % configure_dic['--base-prefix']

    if os.path.exists(configure_dic['--data-prefix']):
        pass
    else:
        commands.getstatusoutput("mkdir -p %s" % configure_dic['--data-prefix'])
        msg = "The Path: %s Create Success" % configure_dic['--data-prefix']
        print LINE
        print NOTICE % msg
        print LINE

    if os.path.exists(configure_dic['--log-prefix']):
        pass
    else:
        commands.getstatusoutput("mkdir -p %s" % configure_dic['--log-prefix'])
        msg = "The Path: %s Create Success" % configure_dic['--log-prefix']
        print LINE
        print NOTICE % msg
        print LINE

    if os.path.exists(data_file_path):
        msg = "[Error]: The MySQL Instance Port: %s exists, Pleace Change" % configure_dic['--instance-ports']
        print LINE_ERROR
        print ERROR % msg
        print LINE_ERROR
        sys.exit()
    else:
        commands.getstatusoutput("mkdir -p %s" % data_file_path)
        msg = "The Path: %s Create Success" % data_file_path
        print LINE
        print NOTICE % msg
        print LINE

    if os.path.exists(log_file_path):
        msg = "[Error]: The MySQL Instance Port: %s exists, Pleace Change" % configure_dic['--instance-ports']
        print LINE_ERROR
        print ERROR % msg
        print LINE_ERROR
        sys.exit()
    else:
        commands.getstatusoutput("mkdir -p %s" % log_file_path)
        msg = "The Path: %s Create Success" % log_file_path
        print LINE
        print NOTICE % msg
        print LINE

    if int(commands.getstatusoutput("id %s" % configure_dic['--mysql-user'])[0]) == 0:
        pass
    else:
        commands.getstatusoutput("useradd %s" % configure_dic['--mysql-user'])
        msg = "Add MySQL User: %s Succsee" % configure_dic['--mysql-user']
        print LINE
        print NOTICE % msg
        print LINE

    check_environment_finish_message = "Check Environment Finish"
    print LINE
    print INFO % check_environment_finish_message
    print LINE

    create_file_start_message = "Create MySQL Configuration File Start . . ."
    print LINE
    print INFO % create_file_start_message
    print LINE
    cp_file = commands.getstatusoutput("cp -fr %s  %s" % (local_file, data_file_path))
    if int(cp_file[0]) == 0:
        pass
    else:
        print cp_file[1]

    mysql_file = os.path.join(data_file_path, local_file)
    #print configure_dic
    for k, v in configure_dic.items():
        commands.getstatusoutput("sed -i 's#$%s#%s#g' %s" % (k, v, mysql_file))

    result_dic = {'result_state': "true", 'result': mysql_file}

    create_file_finish_message = "Create MySQL Configuration File Finish"
    print LINE
    print INFO % create_file_finish_message
    print LINE

    return result_dic


def InstallMysql(install_cfg, filename):
    install_start_message = "Install MySQL Start Port: %s. . ." % install_cfg['--instance-ports']
    print LINE
    print INFO % install_start_message
    print LINE

    #rootpwd = "root"
    mysql_conf_name = "my.cnf"
    old_mysql_file = "/etc/my.cnf"
    data_file_path = "%s/mysql%s" % (install_cfg['--data-prefix'], install_cfg['--instance-ports'])
    log_file_path = "%s/mysql%s" % (install_cfg['--log-prefix'], install_cfg['--instance-ports'])
    data_dir = ['data','mysqltmp','filedir']
    log_dir = ['binlogs', 'slowlogs', 'relaylogs']
    install_dir = "/usr/local/"
    mysql_src_tar = filename
    mysql_src_name = mysql_src_tar.replace(".tar","").strip()
    print mysql_src_tar, mysql_src_name
    mysql_run_script = 'mysql'
    mysql_safe_file = '%s/bin/mysqld_safe' % install_cfg['--base-prefix']
    mysql_conf_file = os.path.join(data_file_path, mysql_conf_name)
    mysql_run_file = os.path.join(data_file_path, mysql_run_script)
    sys_run_file = "/etc/init.d/mysql%s" % install_cfg['--instance-ports']
    mysql_sock = "%s/mysql%s/mysql%s.sock" % (install_cfg['--data-prefix'], install_cfg['--instance-ports'], install_cfg['--instance-ports'])

    if os.path.exists(install_cfg['--base-prefix']) and os.path.exists(os.path.join(install_dir, mysql_src_name)):
        pass
    else:
        tar_mysql = commands.getstatusoutput("tar xvf %s -C %s" % (mysql_src_tar, install_dir))
        if int(tar_mysql[0]) == 0:
            commands.getstatusoutput(
                "ln -s %s %s" % (os.path.join(install_dir, mysql_src_name), install_cfg['--base-prefix']))
            print install_dir, mysql_src_name

    create_environment_start_message = "Create MySQL Environment Start . . ."
    print LINE
    print INFO % create_environment_start_message
    print LINE

    if os.path.exists('/usr/sbin/lsof'):
        pass
    else:
        lsof_install = commands.getstatusoutput("yum install lsof -y")
        if int(lsof_install[0]) != 0:
            msg = "[Error]: Failed To Install Lsof"
            print LINE_ERROR
            print ERROR % msg
            print LINE_ERROR
            sys.exit()

    if os.path.exists('/usr/bin/perl'):
        pass
    else:
        perl_install = commands.getstatusoutput("yum install perl -y")
        if int(perl_install[0]) != 0:
            msg = "[Error]: Failed To Install Perl"
            print LINE_ERROR
            print ERROR % msg
            print LINE_ERROR
            sys.exit()

    if os.path.exists('/lib64/libaio.so.1'):
        pass
    else:
        libaio_install = commands.getstatusoutput("yum install libaio-devel libaio numactl -y")
        if int(libaio_install[0]) != 0:
            msg = "[Error]: Failed To Install Libaio"
            print LINE_ERROR
            print ERROR % msg
            print LINE_ERROR
            sys.exit()

    commands.getstatusoutput("sed -i 's#/usr/local/mysql#'%s'#g' %s" % (install_cfg['--base-prefix'], mysql_safe_file))

    if os.path.exists(old_mysql_file):
        commands.getstatusoutput("mv %s %s.bak" % (old_mysql_file, old_mysql_file))

    for data in data_dir:
        commands.getstatusoutput("mkdir -p %s/%s" % (data_file_path, data))

    for log in log_dir:
        commands.getstatusoutput("mkdir -p %s/%s" % (log_file_path, log))

    commands.getstatusoutput("cp -fr %s %s" % (mysql_run_script, data_file_path))
    commands.getstatusoutput("chown -R mysql.mysql %s" % data_file_path)
    commands.getstatusoutput("chown -R mysql.mysql %s" % log_file_path)

    commands.getstatusoutput("sed -i 's#{MYCNF-DIR}#'%s'#g' %s" % (mysql_conf_file, mysql_run_file))
    commands.getstatusoutput("sed -i 's#{BIN-DIR}#'%s/bin'#g' %s" % (install_cfg['--base-prefix'], mysql_run_file))
    commands.getstatusoutput(
        "sed -i 's#{PID-DIR}#'%s/mysql%s.pid'#g' %s" % (log_file_path, install_cfg['--instance-ports'], mysql_run_file))
    commands.getstatusoutput("cp -fr %s %s" % (mysql_run_file, sys_run_file))
    # commands.getstatusoutput("/bin/rm -f /etc/init.d/mysqld")

    commands.getstatusoutput("chmod 700 %s" % sys_run_file)

    commands.getstatusoutput("/sbin/chkconfig add mysql%s" % install_cfg['--base-prefix'])
    commands.getstatusoutput("/sbin/chkconfig mysql%s on" % install_cfg['--base-prefix'])
    commands.getstatusoutput("find %s -name mysql -exec chmod 700 {} \;" % data_file_path)

    mysql_value = commands.getstatusoutput("grep -w mysql%s /etc/profile | wc -l" % install_cfg['--instance-ports'])
    if int(mysql_value[1]) == 0:
        commands.getstatusoutput("echo alias mysql%s='\"'%s/bin/mysql --defaults-file=%s -S %s'\"' >> /etc/profile" % (
            install_cfg['--instance-ports'], install_cfg['--base-prefix'], mysql_conf_file, mysql_sock))

    create_environment_finish_message = "Create MySQL Environment Finish"
    print LINE
    print INFO % create_environment_finish_message
    print LINE

    initialize_mysql_start_message = "Initialize MySQL . . ."
    print LINE
    print INFO % initialize_mysql_start_message
    print LINE
    MYSTR = "%s/bin/mysqld --defaults-file=%s --basedir=%s  --user=mysql --initialize --explicit_defaults_for_timestamp  >/dev/null" % (
        install_cfg['--base-prefix'], mysql_conf_file, install_cfg['--base-prefix'])
    print INFO % MYSTR
    # mysql_initialize = commands.getstatusoutput()
    scommand = "%s/bin/mysqld --defaults-file=%s --basedir=%s  --user=mysql --initialize --explicit_defaults_for_timestamp " % (
        install_cfg['--base-prefix'], mysql_conf_file, install_cfg['--base-prefix'])
    print INFO % scommand
    status = subprocess.call(scommand, shell=True)
    if status > 0:
        msg = "[Error]: Failed to initialize MySQL data directory. Port: %s" % install_cfg['--instance-ports']
        print LINE_ERROR
        print ERROR % msg
        print LINE_ERROR
        sys.exit()
    #
    #
    # mysql_initialize = commands.getstatusoutput("%s/scripts/mysql_install_db --basedir=%s --defaults-file=%s --user=mysql --explicit_defaults_for_timestamp >/dev/null" % (install_cfg['--base-prefix'], install_cfg['--base-prefix'], mysql_conf_file))

    # if mysql_initialize != 0:

    initialize_mysql_finish_message = "Initialize MySQL Finish"
    print LINE
    print INFO % initialize_mysql_finish_message
    print LINE
    mysql_start_message = "MySQL Start . . ."
    print LINE
    print INFO % mysql_start_message
    print LINE
    print "the sys_run_file is %s" % sys_run_file
    mysql_start = subprocess.call([sys_run_file, 'start'])
    print "start print mysql_start"
    if mysql_start> 0:
        print "MySQL failed to start on %s" % install_cfg['--instance-ports']
        sys.exit()
    mysql_finish_message = "MySQL Start Finish"
    print LINE
    print INFO % mysql_finish_message
    print LINE

    mysql_privileges_start_message = "Cfg MySQL(%s) Privileges Start . . ." % install_cfg['--instance-ports']
    print LINE
    print INFO % mysql_privileges_start_message
    print LINE
    a=""" grep 'temporary password' %s/error.log |sed 's/.*root@localhost: //' """%(log_file_path)
    password_temp=commands.getstatusoutput(a)[1]
    #print password_temp
    password = 'openssl rand -base64 20'
    b = commands.getoutput(password)
    rootpwd = b
    scommand="""
/usr/sbin/lsof -i :%s &>/dev/null && %s/bin/mysql --connect-expired-password --socket=%s -u root -p"%s" -e "SET SQL_LOG_BIN=0;alter user 'root'@'localhost' identified by '%s';SET SQL_LOG_BIN=1;"
    """ % (install_cfg['--instance-ports'], install_cfg['--base-prefix'], mysql_sock,password_temp,rootpwd)
    print  scommand
    cfg_mysql_privileges=subprocess.call(scommand, shell=True)
    if cfg_mysql_privileges > 0:
        msg = "[Error]: Failed to set MySQL root password! Please do it manually."
        print LINE_ERROR
        print ERROR % cfg_mysql_privileges
        print ERROR % msg
        print LINE_ERROR
        sys.exit()
    # cfg_mysql_privileges = commands.getstatusoutput(
    #     "/usr/sbin/lsof -i :%s &>/dev/null && %s/bin/mysqladmin -u root password '%s' -S %s >/dev/null 2>&1" % (
    #         install_cfg['--instance-ports'], install_cfg['--base-prefix'], rootpwd, mysql_sock))
    # /usr/local/mysql/ bin / mysqladmin - uroot - p 'jSh#jXtkC6i8'  password  'root' - -socket = / data / db / mysql3306 / mysql3306.sock
    # if int(cfg_mysql_privileges[0]) != 0:
    #     print cfg_mysql_privileges[1]
    #     msg = "[Error]: Failed to set MySQL root password! Please do it manually."
    #     print LINE_ERROR
    #     print ERROR % cfg_mysql_privileges[1]
    #     print ERROR % msg
    #     print LINE_ERROR
    #     sys.exit()

    mysql_privileges_finish_message = "Cfg MySQL(%s) Privileges Finish" % install_cfg['--instance-ports']
    print LINE
    print INFO % mysql_privileges_finish_message
    print LINE

    check_mysql_start_message = "Check MySQL(%s) Connected to port Start" % install_cfg['--instance-ports']
    print LINE
    print INFO % check_mysql_start_message
    print LINE

    check_mysql = commands.getstatusoutput(
        '''%s/bin/mysql -uroot -p%s -S %s -Bse "SELECT concat(version(),' started in %s port')" ''' % (
            install_cfg['--base-prefix'], rootpwd, mysql_sock, install_cfg['--instance-ports']))
    if int(check_mysql[0]) != 0:
        msg = "[Error]: Failed to connect to port %s! You need to create MySQL monitoring user manually." % install_cfg[
            '--instance-ports']
        print LINE_ERROR
        print ERROR % check_mysql[1]
        print ERROR % msg
        print LINE_ERROR
        sys.exit()
    else:
        msg = "Connected to port %s successful." % install_cfg['--instance-ports']
        print LINE
        print NOTICE % msg
        print LINE

    check_mysql_finish_message = "Check MySQL(%s) Connected to port Finish" % install_cfg['--instance-ports']
    print LINE
    print INFO % check_mysql_finish_message
    print LINE

    create_user_start_message = "Creating MySQL Monitoring User Start..."
    print LINE
    print INFO % create_user_start_message
    print LINE

    create_mysql_monitor = commands.getstatusoutput(
        '''%s/bin/mysql -uroot -p%s -S %s -Bse "GRANT PROCESS,REPLICATION CLIENT ON *.* TO 'monitoring'@'127.0.0.1 identified by '8e3d7855e5681ee463e28394c2bb33043e65dbb9';FLUSH PRIVILEGES;" ''' % (
            install_cfg['--base-prefix'], rootpwd, mysql_sock))

    create_user_finish_message = "Creating MySQL Monitoring User Finish..."
    print LINE
    print INFO % create_user_finish_message
    print LINE

    install_rpl_start_message = "Install rpl_semi_sync on %s  Start.... " % install_cfg['--instance-ports']
    print LINE
    print INFO % install_rpl_start_message
    print LINE

    install_rpl_semi_sync = commands.getstatusoutput(
        '''%s/bin/mysql -uroot -p%s -S %s -e "INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_master.so';INSTALL PLUGIN rpl_semi_sync_slave SONAME 'semisync_slave.so';" ''' % (
            install_cfg['--base-prefix'], rootpwd, mysql_sock))
    if int(install_rpl_semi_sync[0]) != 0:
        print install_rpl_semi_sync[1]
        msg = "[Error]: Install rpl_semi_sync failed on %s" % install_cfg['--instance-ports']
        print LINE_ERROR
        print ERROR % install_rpl_semi_sync[1]
        print ERROR % msg
        print LINE_ERROR
        sys.exit()
    install_rpl_finish_message = "Install rpl_semi_sync on %s  Finish.... " % install_cfg['--instance-ports']
    print LINE
    print INFO % install_rpl_finish_message
    print LINE
    return "true"


if __name__ == '__main__':
    if os.geteuid() != 0:
        msg = "[Error]: This script must be run as root. Aborting."
        print LINE_ERROR
        print ERROR % msg
        print LINE_ERROR
        sys.exit()

    if len(sys.argv) < 3 or sys.argv[1] != "install":
        configure_help = CheckAndHelp(sys.argv[0], "install")
        print configure_help
        sys.exit()

    check_argv = sys.argv[2:]
    #print check_argv
    if "--instance-ports" not in str(check_argv) or "--innodb-buffer-pool-size" not in str(check_argv):
        print "python %s install --instance-ports=3366 --innodb-buffer-pool-size=1G"%sys.argv[0]
        sys.exit()

    #filename = 'mysql-8.0.20-linux-glibc2.12-x86_64.tar.xz'
    filename = 'mysql-8.0.20-linux-glibc2.12-x86_64.tar.xz'

    #filename2 = 'mysql-8.0.20-linux-glibc2.12-x86_64.tar'
    filename2 = 'mysql-8.0.20-linux-glibc2.12-x86_64.tar'
    if not os.path.exists(filename2):
        if not os.path.exists(filename):
            msg = "%s not exists!"%filename
            print LINE_ERROR
            print ERROR % msg
            #print "wget https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.20-linux-glibc2.12-x86_64.tar.xz"
            print "wget https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.20-linux-glibc2.12-x86_64.tar.xz"
            print LINE_ERROR
            sys.exit()
        else:
            commands.getstatusoutput("xz -d %s" % (filename))

    check_result = CheckArgv(check_argv)
    if check_result['result_state'] == "false":
        print check_result['result']
    else:
        #print check_result['result']
        create_result = CreateConfigurationFiles(check_result['result'])
        for port in create_result['--instance-ports'].split(','):
            create_result['--instance-ports'] = port
            create_file_result = CheckReplaceFile(create_result)
            #print create_file_result
            if create_file_result['result_state'] == "true":
                msg = "Configuration File %s Create Success" % create_file_result['result']
                print LINE
                print INFO % msg
                print LINE
                install_result = InstallMysql(create_result, filename2)
                #install_result = CfgMysql(create_result)
            if install_result == "true":
                commands.getstatusoutput('source /etc/profile')
                install_finish_message = "Install MySQL Finish Port: %s" % port
                print LINE
                print INFO % install_finish_message
                print LINE

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653

# my.cnf

#
## my.cnf for MySQL8.0+
## 本配置文件主要适用于MySQL 8.0+版本
# updated at 2020/03/16 by nicole
#
[client]
port	= $--instance-ports
socket	= $--data-prefix/mysql$--instance-ports/mysql$--instance-ports.sock

[mysql]
prompt="\u@\h[\d]> "
no-auto-rehash

[mysqld]
user                                   = $--mysql-user
port                                   = $--instance-ports
basedir                                = $--base-prefix
datadir	                               = $--data-prefix/mysql$--instance-ports/data
socket                                 = $--data-prefix/mysql$--instance-ports/mysql$--instance-ports.sock
pid_file                               = $--log-prefix/mysql$--instance-ports/mysql$--instance-ports.pid
mysqlx_port                            = 1$--instance-ports
mysqlx_socket                          = $--data-prefix/mysql$--instance-ports/mysqlx1$--instance-ports.sock


#--- GLOBAL ---#
server_id                              = $--server-id$--instance-ports
character-set-server                   = utf8mb4
collation-server                       = utf8mb4_unicode_ci
character-set-client-handshake         = FALSE
default_authentication_plugin          = 'mysql_native_password'
tmpdir                                 = $--data-prefix/mysql$--instance-ports/mysqltmp
log_timestamps                         = SYSTEM
log_error                              = $--log-prefix/mysql$--instance-ports/error.log
log_error_verbosity                    = 2
slow_query_log                         = 1
slow_query_log_file                    = $--log-prefix/mysql$--instance-ports/slowlogs/mysql_slow.log
long_query_time                        = 1
general_log_file                       = $--log-prefix/mysql$--instance-ports/mysql_gene.log
secure_file_priv                       = $--data-prefix/mysql$--instance-ports/filedir
local_infile                           = 0

#read_only                              = 1
#super_read_only                        = 1
activate_all_roles_on_login            = 1

innodb_print_ddl_logs                  = 1       # mysql8.0.3 新增
open_files_limit                       = 65535
thread_cache_size                      = 3000
table_open_cache                       = 1024
table_definition_cache                 = 1024
table_open_cache_instances             = 64
thread_stack                           = 512K
external-locking                       = FALSE
sort_buffer_size                       = 4M
join_buffer_size                       = 4M
thread_cache_size                      = 5000
tmp_table_size                         = 32M
max_heap_table_size                    = 32M
#slave_rows_search_algorithms          = 'INDEX_SCAN,HASH_SCAN'        ## 8.0.1版本后已弃用此参数,从8.0.1开始,此参数的默认值为HASH_SCAN和INDEX_SCAN
key_buffer_size                        = 32M
read_buffer_size                       = 8M
read_rnd_buffer_size                   = 4M
bulk_insert_buffer_size                = 64M
lock_wait_timeout                      = 3600
lower_case_table_names                 = 1
explicit_defaults_for_timestamp        = 1

#myisam
myisam_sort_buffer_size                = 128M
myisam_max_sort_file_size              = 10G
myisam_repair_threads                  = 1

#innodb
innodb_thread_concurrency              = 0
innodb_sync_spin_loops                 = 100
innodb_spin_wait_delay                 = 30
transaction_isolation                  = READ-COMMITTED
#innodb_additional_mem_pool_size       = 16M
innodb_buffer_pool_size                = $--innodb-buffer-pool-size
innodb_buffer_pool_load_at_startup     = 1
innodb_buffer_pool_dump_at_shutdown    = 1
innodb_data_file_path                  = ibdata1:1G:autoextend
innodb_log_buffer_size                 = 32M
innodb_log_file_size                   = 2G
innodb_log_files_in_group              = 2
innodb_max_undo_log_size               = 4G

#--- REPL ---#
log_bin                                = $--log-prefix/mysql$--instance-ports/binlogs/mysql_bin
binlog_rows_query_log_events           = ON
#for mysql8 binlog_expire_logs_seconds instead of binlog_expire_logs_days
#expire_logs_days                      = 30
binlog_expire_logs_seconds             = 2592000
binlog_checksum                        = 1
binlog_format                          = row
binlog_cache_size                      = 4M
max_binlog_cache_size                  = 2G
max_binlog_size                        = 1G
min_examined_row_limit                 = 0
log_slow_admin_statements              = 1
log_slow_slave_statements              = 1
log_queries_not_using_indexes          = 0
log_slave_updates
master_info_repository                 = TABLE
relay_log_info_repository              = TABLE
skip_slave_start
#--gtid
gtid_mode                              = ON
enforce_gtid_consistency               = 1

#--- semi sync replication settings ---#
##增强半同步复制
#plugin_dir                             = $--base-prefix/lib/plugin     #官方版本的路径
#plugin_load                            = "rpl_semi_sync_master=semisync_master.so;rpl_semi_sync_slave=semisync_slave.so"   #官方版本的路径
#rpl_semi_sync_master_enabled           = 1      #建议不要直接写入到my.cnf配置文件开启
#rpl_semi_sync_slave_enabled            = 1      #建议不要直接写入到my.cnf配置文件开启
#loose_rpl_semi_sync_master_timeout     = 3000


#--- flush disk ---#
innodb_flush_log_at_trx_commit         = 1
sync_binlog                            = 1
#--relay_log
relay_log                              = $--log-prefix/mysql$--instance-ports/relaylogs/mysql_relay_bin
relay_log_recovery                     = 1
relay_log_purge                        = 1


#--- NETWORK ---#
max_allowed_packet                     = 128M
back_log                               = 1024
interactive_timeout                    = 3600
wait_timeout                           = 3600
max_connections                        = 5000
skip_name_resolve                      = 1
max_connect_errors                     = 1000000

#--UNDO
#innodb_undo_directory                  = ./
#innodb_undo_logs                       = 128
#innodb_undo_log_truncate               = ON
# The setting INNODB_UNDO_TABLESPACES is deprecated and is no longer used.  InnoDB always creates 2 undo tablespaces to start with
#innodb_undo_tablespaces                = 3

#--并行复制
loose-binlog_transaction_dependency_tracking      = WriteSet_Session
#loose-binlog_transaction_dependency_tracking      = WriteSet
loose-binlog_transaction_dependency_history_size  = 1000000       # 可设置的最大值为1000000
loose-transaction_write_set_extraction            = XXHASH64
slave_parallel_type                               = LOGICAL_CLOCK
slave_parallel_workers                            = 8             # 根据服务器配置适当调整

# 根据您的服务器IOPS能力适当调整
# 一般配普通SSD盘的话,可以调整到 10000 - 20000
# 配置高端PCIe SSD卡的话,则可以调整的更高,比如 50000 - 80000
innodb_io_capacity = 4000
innodb_io_capacity_max = 8000
innodb_flush_sync = 0
innodb_flush_neighbors = 0
innodb_write_io_threads = 8
innodb_read_io_threads = 8
innodb_purge_threads = 4
innodb_page_cleaners = 4
innodb_open_files = 65535
innodb_max_dirty_pages_pct = 50
innodb_flush_method = O_DIRECT
innodb_lru_scan_depth = 4000
innodb_checksum_algorithm = crc32
innodb_lock_wait_timeout = 10
innodb_rollback_on_timeout = 1
innodb_print_all_deadlocks = 1
innodb_file_per_table = 1
innodb_online_alter_log_max_size = 4G
#for mysql 8 this variables ls unknown
##internal_tmp_disk_storage_engine = InnoDB
innodb_stats_on_metadata = 0

innodb_status_file = 1
# 注意: 开启 innodb_status_output & innodb_status_output_locks 后, 可能会导致log-error文件增长较快
innodb_status_output = 0
innodb_status_output_locks = 0

#performance_schema
performance_schema = 1
performance_schema_instrument = '%=on'

#innodb monitor
innodb_monitor_enable="module_innodb"
innodb_monitor_enable="module_server"
innodb_monitor_enable="module_dml"
innodb_monitor_enable="module_ddl"
innodb_monitor_enable="module_trx"
innodb_monitor_enable="module_os"
innodb_monitor_enable="module_purge"
innodb_monitor_enable="module_log"
innodb_monitor_enable="module_lock"
innodb_monitor_enable="module_buffer"
innodb_monitor_enable="module_index"
innodb_monitor_enable="module_ibuf_system"
innodb_monitor_enable="module_buffer_page"
innodb_monitor_enable="module_adaptive_hash"


log_bin_trust_function_creators = 1

##--- group replication settings ---
#plugin-load = "group_replication.so"
#transaction-write-set-extraction = XXHASH64
#report_host = 172.31.92.104
#binlog_checksum = NONE
#loose_slave_preserve_commit_order = on
#loose_group_replication = FORCE_PLUS_PERMANENT
#loose_group_replication_group_name = "f0d9e877-661b-487c-a955-7fae37a5c2bd"
#loose_group_replication_compression_threshold = 100000       # mysql8.0.11后默认值为1000000字节,1M
#loose_group_replication_flow_control_mode = 0
#loose_group_replication_single_primary_mode = 1
#loose_group_replication_transaction_size_limit = 331350016
#loose_group_replication_member_expel_timeout = 20
#loose_group_replication_unreachable_majority_timeout = 20
#loose_group_replication_start_on_boot = off
#loose_group_replication_local_address = '172.31.92.104:2$--instance-ports'
#loose_group_replication_group_seeds = '172.31.92.102:2$--instance-ports,172.31.92.103:2$--instance-ports,172.31.92.104:2$--instance-ports'
#loose_group_replication_ip_whitelist= '172.31.92.0/20'
#loose_group_replication_bootstrap_group = off

[mysqldump]
quick

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228

# mysql

#!/bin/sh
# Copyright Abandoned 1996 TCX DataKonsult AB & Monty Program KB & Detron HB
# This file is public domain and comes with NO WARRANTY of any kind

# MySQL daemon start/stop script.

# Usually this is put in /etc/init.d (at least on machines SYSV R4 based
# systems) and linked to /etc/rc3.d/S99mysql and /etc/rc0.d/K01mysql.
# When this is done the mysql server will be started when the machine is
# started and shut down when the systems goes down.

# Comments to support chkconfig on RedHat Linux
# chkconfig: 2345 64 36
# description: A very fast and reliable SQL database engine.

# Comments to support LSB init script conventions
### BEGIN INIT INFO
# Provides: mysql
# Required-Start: $local_fs $network $remote_fs
# Should-Start: ypbind nscd ldap ntpd xntpd
# Required-Stop: $local_fs $network $remote_fs
# Default-Start:  2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: start and stop MySQL
# Description: MySQL is a very fast and reliable SQL database engine.
### END INIT INFO

# If you install MySQL on some other places than /usr/local/mysql, then you
# have to do one of the following things for this script to work:
#
# - Run this script from within the MySQL installation directory
# - Create a /etc/my.cnf file with the following information:
#   [mysqld]
#   basedir=<path-to-mysql-installation-directory>
# - Add the above to any other configuration file (for example ~/.my.ini)
#   and copy my_print_defaults to /usr/bin
# - Add the path to the mysql-installation-directory to the basedir variable
#   below.
#
# If you want to affect other MySQL variables, you should make your changes
# in the /etc/my.cnf, ~/.my.cnf or other MySQL configuration files.

# If you change base dir, you must also change datadir. These may get
# overwritten by settings in the MySQL configuration files.

basedir=
datadir=

# Default value, in seconds, afterwhich the script should timeout waiting
# for server start.
# Value here is overriden by value in my.cnf.
# 0 means don't wait at all
# Negative numbers mean to wait indefinitely
service_startup_timeout=900

# Lock directory for RedHat / SuSE.
lockdir='/var/lock/subsys'
lock_file_path="$lockdir/mysql"

# The following variables are only set for letting mysql.server find things.

# Set some defaults
mysqld_pid_file_path=
if test -z "$basedir"
then
  basedir=/usr/local/mysql
  bindir=/usr/local/mysql/bin
  if test -z "$datadir"
  then
    datadir=/usr/local/mysql/data
  fi
  sbindir=/usr/local/mysql/bin
  libexecdir=/usr/local/mysql/bin
else
  bindir="$basedir/bin"
  if test -z "$datadir"
  then
    datadir="$basedir/data"
  fi
  sbindir="$basedir/sbin"
  libexecdir="$basedir/libexec"
fi

# datadir_set is used to determine if datadir was set (and so should be
# *not* set inside of the --basedir= handler.)
datadir_set=

#
# Use LSB init script functions for printing messages, if possible
#
lsb_functions="/lib/lsb/init-functions"
if test -f $lsb_functions ; then
  . $lsb_functions
else
  log_success_msg()
  {
    echo " SUCCESS! $@"
  }
  log_failure_msg()
  {
    echo " ERROR! $@"
  }
fi

PATH="/sbin:/usr/sbin:/bin:/usr/bin:$basedir/bin"
export PATH

mode=$1    # start or stop

[ $# -ge 1 ] && shift


other_args="$*"   # uncommon, but needed when called from an RPM upgrade action
           # Expected: "--skip-networking --skip-grant-tables"
           # They are not checked here, intentionally, as it is the resposibility
           # of the "spec" file author to give correct arguments only.

case `echo "testing\c"`,`echo -n testing` in
    *c*,-n*) echo_n=   echo_c=     ;;
    *c*,*)   echo_n=-n echo_c=     ;;
    *)       echo_n=   echo_c='\c' ;;
esac

parse_server_arguments() {
  for arg do
    case "$arg" in
      --basedir=*)  basedir=`echo "$arg" | sed -e 's/^[^=]*=//'`
                    bindir="$basedir/bin"
		    if test -z "$datadir_set"; then
		      datadir="$basedir/data"
		    fi
		    sbindir="$basedir/sbin"
		    libexecdir="$basedir/libexec"
        ;;
      --datadir=*)  datadir=`echo "$arg" | sed -e 's/^[^=]*=//'`
		    datadir_set=1
	;;
      --pid-file=*) mysqld_pid_file_path=`echo "$arg" | sed -e 's/^[^=]*=//'` ;;
      --service-startup-timeout=*) service_startup_timeout=`echo "$arg" | sed -e 's/^[^=]*=//'` ;;
    esac
  done
}

wait_for_pid () {
  verb="$1"           # created | removed
  pid="$2"            # process ID of the program operating on the pid-file
  pid_file_path="$3" # path to the PID file.

  i=0
  avoid_race_condition="by checking again"

  while test $i -ne $service_startup_timeout ; do

    case "$verb" in
      'created')
        # wait for a PID-file to pop into existence.
        test -s "$pid_file_path" && i='' && break
        ;;
      'removed')
        # wait for this PID-file to disappear
        test ! -s "$pid_file_path" && i='' && break
        ;;
      *)
        echo "wait_for_pid () usage: wait_for_pid created|removed pid pid_file_path"
        exit 1
        ;;
    esac

    # if server isn't running, then pid-file will never be updated
    if test -n "$pid"; then
      if kill -0 "$pid" 2>/dev/null; then
        :  # the server still runs
      else
        # The server may have exited between the last pid-file check and now.
        if test -n "$avoid_race_condition"; then
          avoid_race_condition=""
          continue  # Check again.
        fi

        # there's nothing that will affect the file.
        log_failure_msg "The server quit without updating PID file ($pid_file_path)."
        return 1  # not waiting any more.
      fi
    fi

    echo $echo_n ".$echo_c"
    i=`expr $i + 1`
    sleep 1

  done

  if test -z "$i" ; then
    log_success_msg
    return 0
  else
    log_failure_msg
    return 1
  fi
}

# Get arguments from the my.cnf file,
# the only group, which is read from now on is [mysqld]
if test -x "$bindir/my_print_defaults";  then
  print_defaults="$bindir/my_print_defaults"
else
  # Try to find basedir in /etc/my.cnf
  conf="{MYCNF-DIR}"
  print_defaults=
  if test -r $conf
  then
    subpat='^[^=]*basedir[^=]*=\(.*\)$'
    dirs=`sed -e "/$subpat/!d" -e 's//\1/' $conf`
    for d in $dirs
    do
      d=`echo $d | sed -e 's/[ 	]//g'`
      if test -x "$d/bin/my_print_defaults"
      then
        print_defaults="$d/bin/my_print_defaults"
        break
      fi
    done
  fi

  # Hope it's in the PATH ... but I doubt it
  test -z "$print_defaults" && print_defaults="my_print_defaults"
fi

#
# Read defaults file from 'basedir'.   If there is no defaults file there
# check if it's in the old (depricated) place (datadir) and read it from there
#

extra_args=""
if test -r "$basedir/my.cnf"
then
  extra_args="-e $basedir/my.cnf"
fi

parse_server_arguments `$print_defaults $extra_args mysqld server mysql_server mysql.server`

#
# Set pid file if not given
#
if test -z "$mysqld_pid_file_path"
then
  mysqld_pid_file_path=$datadir/`hostname`.pid
else
  case "$mysqld_pid_file_path" in
    /* ) ;;
    * )  mysqld_pid_file_path="$datadir/$mysqld_pid_file_path" ;;
  esac
fi

case "$mode" in
  'start')
    # Start daemon

    # Safeguard (relative paths, core dumps..)
    cd $basedir

    echo $echo_n "Starting MySQL"
    if test -x $bindir/mysqld_safe
    then
      # Give extra arguments to mysqld with the my.cnf file. This script
      # may be overwritten at next upgrade.
      #$bindir/mysqld_safe --datadir="$datadir" --pid-file="$mysqld_pid_file_path" $other_args >/dev/null &  #this is  mysql original  script for start
	  {BIN-DIR}/mysqld_safe --defaults-file="{MYCNF-DIR}" >/dev/null 2>&1 &
      #wait_for_pid created "$!" "$mysqld_pid_file_path"; return_value=$?  #this is  mysql  original script  for start
	  wait_for_pid created "$!" "{PID-DIR}"; return_value=$?

      # Make lock for RedHat / SuSE
      if test -w "$lockdir"
      then
        touch "$lock_file_path"
      fi

      exit $return_value
    else
      log_failure_msg "Couldn't find MySQL server ({BIN-DIR}/mysqld_safe)"
    fi
    ;;

  'stop')
    # Stop daemon. We use a signal here to avoid having to know the
    # root password.
    mysqld_pid_file_path={PID-DIR}
    if test -s "$mysqld_pid_file_path"
    then
      # signal mysqld_safe that it needs to stop
      touch "$mysqld_pid_file_path.shutdown"

      mysqld_pid=`cat "$mysqld_pid_file_path"`

      if (kill -0 $mysqld_pid 2>/dev/null)
      then
        echo $echo_n "Shutting down MySQL"
        kill $mysqld_pid
        # mysqld should remove the pid file when it exits, so wait for it.
        wait_for_pid removed "$mysqld_pid" "$mysqld_pid_file_path"; return_value=$?
      else
        log_failure_msg "MySQL server process #$mysqld_pid is not running!"
        rm "$mysqld_pid_file_path"
      fi

      # Delete lock for RedHat / SuSE
      if test -f "$lock_file_path"
      then
        rm -f "$lock_file_path"
      fi
      exit $return_value
    else
      log_failure_msg "MySQL server PID file could not be found!"
    fi
    ;;

  'restart')
    # Stop the service and regardless of whether it was
    # running or not, start it again.
    if $0 stop  $other_args; then
      $0 start $other_args
    else
      log_failure_msg "Failed to stop running server, so refusing to try to start."
      exit 1
    fi
    ;;

  'reload'|'force-reload')
    if test -s "$mysqld_pid_file_path" ; then
      read mysqld_pid <  "$mysqld_pid_file_path"
      kill -HUP $mysqld_pid && log_success_msg "Reloading service MySQL"
      touch "$mysqld_pid_file_path"
    else
      log_failure_msg "MySQL PID file could not be found!"
      exit 1
    fi
    ;;
  'status')
    # First, check to see if pid file exists
	mysqld_pid_file_path={PID-DIR}
    if test -s "$mysqld_pid_file_path" ; then
      read mysqld_pid < "$mysqld_pid_file_path"
      if kill -0 $mysqld_pid 2>/dev/null ; then
        log_success_msg "MySQL running ($mysqld_pid)"
        exit 0
      else
        log_failure_msg "MySQL is not running, but PID file exists"
        exit 1
      fi
    else
      # Try to find appropriate mysqld process
      mysqld_pid=`pidof $libexecdir/mysqld`

      # test if multiple pids exist
      pid_count=`echo $mysqld_pid | wc -w`
      if test $pid_count -gt 1 ; then
        log_failure_msg "Multiple MySQL running but PID file could not be found ($mysqld_pid)"
        exit 5
      elif test -z $mysqld_pid ; then
        if test -f "$lock_file_path" ; then
          log_failure_msg "MySQL is not running, but lock file ($lock_file_path) exists"
          exit 2
        fi
        log_failure_msg "MySQL is not running"
        exit 3
      else
        log_failure_msg "MySQL is running but PID file could not be found"
        exit 4
      fi
    fi
    ;;
    *)
      # usage
      basename=`basename "$0"`
      echo "Usage: $basename  {start|stop|restart|reload|force-reload|status}  [ MySQL server options ]"
      exit 1
    ;;
esac

exit 0

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380

# 使用方法

[root@localhost mysql_install]$ ls
install_mysql8.py  my.cnf  mysql  mysql-8.0.20-linux-glibc2.12-x86_64.tar
[root@localhost mysql_install]$ # python install_mysql8.py
Usage: python install_mysql8.py install [OPTION] [arg...]
Example:

            Simple: python mysql_install.py install --instance-ports=3366
            Multiple instances: python mysql_install.py install --instance-ports=3366,3399,4466


            If you know enough to the MySQL, you can use configure area:

            Simple:

                python install_mysql8.py install --instance-ports=3366 --innodb-buffer-pool-size=1G


install:
            custom  install:
                    --instance-ports            default:3306
                    --mysql-user                default:mysql
                    --base-prefix               default:/usr/local/mysql
                    --data-prefix               default:/data/db

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
上次更新: 4/24/2025

MySQL导出CSV格式解决乱码→

最近更新
01
tidb fast ddl
04-04
02
TiDB配置文件调优 原创
04-03
03
如何移除TiDB中的表分区 原创
04-03
更多文章>
Theme by Vdoing
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式