0. 概述

最近鼓捣出了一个mysql安装脚本,将该脚本,mysql的my.cnf文件,mysql的安装包这三个文件放在同一个目录下面,执行sh mysql-auto-install.sh就可以完成mysql的一键安装,是不是很方便呢。

1. 准备mysql的安装包

mysql的安装包下载地址:https://dev.mysql.com/downloads/mysql/
注意需要下载的是linux GA版本 64bit,图中我用红色圈出来的部分

2. 准备mysql-auto-install.sh

#!/bin/sh
# author: yang bao
# time: --
# note: this script is used to install mysql on a new machine.
# . at first, you should prepare mysql install binary package like 'mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz'
# and mysql-auto-install.sh and my.cnf under same directory such as /root.
# . then exec 'sh mysql-auto-install.sh' and see the output. # prepare mysql related file
echo "$(date +"%Y%m%d %H:%M:%S") start to check mysql related file...."
# check if there is mysql install binary package or not
cnt1=`find . -maxdepth -name 'mysql-*-linux-glibc2.12-x86_64.tar.gz' -type f | wc -l`
if [ $cnt1 -lt ]; then
echo "It seems there isn't mysql install binary package in current directory!"
exit
elif [ $cnt1 -gt ]; then
echo "It seems there are too many mysql install binary packages in current directory,\
please just keep one, rename or move the others!"
exit
fi # check if there is my.cnf in current directory or not
cnt2=`find . -maxdepth -name 'my.cnf' -type f | wc -l`
if [ $cnt2 -lt ]; then
echo "It seems there isn't my.cnf in current directory!"
exit
fi # check if there is my.cnf in /etc or not
cnt3=`find /etc -maxdepth -name 'my.cnf' -type f | wc -l`
if [ $cnt3 -eq ]; then
echo "It seems there is my.cnf in /etc already, please delete it first!"
exit
fi # check if there is /opt/mydata in system or not
cnt4=`find / -maxdepth -name 'opt' -type d | wc -l`
if [ $cnt4 -eq ]; then
cnt5=`find /opt -maxdepth -name 'mydata' -type d | wc -l`
if [ $cnt5 -eq ]; then
echo "It seems there is /opt/mydata already, please delete it first!"
exit
fi
fi
echo "$(date +"%Y%m%d %H:%M:%S") mysql related file is ok...." # check mysql user
id mysql &> /dev/null
if [ $? -eq ]; then
echo "mysql user is alreay exist, please delete it first!"
exit
fi # prepare install mysql
echo "$(date +"%Y%m%d %H:%M:%S") start prepare install mysql...."
# clear old version
rpm -qa | grep -i mysql | grep -v libs | xargs rpm -ev --nodeps &> /dev/null # install required package
yum install -y libaio &> /dev/null # if the package is not install correctly, terminate the script.
cnt6=`rpm -qa | grep libaio | wc -l`
if [ $cnt6 -lt ]; then
echo "libaio package is not install, please check!"
exit
fi # adjust some parameter in /etc/security/limits.conf
echo "mysql soft nproc 16384" >> /etc/security/limits.conf
echo "mysql hard nproc 16384" >> /etc/security/limits.conf
echo "mysql soft nofile 65536" >> /etc/security/limits.conf
echo "mysql hard nofile 65536" >> /etc/security/limits.conf
echo "mysql soft stack 1024000" >> /etc/security/limits.conf
echo "mysql hard stack 1024000" >> /etc/security/limits.conf # adjust some parameter in /etc/sysctl.conf
echo "vm.swappiness = 5" >> /etc/sysctl.conf
sysctl -p &> /etc/null # turn off firewall
/etc/init.d/iptables stop &> /etc/null
chkconfig iptables off
sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config # move my.cnf to /etc
mv my.cnf /etc/ # add user mysql
groupadd mysql
useradd -r -g mysql -s /bin/false mysql # prepare directory
mkdir -p /opt/mydata/data
mkdir -p /opt/mydata/log/binlog
mkdir -p /opt/mydata/log/redo
mkdir -p /opt/mydata/log/undo
mkdir -p /opt/mydata/log/relaybin
mkdir -p /opt/mydata/tmp
chown -R mysql:mysql /opt/mydata # add path to profile
echo 'export PATH=$PATH:/usr/local/mysql/bin' >> /root/.bash_profile # unpackage mysql
rm -rf /usr/local/mysql-*-linux-glibc2.-x86_64 mysql
tar -zxvf mysql-*-linux-glibc2.-x86_64.tar.gz -C /usr/local/ &> /etc/null
cd /usr/local/
ln -s mysql-*-linux-glibc2.-x86_64 mysql echo "$(date +"%Y%m%d %H:%M:%S") prepare install mysql is ok...." # start install mysql
echo "$(date +"%Y%m%d %H:%M:%S") start install mysql...."
cd mysql
bin/mysqld --initialize --user=mysql
if [ $? -ne ]; then
echo "mysql initialize failed, please check the error log!"
exit
fi
bin/mysql_ssl_rsa_setup &> /dev/null
chmod +r /opt/mydata/data/server-key.pem
nohup bin/mysqld_safe --user=mysql &
if [ $? -ne ]; then
echo "mysql start failed, please check the error log!"
exit
fi
cp support-files/mysql.server /etc/init.d/mysql # wait mysql startup
cnt7=`ps -ef | grep mysqld | grep -v grep | wc -l`
while [ $cnt7 -lt ]
do
sleep
cnt7=`ps -ef | grep mysqld | grep -v grep | wc -l`
done # wait 10s for mysql startup completly and then change root password
sleep
pass=`grep "temporary password" /opt/mydata/log/error.log |awk -F " " '{print $11}'`
/usr/local/mysql/bin/mysqladmin -uroot -p$pass password 'root' echo "mysql db user root initial password is 'root'"
echo "$(date +"%Y%m%d %H:%M:%S") install mysql complete...."
exit

