mysql5.7 shell自动安装

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介:
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
cat  mysql.sh
#!/bin/sh
#auto install mysql5.7.10
   
#setting default directory
Port=$1
SoftDir= "/opt/install"
tarbag= "mysql-5.7.10-linux-glibc2.5-x86_64"
BaseDir= "/opt/mysql"
DataDir= "/opt/$Port"
 
mkdir  -p  /usr/local/mysql/data
 
#remove before being installed mysql
function  rmMysql() {
         yum -y erase mysql > /dev/null  2>&1
         yum -y erase mysql-server > /dev/null  2>&1
         ret=`rpm -aq |  grep  -i  "mysql-5"  wc  -l`
         num=`rpm -aq |  grep  -i  "mysql-server"  wc  -l`
         test  $ret - eq  1 &&  echo  "mysql uninstall failed"  &&  exit  1
         test  $num - eq  1 &&   echo  "mysql-server uninstall failed"  &&  exit  2
}
#libaio package is needed for mysql5.7.10
function  chkEnv() {
         yum -y  install  libaio > /dev/null  2>&1
         res=`rpm -aq| grep  libaio |  wc  -l`
         test  $res - ne  1 &&  echo  "libaio package install failed..."  &&  exit  3
}
   
#create mysql user and group, authorization, extract
function  preInstall() {
         /usr/sbin/groupadd  mysql
         /usr/sbin/useradd  -r -g mysql -s  /bin/false  mysql
         mkdir  -p $BaseDir
         mkdir  -p $DataDir /data
         chown  mysql.mysql $DataDir
         if  test  -f $SoftDir/$tarbag. tar .gz
           then
                 cd  $SoftDir &&  tar  -zxf $tarbag. tar .gz
                 cd  $SoftDir/$tarbag &&  cp  -r * $BaseDir
                 echo  ok
           else
                 echo  "$tarbag.tar.gz is not found..."
                 exit  10
         fi
}
   
function  multPreInstall() {
     mkdir  -p $DataDir /data
         chown  mysql.mysql $DataDir
}
   
function  install_mysql() {
         #initialize mysql database
         $BaseDir /bin/mysqld  \
         --initialize \
         --user=mysql \
         --basedir=$BaseDir \
         --datadir=$DataDir /data  \
         --character- set -server=utf8mb4 \
         --collation-server=utf8mb4_general_ci \
         --initialize-insecure > /dev/null  2>&1
}
#get my.cnf and start/stop script, attention alter parameters by your envionment
function  conf_mysql() {
         cp  $SoftDir /my .cnf $DataDir
         cp  $SoftDir /mysql .server $DataDir
/usr/bin/vim  $DataDir /my .cnf<<EOF > /dev/null  2>&1
:%s /3306/ $Port /g
:wq
EOF
         sed  -i  "s/port=3306/port=$Port/"  $DataDir /mysql .server
         sed  -i  "s%CmdPath=\"\"%CmdPath=\"${BaseDir}\/bin\"%"  $DataDir /mysql .server
         sed  -i  "s%mysql_sock=\"\"%mysql_sock=\"${DataDir}\/mysql.sock\"%"  $DataDir /mysql .server
         chmod  600 $DataDir /my .cnf
         chmod  700 $DataDir /mysql .server
         $DataDir /mysql .server start > /dev/null  2>&1
         sleep  60
#        ren=`netstat -natp|grep mysqld | grep "$1" | wc -l`
       
         if  test  -e $DataDir /mysql .sock; then
         echo  "$DataDir/mysql.sock"
                 echo  -e  "\033[33;1mmysql install success...\033[0m"
         pro=` grep  $BaseDir  /root/ .bash_profile |  wc  -l`
         if  test  "$pro"  - ne  1; then
            # sed -i "s%PATH=\$PATH\:\$HOME\/bin%PATH=\$PATH\:\$HOME\/bin\:$BaseDir\/bin%" /root/.bash_profile
             sed  -i  "s%PATH=\$PATH\:\$HOME\/bin%PATH=\$HOME\/bin\:$BaseDir\/bin%:\$PATH"  /root/ .bash_profile
             source  /root/ .bash_profile
                 fi
         else
                 echo  -e  "\033[31;1mmysql install failed...\033[0m"
         fi
}
   
