MySQL單機單實例安裝腳本(轉載)

說明:使用mysql generic tar.gz包快速安裝mysqlpython

三個文件installation_of_single_mysql.sh、template_install-my.cnf、mysql-5.7.18-linux-glibc2.5-x86_64.tar.gzmysql

修改腳本的配置信息指定template_install-my.cnf和mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz位置linux

默認root帳號是iforogtc++

  • installation_of_single_mysql.sh
#!/bin/bash # line: V1.8 # mail: gczheng@139.com # data: 2018-09-04 # script_name: installation_of_single_mysql.sh # function: Install mysql5.7.18 #======================================================================= #配置信息 #======================================================================= MYSQL_DATADIR=/data/mysqldata MYCNF=template_install-my.cnf MYSQL_SOURCE_PACKAGES=/software/mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz MYSQL_SOURCE_PACKAGES_NAMES=`echo $MYSQL_SOURCE_PACKAGES |awk -F '/' '{print $NF}' |awk -F ".tar.gz" '{printf $1}'` MYSQL_DOWNLOAD_LINK='http://mirrors.sohu.com/mysql/MySQL-5.7/mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz' MYSQL=/usr/local/mysql/bin/mysql #======================================================================= # echo添加顏色 #======================================================================= echo_color(){ color=${1} && shift case ${color} in black) echo -e "\e[0;30m${@}\e[0m" ;; red) echo -e "\e[0;31m${@}\e[0m" ;; green) echo -e "\e[0;32m${@}\e[0m" ;; yellow) echo -e "\e[0;33m${@}\e[0m" ;; blue) echo -e "\e[0;34m${@}\e[0m" ;; purple) echo -e "\e[0;35m${@}\e[0m" ;; cyan) echo -e "\e[0;36m${@}\e[0m" ;; *) echo -e "\e[0;37m${@}\e[0m" ;; esac # --- end of case --- } #======================================================================= #檢查安裝包、腳本、my.cnf是否齊全 #======================================================================= function chk_install_resource() { #判斷 template_install-my.cnf 是否存在 if [ ! -f "$MYCNF" ];then echo_color red "$(date +'%Y-%m-%d %H:%M:%S') $MYCNF file is not exits!$(echo_warning)" exit 1 fi #判斷 MySQL Community Server 5.7.18 tar包是否存在 if [ ! -f "$MYSQL_SOURCE_PACKAGES" ];then echo_color red "$(date +'%Y-%m-%d %H:%M:%S') $MYSQL_SOURCE_PACKAGES is not exits, please download it from $MYSQL_DOWNLOAD_LINK" exit 1 fi } #======================================================================= # 添加賬號和目錄 #======================================================================= function create_sys_user() { #添加mysql用戶信息 if id mysql &> /dev/null;then echo_color red "$(date +'%Y-%m-%d %H:%M:%S') MySQL user is exits." else useradd -r -s /bin/false mysql && echo_color cyan "$(date +'%Y-%m-%d %H:%M:%S') The system user is added to success .." fi if [ ! -d "${MYSQL_DATADIR}" ];then mkdir -p ${MYSQL_DATADIR} && echo_color cyan "$(date +'%Y-%m-%d %H:%M:%S') MySQL data directory is created .." chown -R mysql:mysql ${MYSQL_DATADIR} chmod 750 ${MYSQL_DATADIR} elif [ "$(ls -A ${MYSQL_DATADIR})" = "" ];then echo_color cyan "$(date +'%Y-%m-%d %H:%M:%S') MySQL data directory is exits." else echo_color red "$(date +'%Y-%m-%d %H:%M:%S') MySQL data directory is not empty. Please check it." exit 1 fi } #======================================================================= #檢查是否有舊的mysql/mariadb版本存在 #======================================================================= function chk_old_mysql_version() { mysqlNum=$(rpm -qa | grep -Ei 'mysql|mariadb'|wc -l) if [ "${mysqlNum}" -gt "0" ];then echo_color red "$(date +'%Y-%m-%d %H:%M:%S') The system has MySQL other version. There may be a conflict in the version!If it continues, the original database will be uninstall." read -p "Do you continue to install it(y/n):" cn case $cn in y|Y) rpm -qa | grep -Ei 'mysql|mariadb' | xargs yum remove -y &> /dev/null source /etc/profile tar_install ;; n|N) exit 1 ;; *) echo_color red "$(date +'%Y-%m-%d %H:%M:%S') Input ERROR." esac else echo_color cyan "$(date +'%Y-%m-%d %H:%M:%S') No old version was found." source /etc/profile tar_install fi } #======================================================================= # 解壓安裝 #======================================================================= function installPackage() { count=0 package=(gcc gcc-c++ bzip2 bzip2-devel bzip2-libs python-devel libaio libaio-devel ncurses ncurses-devel cmake numactl-libs) nums01=${#package[@]} for((i=0;i<nums01;i++)); do char=${package[$i]} rpm -qa | grep "^$char" if [ $? != 0 ] ; then error[$count]=${package[$i]} count=$(($count+1)) echo_color red "$(date +'%Y-%m-%d %H:%M:%S') The ${package[$i]} is not installed.Please check it.." fi done if [ $count -gt "0" ];then echo "You have $count patchs are not installed." echo "the not installed patch is:" nums02=${#error[@]} for((ii=0;ii<nums02;ii++)); do echo "${error[$ii]}^" done echo -e "Are you sure to install the patch[yes or no]:\c" read select if [ $select == "yes" ]; then for((is=0;is<nums02;is++)); do var=${error[$is]} echo $var yum install -y $var done fi else echo_color cyan "$(date +'%Y-%m-%d %H:%M:%S') Check pass!.." fi count=0 rpm -q gcc gcc-c++ bzip2 bzip2-devel bzip2-libs python-devel libaio libaio-devel ncurses ncurses-devel cmake numactl-libs | grep "not installed" } function tar_install() { installPackage echo_color cyan "$(date +'%Y-%m-%d %H:%M:%S') Starting unzip $MYSQL_SOURCE_PACKAGES .." tar zxvf $MYSQL_SOURCE_PACKAGES -C /usr/local/ echo_color cyan "$(date +'%Y-%m-%d %H:%M:%S') Unzip $MYSQL_SOURCE_PACKAGES SUCCESS .." if [ ! -d "/usr/local/mysql" ];then ln -s /usr/local/${MYSQL_SOURCE_PACKAGES_NAMES} /usr/local/mysql chown -R mysql:mysql /usr/local/mysql chown -R mysql:mysql /usr/local/${MYSQL_SOURCE_PACKAGES_NAMES} chmod 750 /usr/local/mysql chmod 750 /usr/local/${MYSQL_SOURCE_PACKAGES_NAMES} echo_color cyan "$(date +'%Y-%m-%d %H:%M:%S') MySQL package has been placed in the right position .." cp -f $MYCNF /etc/my.cnf else read -p "/usr/local/mysql install directory already exists, delete it, and continue(y/n):" dn case $dn in y|Y) rm -rf /usr/local/mysql ln -s /usr/local/${MYSQL_SOURCE_PACKAGES_NAMES} /usr/local/mysql chown -R mysql:mysql /usr/local/mysql chown -R mysql:mysql /usr/local/${MYSQL_SOURCE_PACKAGES_NAMES} chmod 750 /usr/local/mysql chmod 750 /usr/local/${MYSQL_SOURCE_PACKAGES_NAMES} echo_color cyan "$(date +'%Y-%m-%d %H:%M:%S') MySQL package has been placed in the right position .." cp -f $MYCNF /etc/my.cnf ;; n|N) exit 1 ;; *) echo_color red "$(date +'%Y-%m-%d %H:%M:%S') /usr/local/mysql is exits.Please check it." esac fi echo_color cyan "$(date +'%Y-%m-%d %H:%M:%S') Starting initialization .." /usr/local/mysql/bin/mysqld --initialize --user=mysql &> /dev/null && echo_color cyan "$(date +'%Y-%m-%d %H:%M:%S') Initialization ......SUCCESS" } #======================================================================= # 修改環境變量 #======================================================================= function add_system_profile() { cat >> /etc/profile <<EOF export PATH=\$PATH:/usr/local/mysql/bin/ EOF source /etc/profile } function add_mysql_ldconfig() { cat > /etc/ld.so.conf.d/mysql.conf <<EOF /usr/local/mysql/lib EOF ldconfig } function add_libmysqlclient() { if [ -f /etc/ld.so.conf.d/mysql.conf ];then LDNUMS=`grep -i "/usr/local/mysql/lib" /etc/ld.so.conf.d/mysql.conf |wc -l` if [ $LDNUMS -eq 0 ];then echo_color red "$(date +'%Y-%m-%d %H:%M:%S') The configuration file is empty!" add_mysql_ldconfig echo_color cyan "$(date +'%Y-%m-%d %H:%M:%S') Flush ldconfig done .." else LDEXISTS=`grep -i "/usr/local/mysql/lib" /etc/ld.so.conf.d/mysql.conf |grep -e "^#" |wc -l` if [ $LDEXISTS -gt 0 ];then add_mysql_ldconfig echo_color cyan "$(date +'%Y-%m-%d %H:%M:%S') Has been configured!" fi fi else echo_color red "$(date +'%Y-%m-%d %H:%M:%S') /etc/ld.so.conf.d/mysql.conf is not exits!" add_mysql_ldconfig echo_color cyan "$(date +'%Y-%m-%d %H:%M:%S') Flush ldconfig done .." fi } function modify_system_env() { #egrep "/usr/local/mysql/bin/" /etc/profile &> /dev/null PROFILES=`grep -i "/usr/local/mysql/bin/" /etc/profile |wc -l` if [ $PROFILES -eq 0 ];then add_system_profile echo_color cyan "$(date +'%Y-%m-%d %H:%M:%S') Flush profile done .." else EXISTS=`grep -i "/usr/local/mysql/bin/" /etc/profile |grep -e "^#" |wc -l` if [ $EXISTS -gt 0 ];then add_system_profile echo_color cyan "$(date +'%Y-%m-%d %H:%M:%S') Flush profile done .." fi fi } #======================================================================= #建立MySQL服務 #======================================================================= function el7_create_mysql_service() { cat > /usr/lib/systemd/system/mysql.service <<EOF [Unit] Description=mysql After=syslog.target network.target remote-fs.target nss-lookup.target [Service] Type=forking ExecStart=/usr/local/mysql/support-files/mysql.server start ExecReload=/usr/local/mysql/support-files/mysql.server restart ExecStop=/usr/local/mysql/support-files/mysql.server stop LimitNOFILE = 65535 PrivateTmp=false [Install] WantedBy=multi-user.target EOF systemctl daemon-reload && echo_color cyan "$(date +'%Y-%m-%d %H:%M:%S') Reload systemd services .." systemctl enable mysql.service && echo_color cyan "$(date +'%Y-%m-%d %H:%M:%S') Enable MySQL systemd service .." systemctl start mysql.service && echo_color cyan "$(date +'%Y-%m-%d %H:%M:%S') Starting MySQL......SUCCESS!" || echo_color red "$(date +'%Y-%m-%d %H:%M:%S') Starting MySQL......FAILED!." } function el6_create_mysql_service() { cd /usr/local/mysql/support-files/ cp mysql.server /etc/init.d/mysql chmod +x /etc/init.d/mysql chkconfig --add mysql && echo_color cyan "$(date +'%Y-%m-%d %H:%M:%S') Add MySQL service for management .." chkconfig --list mysql && echo_color cyan "$(date +'%Y-%m-%d %H:%M:%S') List MySQL service .." /etc/init.d/mysql start && echo_color cyan "$(date +'%Y-%m-%d %H:%M:%S') Starting MySQL......SUCCESS!" || echo_color red "$(date +'%Y-%m-%d %H:%M:%S') Starting MySQL......FAILED!." } #======================================================================= # 添加賬號 #======================================================================= function modify_mysql_account() { password=`awk '/A temporary password/ {print $NF}' ${MYSQL_DATADIR}/error.log` #echo_color cyan "mysql temp password is ${password}" if [ "${password}" != "" ];then ${MYSQL} -uroot -p"${password}" --connect-expired-password -e "alter user root@localhost identified by 'iforgot';flush privileges;" &> /dev/null && echo_color cyan "$(date +'%Y-%m-%d %H:%M:%S') 系統隨機密碼修改爲功." p1=$? else echo_color red "$(date +'%Y-%m-%d %H:%M:%S') MySQL密碼獲取失敗,請排查/清除數據目錄從新安裝." exit 1 fi ${MYSQL} -uroot -piforgot -e "grant all privileges on *.* to root@'%' identified by 'iforgot';" &> /dev/null && echo_color cyan "$(date +'%Y-%m-%d %H:%M:%S') 授予root用戶經過任意主機操做全部數據庫的全部權限成功." p2=$? ${MYSQL} -uroot -piforgot -e "grant RELOAD,REPLICATION SLAVE, REPLICATION CLIENT on *.* to repl@'%' identified by 'repl';" &> /dev/null && echo_color cyan "$(date +'%Y-%m-%d %H:%M:%S') 授予repl用戶經過任意主機對全部數據庫進行主從複製的權限成功." p3=$? ${MYSQL} -uroot -piforgot -e "grant SELECT, PROCESS, REPLICATION CLIENT, SHOW DATABASES on *.* to monitor@'%' identified by 'monitor';" &> /dev/null && echo_color cyan "$(date +'%Y-%m-%d %H:%M:%S') 授予monitor用戶經過任意主機對全部數據庫的讀取權限成功." p4=$? ${MYSQL} -uroot -piforgot -e "grant SELECT,RELOAD,LOCK TABLES,REPLICATION CLIENT,PROCESS,SUPER,CREATE,SHOW DATABASES,SHOW VIEW, EVENT, TRIGGER, create tablespace on *.* to dbbackup@'localhost' identified by 'dbbackup';" &> /dev/null && echo_color cyan "$(date +'%Y-%m-%d %H:%M:%S') 授予dbbackup用戶經過localhost主機對全部數據庫進行備份的權限成功." p5=$? ${MYSQL} -uroot -piforgot -e "grant insert,update,delete,select,create,drop,index,trigger,alter on *.* to producer@'%' identified by 'iforgot';" &> /dev/null && echo_color cyan "$(date +'%Y-%m-%d %H:%M:%S') 授予producer用戶經過任意主機對全部數據庫進行常規操做的權限成功." p6=$? ${MYSQL} -uroot -piforgot -e "grant insert,update,delete,select,create,drop,index,trigger,alter on *.* to producer@'localhost' identified by 'iforgot';" &> /dev/null && echo_color cyan "$(date +'%Y-%m-%d %H:%M:%S') 授予producer用戶經過localhost主機對全部數據庫進行常規操做的權限成功." p7=$? ${MYSQL} -uroot -piforgot -e "flush privileges" &> /dev/null && echo_color cyan "$(date +'%Y-%m-%d %H:%M:%S') 權限刷新成功." p8=$? if [[ "${p1}" == "0" && "${p2}" == "0" && "${p3}" == "0" && "${p4}" == "0" && "${p5}" == "0" && "${p6}" == "0" && "${p7}" == "0" && "${p8}" == "0" ]];then echo_color cyan "$(date +'%Y-%m-%d %H:%M:%S') 如今能夠登陸mysql數據庫,root和producer用戶的默認密碼是\033[41;37m iforgot\033[0m." ${MYSQL} -uroot -piforgot -e "select user,host,authentication_string from mysql.user;" else echo_color red "$(date +'%Y-%m-%d %H:%M:%S') 受權失敗,請手動執行受權操做." fi } #======================================================================= # 開始安裝mysql #======================================================================= function mysql_install() { version=$(uname -r |awk -F '.' '{ print $(NF-1) }') if [ "${version}" != "el7" ];then echo_color cyan "$(date +'%Y-%m-%d %H:%M:%S') Start install mysql for el6." chk_install_resource create_sys_user chk_old_mysql_version modify_system_env add_libmysqlclient el6_create_mysql_service sleep 5 modify_mysql_account else echo_color cyan "$(date +'%Y-%m-%d %H:%M:%S') Start install mysql for el7." chk_install_resource create_sys_user chk_old_mysql_version modify_system_env add_libmysqlclient el7_create_mysql_service sleep 5 modify_mysql_account fi } mysql_install echo_color blue "$(date +'%Y-%m-%d %H:%M:%S') \033[42;37m installation_of_single_mysql.sh執行完成 \033[0m" 
  • template_install-my.cnf