3. 准备my.cnf文件

由于我这个是测试环境,所以内存值调的比较小,生产上面可以将下面几个个参数进行调整

innodb_buffer_pool_size  # buffer pool大小,一般设置成物理内存 * 60%

innodb_max_undo_log_size  # undo表空间大小

innodb_log_file_size  # redo日志大小

[client]
port =
socket = /opt/mydata/data/mysql.sock [mysql]
prompt="(\\u@\\h)[\\d]> " [mysqld]
# basic settings #
server-id =
user = mysql
port =
basedir = /usr/local/mysql
datadir = /opt/mydata/data
tmpdir = /opt/mydata/tmp
pid-file = /opt/mydata/data/mysql.pid
socket = /opt/mydata/data/mysql.sock
character_set_server = utf8mb4
transaction_isolation = READ-COMMITTED
explicit_defaults_for_timestamp =
max_allowed_packet = 1024M
lower_case_table_names =
secure_file_priv = ''
open_files_limit = # connection #
skip_name_resolve =
max_connections =
max_user_connections =
max_connect_errors =
thread_cache_size = # memory && myisam #
max_heap_table_size = 128M
tmp_table_size = 128M
join_buffer_size = 16M
key_buffer_size = 64M
bulk_insert_buffer_size = 16M
myisam_sort_buffer_size = 64M
myisam_max_sort_file_size = 6G
myisam_recover_options = DEFAULT # log settings #
log_error = /opt/mydata/log/error.log
log_timestamps = SYSTEM
slow_query_log_file = /opt/mydata/log/slowquery.log
slow_query_log =
long_query_time =
log_queries_not_using_indexes =
log_throttle_queries_not_using_indexes =
min_examined_row_limit =
log_slow_admin_statements =
log-bin = /opt/mydata/log/binlog/mysql-bin
binlog_format = row
expire_logs_days =
binlog_rows_query_log_events =
binlog_row_image = minimal
binlog_cache_size = 8M
max_binlog_cache_size = 4G
max_binlog_size = 2G
log_bin_trust_function_creators = # innodb settings #
innodb_data_file_path = ibdata1:1024M:autoextend
innodb_buffer_pool_size = 300M
innodb_lock_wait_timeout =
innodb_io_capacity =
innodb_io_capacity_max =
innodb_flush_method = O_DIRECT
innodb_flush_neighbors =
innodb_undo_directory = /opt/mydata/log/undo
innodb_undo_tablespaces =
innodb_undo_log_truncate =
innodb_max_undo_log_size = 2G
innodb_log_group_home_dir = /opt/mydata/log/redo
innodb_log_file_size = 1G
innodb_log_files_in_group =
innodb_log_buffer_size = 32M
innodb_thread_concurrency =
innodb_print_all_deadlocks =
innodb_sort_buffer_size = 16M
innodb_write_io_threads =
innodb_read_io_threads =
innodb_rollback_on_timeout =
innodb_file_per_table =
innodb_open_files =
innodb_stats_persistent_sample_pages =
innodb_autoinc_lock_mode = # Slave #
relay-log = /opt/mydata/log/relaybin/slave-relay-bin
log_slave_updates =
relay_log_purge =
relay_log_space_limit = 30G
relay_log_recovery =
relay_log_info_repository = TABLE
skip-slave-start [mysqldump]
default_character_set = utf8mb4