if  [[  "$1"  =~ ^[0-9]+$ ]];  then
    inPort=` netstat  -natp |  grep  "mysqld"  grep  "LISTEN"  awk  '{print $4}'  cut  -b 4-`
    if  test  ! -z  "$inPort" ; then
         for  myPort  in  $inPort
           do
             if  test  "$myPort"  - eq  "$1" ; then
                 echo  -e  "\033[33;1m$1 instance has already existed...\033[0m"
                 exit  1
             fi
         done
        echo  -e  "\033[32;1m===========================================\033[0m"
        echo  -e  "\033[32;1mStarting create new instance $1\033[0m"
        multPreInstall
        install_mysql
        echo  -e  "\033[32;1m===========================================\033[0m"
        echo  -e  "\033[32;1mconfiguration and starting $1 instance...\033[0m"
        conf_mysql
    else
        echo  -e  "\033[32;1m===========================================\033[0m"
        echo  -e  "\033[32;1mremove before being installed mysql...\033[0m"
        rmMysql
        echo  -e  "\033[32;1m===========================================\033[0m"
        echo  -e  "\033[32;1minstall libaio package...\033[0m"
        chkEnv
        echo  -e  "\033[32;1m===========================================\033[0m"
        echo  -e  "\033[32;1mget ready install envionment...\033[0m"  
        preInstall
        echo  -e  "\033[32;1m===========================================\033[0m"
        echo  -e  "\033[32;1mStarting install mysql ver5.7.10...\033[0m"
        install_mysql
        echo  -e  "\033[32;1m===========================================\033[0m"
        echo  -e  "\033[32;1mconfiguration mysql and starting mysql...\033[0m"
        conf_mysql
    fi
else
    echo  "Usage: $0 Port (Port is inteager)"
fi
 
source  /root/ .bash_profile
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
cat  mysql.server
#!/bin/sh
  
# This is an interactive program, we needthe current locale 
[ -f  /etc/profile .d /lang .sh ] && .  /etc/profile .d /lang .sh 
# We can't Japanese on normal console atboot time, so force. 
if  "$LANG"  "ja"  -o  "$LANG"  "ja_JP.eucJP"  ];  then
    if  "$TERM"  "linux"  ] ;  then
        LANG=C 
    fi
fi
     
# Source function library. 
/etc/init .d /functions
  
#init
port=3306
mysql_user= "root"
mysql_pwd= ""
CmdPath= ""
mysql_sock= ""
  
#startup function
function_start_mysql()
{
     if  [ ! -e  "$mysql_sock"  ]; then
       printf  "Starting MySQL...\n"
       ${CmdPath} /mysqld_safe  --defaults- file = /opt/ ${port} /my .cnf --ledir=${CmdPath} 2>&1 >  /dev/null  &
       sleep  2
     else
       printf  "MySQL is running...\n"
       exit
     fi
}
  
#stop function
function_stop_mysql()
{
     if  [ ! -e  "$mysql_sock"  ]; then
        printf  "MySQL is stopped...\n"
        exit
     else
        printf  "Stoping MySQL...\n"
        ${CmdPath} /mysqladmin  -u ${mysql_user} -p${mysql_pwd} -S $mysql_sock  shutdown
        sleep  2
    fi
}
  
#restart function
function_restart_mysql()
{
     printf  "Restarting MySQL...\n"
     function_stop_mysql
     sleep  2
     function_start_mysql
}
  
case  $1  in
start)
     function_start_mysql
;;
stop)
     function_stop_mysql
;;
restart)
     function_restart_mysql
;;
status)
     status mysqld
;;
*)
     printf  "Usage: /opt/${port}/mysql {start|stop|restart|status}\n"
esac
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
cat  my.cnf
[client]
port = 3306
socket =  /opt/3306/mysql .sock
character- set -server = utf8mb4
default-character- set  = utf8mb4
   
[mysql]
no-auto-rehash
default-character- set  = utf8mb4
   
[mysqld]
user    = mysql
port    = 3306
character- set -client-handshake = FALSE
character- set -server = utf8mb4
collation-server = utf8mb4_unicode_ci
init_connect= 'SET NAMES utf8mb4'
#show_compatibility_56 = 1
socket  =  /opt/3306/mysql .sock
basedir =  /opt/mysql
datadir =  /opt/3306/data
explicit_defaults_for_timestamp =  true
skip-ssl
secure- file -priv = NULL
lower_case_table_names = 1
back_log = 300
max_connections = 3000
max_connect_errors = 100
table_open_cache = 4096
external-locking = FALSE
max_allowed_packet = 64M
sort_buffer_size = 16M
join_buffer_size = 16M
thread_cache_size = 16
query_cache_size = 128M
query_cache_limit = 4M
ft_min_word_len = 8
thread_stack = 512K
transaction_isolation = REPEATABLE-READ
tmp_table_size = 128M
max_heap_table_size = 128M
   
