mysql一键安装脚本(查看mysql的安装路径)

墨墨导读:良好的开端是成功的一半,从MySQL安装开始。

学习数据库技术,实际动手的第一步是安装自己的MySQL。MySQL方面也提供多样式的安装方式rpm ,tar ,源码包。当安装完投入使用之后,随着业务量,数据量的增加,往往会碰到很多意向不到的问题。如性能,安全,配置不合理等。对于最佳实践,都应该注意哪些。

1. MySQL 安装部署流程

mysql一键安装脚本(查看mysql的安装路径)

1. 操作系统

Selinux:建议关闭SELinux功能,通过MySQL本身进行安全控制Firewalld Iptable:防火墙肯定要设置 或则 关闭时区:对于系统的来说 时间是非常重要指标网络配置:高配置机器,网卡MTU提高,建议将私网网卡的MTU值增加到9000,同时启用私网交换机的Jumbo Frame属性。磁盘格式:在平均文件较小,并发较小的IO场景,ext4和xfs表现差不多,前者略微胜出。当文件较大,并发较大时,xfs比ext4性能更好,同时更稳定。实际使用上来说,一般数据库的文件系统推荐用xfs。xfs的恢复比较麻烦,,这方面ext4的fschk修复成功率较高,而且ext4的社区支持比较完备。盘调度算法:默认是使用的CFQ算法,对于数据库专用服务器,如果为机械磁盘,建议将磁盘调度算法调整为deadline模式,如果为固态硬盘,调整为noop模式,以提升I/O吞吐量和降低I/O响应时间。虚拟内存使用策略:vm.swappiness,以提高mysql对内存的使用效率资源限制:limits.conf的 nproc nofile内核参数:net.ipv4.tcp 相关的信号量:对应InnoDB: a long semaphore wait

2. MYSQL软件安装

下载版本:一定是官方下载,应用测试过兼容的版本安装依赖:mysql执行依赖包安装部署:建议tar.gz包

3. MYSQL软件安装

用户:安全考虑数据目录:便于管理 提升io性能权限赋予:赋予特定用户权限 执行权限my.cnf配置:按照硬件配置,合理的配置

4. MYSQL初始化

初始化:初始化系统数据密码:密码修改,不安全账号删除第三方工具:pt-toolkit,xtrabackup 等常用运维工具

2. MySQL 一键式部署脚本

基于5.7.32编写的自动安装部署脚本。操作系统方面只加了资源添加部分。

Vim MySQL_AutoSetup .sh