# line          :V1.8
# mail          :gczheng@139.com
# data          :2018-07-19
# file_name     :my.cnf
# update :調整innodb_open_files設置值,必須小於open_files_limit的設置值 #### 注意 :建議參數根據實際狀況做調整 #### 本配置文件主要適用於MySQL 5.7.18版本 # ********* 如下重要參數必須修改覈對 ********* # 1.innodb_flush_log_at_trx_commit=1 # 2.sync_binlog = 1 # 3.innodb_strict_mode = OFF #關閉InnoDB嚴格檢查模式 # 4.innodb_flush_method = O_DIRECT # 5.lower_case_table_names = 1 #設置區分大小寫,1表示不區分大小寫,0表示區分大小寫 # 6.character-set-server = utf8 # 7.sql_mode #默認配置 # 8.server-id =1 #修改爲對應數值 # 9.innodb_buffer_pool_size = 10G #純mysql server 配置50%和 混合內存配置不低於10G~40% #10.key_buffer_size=1G #若是有myisam表請配置爲1G,沒有請配置64M #11.innodb_data_file_path = ibdata1:1G:autoextend #確認配置是否跟原來同樣,以前已配置好請維持原樣,如未配置請註釋掉,新版本請取消註釋 #12.log_bin = /data/mysqldata/binlog #舊版本或者以前已配置好如:log_bin =/r2/data/mysqldata/slave-bin,請維持原樣 #13.slave-parallel #從庫開啓並行複製,並行複製參數取消註釋 #14.undolog #確認配置是否跟原來同樣,以前已配置好請維持原樣,如未配置請註釋掉,新版本(包括升級版本)請取消註釋並建立目錄並受權 # ******************************************** [client] port = 3306 socket = /data/mysqldata/mysql.sock #======================================================================= # # MySQL客戶端配置 #======================================================================= [mysql] prompt="(\u@\h) \\R:\\m:\\s [\d]> " no-auto-rehash default-character-set = utf8mb4 #======================================================================= # MySQL服務器全局配置 #======================================================================= [mysqld] user = mysql port = 3306 server-id = 1 tmpdir = /data/mysqldata datadir = /data/mysqldata socket = /data/mysqldata/mysql.sock wait_timeout = 31536000 #interactive_timeout = 600 #sql_mode = #sql_mode 配置爲空值 #skip_name_resolve = 1 lower_case_table_names = 1 character-set-server = utf8mb4 log_timestamps = SYSTEM init_connect='SET NAMES utf8mb4' max_allowed_packet = 128M ######################### 性能參數 #################### open_files_limit = 10240 max_connections = 10000 max_user_connections=9990 max_connect_errors = 100000 table_open_cache = 1024 thread_cache_size = 64 max_heap_table_size = 32M query_cache_type = 0 ###global cache ### key_buffer_size = 1G query_cache_size = 0 tmp_table_size = 32M #內存臨時表 binlog_cache_size = 4M #二進制日誌緩衝 ###session cache ### sort_buffer_size = 8M #排序緩衝 join_buffer_size = 4M #錶鏈接緩衝 read_buffer_size = 8M #順序讀緩衝 read_rnd_buffer_size = 8M #隨機讀緩衝 thread_stack = 256KB #線程的堆棧的大小 ######################### binlog設置 ##################### binlog_format = ROW log_bin = /data/mysqldata/binlog max_binlog_size = 1G expire_logs_days = 15 #binlog比較佔空間,注意磁盤空間 sync_binlog = 1 #重要參數必須修改成1 ######################### 複製設置 ######################## log_slave_updates = 1 #replicate-do-db = test #binlog-ignore-db = mysql ### GTID 配置 ### gtid_mode=ON enforce-gtid-consistency=true #****************** 開啓並行複製(從庫)****************** slave-parallel-type=LOGICAL_CLOCK #基於組提交的並行複製方式 slave-parallel-workers= 8 #並行的SQL線程數量 master-info_repository=TABLE #master信息以表的形式保存 relay_log_info_repository=TABLE #slave信息以表的形式保存 relay_log_recovery=ON #relay_log自我修復 ######################### innodb ########################## default_storage_engine = InnoDB innodb_data_file_path = ibdata1:1G:autoextend innodb_buffer_pool_size = 12G #系統內存50% innodb_open_files = 5120 innodb_flush_log_at_trx_commit = 1 #線上服務器必須配置爲1 innodb_file_per_table = 1 innodb_lock_wait_timeout = 5 innodb_io_capacity = 400 #根據您的服務器IOPS能力適當調整innodb_io_capacity,配SSD盤可調整到 10000 - 20000 innodb_io_capacity_max = 20000 innodb_flush_method = O_DIRECT innodb_log_file_size = 2G innodb_log_files_in_group = 2 innodb_large_prefix = 0 innodb_thread_concurrency = 64 innodb_strict_mode = OFF innodb_sort_buffer_size = 4194304 #****************** undolog設置 ****************** #innodb_undo_directory = /data/mysqldata/undolog #undolog日誌目錄 #innodb_undo_tablespaces = 2 #undolog日誌文件個數,mysql8以後將棄用 #innodb_undo_logs = 128 #回滾段的數量, 至少大於等於35,默認128。 #innodb_max_undo_log_size = 1G #當超過這個閥值(默認是1G),會觸發truncate回收(收縮)動做,truncate後空間縮小到10M。 #innodb_purge_rseg_truncate_frequency = 128 #控制回收(收縮)undolog的頻率 #innodb_undo_log_truncate = 1 #即開啓在線回收undolog日誌文件 ######################### log 設置 ##################### log_error = /data/mysqldata/error.log slow_query_log = 1 long_query_time = 10 slow_query_log_file = /data/mysqldata/slow.log #======================================================================= # MySQL mysqldump配置 #======================================================================= [mysqldump] quick max_allowed_packet = 128M #======================================================================= # MySQL mysqld_safe配置 #======================================================================= [mysqld_safe] log_error = /data/mysqldata/error.log pid_file = /data/mysqldata/mysqldb.pid
相關文章
相關標籤/搜索