###*** slow query parameters
long_query_time = 6
slow_query_log
slow_query_log_file =  /opt/3306/slow .log
   
###*** binlog parameters
log-bin =  /opt/3306/mysql-bin
binlog_cache_size = 4M
max_binlog_cache_size = 256M
max_binlog_size = 32M
#binlog_format = MIXED
binlog_format = row
expire_logs_days = 7
   
###*** relay-log parameters
#relay-log = /opt/3307/relay-bin
#relay-log-info-file = /opt/3307/relay-log.info
#master-info-repository = table
#relay-log-info-repository = table
#relay-log-recovery = 1
   
#*** MyISAM parameters
key_buffer_size = 16M
read_buffer_size = 1M
read_rnd_buffer_size = 16M
bulk_insert_buffer_size = 1M
   
#skip-name-resolve
   
###*** master-slave replication parameters
server- id  = 105
slave-skip-errors = all
   
#*** Innodb storage engine parameters
innodb_buffer_pool_size = 2G
innodb_data_file_path = ibdata1:10M:autoextend
#innodb_file_io_threads = 8
innodb_thread_concurrency = 16
innodb_flush_log_at_trx_commit = 2
innodb_log_buffer_size = 16M
innodb_log_file_size = 512M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 90
innodb_lock_wait_timeout = 120
innodb_file_per_table = on
   
 
sql_mode= 'STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'
 
[mysqldump]
quick
max_allowed_packet = 32M
   
[myisamchk]
key_buffer = 16M
sort_buffer_size = 16M
read_buffer = 8M
write_buffer = 8M
   
[mysqld_safe]
open -files-limit = 8192
log-error= /opt/3306/mysql_3306 .err
pid- file = /opt/3306/mysqld .pid
1
MySQL版本 mysql-5.7.10-linux-glibc2.5-x86_64. tar .gz

使用这个脚本注意的地方

  1. /etc/my.cnf必须要移除掉



本文转自 liqius 51CTO博客,原文链接:http://blog.51cto.com/szgb17/1854369,如需转载请自行联系原作者
相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
11天前
|
关系型数据库 MySQL 数据库
mysql卸载、下载、安装(window版本)
mysql卸载、下载、安装(window版本)
|
1月前
|
关系型数据库 MySQL 数据库连接
关于MySQL-ODBC的zip包安装方法
关于MySQL-ODBC的zip包安装方法
|
1月前
|
关系型数据库 MySQL Python
python安装MySQL-python:EnvironmentError解决办法
python安装MySQL-python:EnvironmentError解决办法
31 1
|
1月前
|
SQL 关系型数据库 MySQL
【MySQL】— —熟练掌握用SQL语句实现数据库和基本表的创建。熟练掌握MySQL的安装、客户端登录方法;熟练掌握MySQL的编码、数据类型等基础知识;掌握实体完整性的定义和维护方法、掌握参照完整性
【MySQL】— —熟练掌握用SQL语句实现数据库和基本表的创建。熟练掌握MySQL的安装、客户端登录方法;熟练掌握MySQL的编码、数据类型等基础知识;掌握实体完整性的定义和维护方法、掌握参照完整性
99 1
|
29天前
|
关系型数据库 MySQL 数据库
rds安装数据库客户端工具
安装阿里云RDS的数据库客户端涉及在本地安装对应类型(如MySQL、PostgreSQL)的客户端工具。对于MySQL,可选择MySQL Command-Line Client或图形化工具如Navicat,安装后输入RDS实例的连接参数进行连接。对于PostgreSQL,可以使用`psql`命令行工具或图形化客户端如PgAdmin。首先从阿里云控制台获取连接信息,然后按照官方文档安装客户端,最后配置客户端连接以确保遵循安全指引。
82 1
|
24天前
|
Ubuntu 关系型数据库 MySQL
Ubuntu 中apt 安装MySQL数据库
Ubuntu 中apt 安装MySQL数据库
66 0
|
3天前
|
关系型数据库 MySQL Linux
Linux联网安装MySQL Server
Linux联网安装MySQL Server
13 0
|
3天前
|
关系型数据库 MySQL Linux
centos7安装mysql-带网盘安装包
centos7安装mysql-带网盘安装包
33 2
|
7天前
|
关系型数据库 MySQL 数据库
Docker安装MySQL
Docker安装MySQL
14 1
|
7天前
|
关系型数据库 MySQL 数据安全/隐私保护
MySQL 安装及连接
MySQL 安装及连接
25 0