#!/bin/bash#####MySQL5.7.32数据库自动安装脚本# Version: 1.0# Author: kevinCUI# Date: 2020-12-31######mysql 安装包的绝对路径,去掉.tar.gztarGzPath=/opt/idc/tarGzFile=mysql-5.7.32-linux-glibc2.12-x86_64#mysql 安装路径installPath=/home/mysql/#my.cnf配置文件mysqlcnf=/home/mysql/my.cnf#mysql serverid需要设置唯一的id,比如 ip 3位数字mysqlServerid=1010101#mysql 密码(不可擅自修改)defaultPwd=123456#mysql 端口mysqlPort=3306#mysql数据目录data_default=${installPath}${mysqlPort}data_datadir=${data_default}/datadata_binlog=${data_default}/binlogdata_dbdata=${data_default}/dbdatadata_logs=${data_default}/logsdata_tmp=${data_default}/tmpdata_undo=${data_default}/undo# 校验是否为ROOT用户CheckRoot(){if [ $(id -u) != “0” ]; then echo “Error: You must be root to run this script, please use root to install” exit 1ficlear} #优化文件最大打开数DependFile(){if [ $( cat /etc/security/limits.conf | grep “mysql” | wc -l ) -lt 1 ] ;thencat >>/etc/security/limits.conf << EOF* soft nproc 65536* hard nproc 65536* soft nofile 65536* hard nofile 65536mysql soft nproc 65536mysql hard nproc 65536mysql soft nofile 65536mysql hard nofile 65536EOFfiif [ -e /etc/security/limits.d/20-nproc.conf ];thenif [ $( cat /etc/security/limits.d/20-nproc.conf | grep “mysql” | wc -l ) -lt 1 ] ;thencat >>/etc/security/limits.d/20-nproc.conf<<EOFmysql soft nproc unlimitedEOFfifiif [ -e /etc/security/limits.d/90-nproc.conf ];thenif [ $( cat /etc/security/limits.d/90-nproc.conf | grep “mysql” | wc -l ) -lt 1 ] ;thencat >>/etc/security/limits.d/90-nproc.conf<<EOFmysql soft nproc unlimitedEOFfifiif [ -e /etc/sysctl.conf ];thenfs_file=$( cat /proc/sys/fs/file-max)if [ ${fs_file} -lt 65535 ] ;thensed -i “s/${fs_file}/65535/g” /etc/sysctl.conf/usr/sbin/sysctl -p fifiecho -e “\e[31m #1.配置基础资源 \e[0m”}#拷贝tar.gz包DecompressionTarGz(){if [ ! -e ${tarGzPath}${tarGzFile}.tar.gz ];then echo -e “\e[31m ${tarGzPath}${tarGzFile}.tar.gz 不存在!请检查后重新执行脚本 \e[0m” exit 1fi#解压并重命名到安装目录if [ ! -d ${installPath}${tarGzFile} ] ;then mkdir -p ${installPath} tar -xvf ${tarGzPath}${tarGzFile}.tar.gz -C ${installPath} &> /dev/nullfiecho -e “\e[31m #2.软件已解压 \e[0m”}#添加组合角色AddMysqlUser(){if [ ! $(id -u “mysql”) ]; then echo “mysql user is not exists for to created” /usr/sbin/groupadd mysql /usr/sbin/useradd -g mysql -r -s /sbin/nologin -M mysqlfiecho -e “\e[31m #3.mysql启动用户已准备完成 \e[0m”}#创建mysql 数据目录createMysqlFolder(){if [ -d ${data_default} ] ;then if [ $(du -s ${data_default} | awk ‘NR==1{print $1}’) -gt 0 ] ;then mv ${data_default} ${data_default}”`date %Y%m%d%H%M`” fifimkdir -p ${data_datadir}mkdir -p ${data_binlog}mkdir -p ${data_dbdata}mkdir -p ${data_logs}mkdir -p ${data_tmp}mkdir -p ${data_undo}#赋予权限chown -R mysql:mysql ${data_default}chmod 700 ${data_tmp}echo -e “\e[31m #4.mysql 数据目录 权限 已准备完成 \e[0m”}#创建my.cnfMakeMyCnf(){if [ -e ${mysqlcnf} ] ;then #mv ${mysqlcnf} ${mysqlcnf}”`date %Y%m%d%H%M`” rm ${mysqlcnf}ficat >${mysqlcnf}<<EOF[mysqld_safe]user = mysqlnice = 0[client] socket = ${data_datadir}/mysql.sockport = ${mysqlPort}[mysqld]############# GENERAL #############skip_sslskip-name-resolveautocommit = ONcharacter_set_server = utf8mb4collation_server = utf8mb4_unicode_ciexplicit_defaults_for_timestamp = ON lower_case_table_names = 1port = ${mysqlPort}read_only = OFFtransaction_isolation = READ-COMMITTEDopen_files_limit = 65535max_connections = 2000expire_logs_days = 10default-time_zone = ‘ 8:00’####### CACHES AND LIMITS #########interactive_timeout = 600 lock_wait_timeout = 300max_connect_errors = 1000000table_definition_cache = 2000table_open_cache = 2000 table_open_cache_instances = 8thread_cache_size = 32thread_stack = 256Ktmp_table_size = 32Mmax_heap_table_size = 64Mquery_cache_size = 0query_cache_type = 0sort_buffer_size = 1Mjoin_buffer_size = 1Msort_buffer_size = 1Mread_rnd_buffer_size = 2Minnodb_io_capacity = 1000 innodb_io_capacity_max = 2000max_allowed_packet = 1024Mslave_max_allowed_packet = 1024Mslave_pending_jobs_size_max = 1024M############# SAFETY ##############local_infile = OFFskip_name_resolve = ONsql_mode = STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ENGINE_SUBSTITUTION,STRICT_ALL_TABLES############# LOGGING #############general_log = 0log_queries_not_using_indexes = ONlog_slow_admin_statements = ONlog_warnings = 2long_query_time = 1 #1秒慢日志slow_query_log = ON############# REPLICATION #############server_id = ${mysqlServerid} #ip 3位数字binlog_checksum = CRC32binlog_format = ROWbinlog_rows_query_log_events = ONenforce_gtid_consistency = ONgtid_mode = ONlog_slave_updates = ONmaster_info_repository = TABLEmaster_verify_checksum = ONmax_binlog_size = 512Mmax_binlog_cache_size = 1024M #已修改,原值1024binlog_cache_size = 8Mrelay_log_info_repository = TABLEskip_slave_start = ONslave_net_timeout = 10slave_sql_verify_checksum = ONsync_binlog = 1sync_master_info = 1sync_relay_log = 1sync_relay_log_info = 1############### PATH ##############basedir = ${installPath}${tarGzFile}datadir = ${data_datadir}tmpdir = ${data_tmp}socket = ${data_datadir}/mysql.sockpid_file = ${data_datadir}/mysql.pidinnodb_data_home_dir = ${data_dbdata}log_error = ${data_logs}/error.loggeneral_log_file = ${data_logs}/general.logslow_query_log_file = ${data_logs}/slow.loglog_bin = ${data_binlog}/mysql-binlog_bin_index = ${data_binlog}/mysql-bin.indexrelay_log = ${data_binlog}/relay-logrelay_log_index = ${data_binlog}/relay-log.index# undo settingsinnodb_undo_directory = ${data_undo}innodb_undo_log_truncate = 1 innodb_max_undo_log_size = 16Minnodb_undo_tablespaces = 4############# INNODB #############innodb_file_format = barracudainnodb_flush_method = O_DIRECTinnodb_buffer_pool_size = 1024Minnodb_buffer_pool_instances = 4 innodb_thread_concurrency = 0innodb_log_file_size = 1024Minnodb_log_files_in_group = 2innodb_flush_log_at_trx_commit = 1innodb_support_xa = ONinnodb_strict_mode = ONinnodb_data_file_path = ibdata1:32M;ibdata2:16M:autoextendinnodb_temp_data_file_path = ibtmp1:1G:autoextend:max:30Ginnodb_checksum_algorithm = strict_crc32innodb_lock_wait_timeout = 600innodb_log_buffer_size = 8Minnodb_open_files = 65535innodb_page_cleaners = 1innodb_lru_scan_depth = 256innodb_purge_threads = 4innodb_read_io_threads = 4innodb_write_io_threads = 4 innodb_print_all_deadlocks = 1[mysql]############# CLIENT ############# max_allowed_packet = 16Msocket = ${data_datadir}/mysql.sockno-auto-rehash[mysqldump] max_allowed_packet=16MEOFecho -e “\e[31m #5.mysql cnf配置完成,【需要按照实际情况更改】 \e[0m”}#初始化数据库InitDataBase(){#cd ${installPath}${tarGzFile}${installPath}${tarGzFile}/bin/mysqld –defaults-file=${mysqlcnf} –basedir=${installPath}${tarGzFile} –datadir=${data_datadir} –user=mysql –initialize${installPath}${tarGzFile}/bin/mysqld_safe –defaults-file=${mysqlcnf} –user=mysql &echo -e “\e[31m #6. 初始化数据库完成并启动服务. \e[0m”}#重置密码ResetPwd(){sleep 10s#从日志中获取mysql初始密码pwd=`grep “A temporary password is generated for root@localhost: ” ${data_logs}/error.log`pwd=${pwd##*root@localhost:}#防止因为初始密码中有特殊字符出错 拼接单引号pwd=${pwd// /}echo ${pwd}${installPath}${tarGzFile}/bin/mysql -uroot -p${pwd} -S ${data_datadir}/mysql.sock –connect-expired-password -e “alter user ‘root’@’localhost’ identified by ‘${defaultPwd}’;”echo -e “\e[31m #7. 已重置数据库密码。登录方式如下: \e[0m”echo -e “\e[31m ${installPath}${tarGzFile}/bin/mysql -uroot -p -S ${data_datadir}/mysql.sock \e[0m”}#ResetPwdmain() { ###1.校验是否为ROOT用户CheckRoot ###2.优化文件最大打开数DependFile###3.拷贝tar.gz包DecompressionTarGz###4.添加组合角色AddMysqlUser###5.创建mysql 数据目录createMysqlFolder###6.创建my.cnfMakeMyCnf###7.初始化数据库InitDataBase###8.重置密码ResetPwd} main

3.总结

良好的开端是成功的一半,MySQL是轻量级数据,安装部署也需要学问,粗略的安装往往会导致后期的一些各种大小问题。考虑的越周全,走的越远。

发表评论

登录后才能评论