4. 执行脚本,并查看输出

[root@mysqltest ~]# pwd
/root
[root@mysqltest ~]# ll
total
-rw-r--r--. root root Apr : my.cnf
-rw-r--r--. root root Mar : mysql-5.7.-linux-glibc2.-x86_64.tar.gz
-rw-r--r--. root root Apr : mysql-auto-install.sh
[root@mysqltest ~]# sh mysql-auto-install.sh
:: start to check mysql related file....
:: mysql related file is ok....
:: start prepare install mysql....
:: prepare install mysql is ok....
:: start install mysql....
nohup: appending output to `nohup.out'
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.
mysql db user root initial password is 'root'
:: install mysql complete....

5. 新开一个窗口登陆mysql

[root@mysqltest ~]# mysql -uroot -proot
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is
Server version: 5.7.-log MySQL Community Server (GPL) Copyright (c) , , Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. (root@localhost)[(none)]>

登陆成功,表示mysql安装以及启动,密码修改都已经成功。

6. 总结

在写这个脚本的时候,开始都很顺利,到后面执行mysqladmin修改root密码的时候老报错
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
mysqladmin: connect to server at 'localhost' failed
error: 'Can't connect to local MySQL server through socket '/opt/mydata/data/mysql.sock' (2)'
Check that mysqld is running and that the socket: '/opt/mydata/data/mysql.sock' exists!
询问了博客园大神ivictor[https://www.cnblogs.com/ivictor/]后,是因为我的mysql还没有起来,所以会报以上错误。因此我在脚本里面添加了sleep,等一段时间再修改密码。十分感谢ivictor的帮助。

mysql5.7一键安装脚本的更多相关文章

  1. LAMP最新源码一键安装脚本

    Linux+Apache+MySQL+PHP (脚本可以选择是否安装+Pureftpd+User manager for PureFTPd+phpMyAdmin+memcache),添加虚拟主机请执行 ...

  2. LNMP、LAMP、LANMP一键安装脚本(定期更新)[转]

    这个脚本是使用shell编写,为了快速在生产环境上部署LNMP/LAMP/LANMP(Linux.Nginx/Tengine.MySQL/MariaDB/Percona.PHP),适用于CentOS/ ...

  3. centos shell编程5 LANMP一键安装脚本 lamp sed lnmp 变量和字符串比较不能用-eq cat > /usr/local/apache2/htdocs/index.php <<EOF重定向 shell的变量和函数命名不能有横杠 平台可以用arch命令,获取是i686还是x86_64 curl 下载 第三十九节课

    centos shell编程5  LANMP一键安装脚本 lamp  sed  lnmp  变量和字符串比较不能用-eq  cat > /usr/local/apache2/htdocs/ind ...

  4. MySQL For Linux(CentOS/Ubuntu/Debian/Fedora/Arch)一键安装脚本(5.1-8.0)

    简介 很多童鞋不懂这么在Linux系统安装MySQL,网上大多数教程较复杂,不太适合小白安装,本教程提供一键安装脚本供大家使用,教大家怎么在Linux操作系统( 支持CentOS/Ubuntu/Deb ...

  5. CentOS 6、7下pptp vpn一键安装脚本

    之前有折腾过<CentOS 6.7下IPSEC/L2TP VPN一键安装脚本>,不稳定.不支持IOS,因此换成pptp,并已经添加到<lnmp一键安装包>.这个脚本可以单独使用 ...

  6. SSR服务端一键安装脚本

    支持新协议混淆,SSR服务端一键安装脚本   Shadowsocks-R 是项目 shadowsocks 的增强版,用于方便地产生各种协议接口.实现为在原来的协议外套一层编码和解码接口,不但可以伪装成 ...

  7. jdk1.8一键安装脚本(linux环境)

    1.下载jdk安装包和安装脚本 下载地址:https://pan.baidu.com/s/1bo6ADQ3 其中包括: jdk安装包:jdk-8u151-linux-x64.tar.gz jdk一键安 ...

  8. Centos7搭建pptp一键安装脚本

    废话不多说,先上脚本地址:Centos7一键pptp 使用: wget http://files.cnblogs.com/files/wangbin/CentOS7-pptp-host1plus.sh ...

  9. L2TP/IPSec一键安装脚本

    本脚本适用环境:系统支持:CentOS6+,Debian7+,Ubuntu12+内存要求:≥128M更新日期:2017 年 05 月 28 日 关于本脚本:名词解释如下L2TP(Layer 2 Tun ...

随机推荐

  1. SpringBoot修改Servlet相关配置

    第一种方式在配置文件中进行修改 server.port=8081 server.servlet.context-path=/springboot server.tomcat.uri-encoding= ...

  2. 算法时间复杂度和NP问题简介

    这里主要简单说一下算法的时间复杂度和NP问题简介,毕竟分析算法的时间复杂度上界有助于分析算法的好坏,分析算法好坏也有助于分析是否还有更好的算法: 一.时间复杂度: 一般关心的还有递归问题中的时间复杂度 ...

  3. ARMV8 datasheet学习笔记4:AArch64系统级体系结构之VMSA

    1. 前言 2. VMSA概述 2.1 ARMv8 VMSA naming VMSAv8 整个转换机中,地址转换有一个或两个stage VMSAv8-32 由运行AArch32的异常级别来管理 VMS ...

  4. camera驱动框架分析(上)【转】

    转自:https://www.cnblogs.com/rongpmcu/p/7662738.html 前言 camera驱动框架涉及到的知识点比较多,特别是camera本身的接口就有很多,有些是直接连 ...

  5. Python3学习笔记14-迭代与列表生成式

    迭代 如果给定一个list或tuple,我们可以通过for循环来遍历这个list或tuple,这种遍历我们称为迭代(Iteration) 在Python中,迭代是通过for...in来完成的. d = ...

  6. mysql锁表与不锁表设置主从复制的方法

    有时候MySQL主从同步不一致比较严重的时候,需要手动同步.先说说在锁表的情况下如何操作:以下是其简要过程 1.先对主库锁表FLUSH TABLES WITH READ LOCK; 2.备份数据mys ...

  7. centos下常用文件管理命令

    fdisk     d 删除分区     n:新建一个分区     p:列出已有分区     t:调整分区ID     l:列出内核支持的分区id     w:保存退出     q:不保存退出    ...

  8. OneNET麒麟座应用开发之三:获取温湿度数据

    对于大气环境监测来说温湿度也是重要的指标.原本计划使用SHT15来采集温湿度数据,不过在OneNET麒麟开发板上,我们发现已经集成有SHT20温湿度传感器,于是我们就使用它了.如下图的红框处: 我们还 ...

  9. 【转】js中的事件委托或是事件代理详解

    起因: 1.这是前端面试的经典题型,要去找工作的小伙伴看看还是有帮助的: 2.其实我一直都没弄明白,写这个一是为了备忘,二是给其他的知其然不知其所以然的小伙伴们以参考: 概述: 那什么叫事件委托呢?它 ...

  10. hdu3193 降维+rmq

    /* 给定n个数对(p,d),如果有这么一个数对(pi,di),其他所有的数对都不能严格小于这个数对 请求出有多少个这样的数对! 解法:对于数对(p,d),能找到在[1,p-1]之间的小于d的数对,那 ...