参考

MySQL-5.7

实践

vim mysql.sh

  1. #!/bin/bash
  2. v_fileName=$1
  3. #获取当前脚本路径
  4. v_curPath=$(cd `dirname $0`; pwd)
  5. v_log=${v_curPath}"/"$0".log"
  6. #声明消息提醒前缀方法
  7. f_msg(){
  8. echo $(date "+%Y-%m-%d %H:%M:%S")$LINENO" "$1"==>"
  9. }
  10. #判断日志文件是否存在若不存在则创建
  11. if [ ! -f ${v_log} ]
  12. then
  13. touch ${v_log}
  14. echo $(f_msg INFO)$LINENO" 创建日志文件" >> ${v_log}
  15. else
  16. echo "=======================================">>${v_log}
  17. fi
  18. #校验工具是否存在方法
  19. f_checkRpm(){
  20. v_name1=$1
  21. v_check=$(rpm -qa ${v_name1})
  22. if [ ${#v_check} -eq 0 ]
  23. then
  24. echo $(f_msg ERROR)$LINENO " "${v_name1}"工具不存在,请安装"${v_name1}"工具">>${v_log}
  25. return 0
  26. else
  27. echo $(f_msg INFO)$LINENO " "${v_name1}"工具的版本号为"${v_check}>>${v_log}
  28. return 1
  29. fi
  30. }
  31. v_yum=$(rpm -qa yum)
  32. if [ ${#v_yum} -eq 0 ]
  33. then
  34. echo $(f_msg ERROR)$LINENO" 未检测到yum,请安装yum"
  35. echo $(f_msg ERROR)$LINENO" 未检测到yum,请安装yum" >> ${v_log}
  36. exit 0
  37. else
  38. echo $(f_msg INFO)$LINENO" yum的版本为"${v_yum} >> ${v_log}
  39. fi
  40. v_tcl=$(rpm -qa tcl)
  41. if [ ${#v_tcl} -eq 0 ]
  42. then
  43. echo $(f_msg INFO)$LINENO" 未检测到tcl,请安装tcl"
  44. echo "执行语句 yum -y install tcl "
  45. echo $(f_msg INFO)$LINENO" 未检测到tcl,请安装tcl" >> ${v_log}
  46. exit 0
  47. else
  48. echo $(f_msg INFO)$LINENO" tcl的版本为"${v_tcl} >> ${v_log}
  49. fi
  50. v_expect=$(rpm -qa expect)
  51. if [ ${#v_expect} -eq 0 ]
  52. then
  53. echo $(f_msg INFO)$LINENO" 未检测到expect,请安装expect"
  54. echo "执行语句 yum -y install expect "
  55. echo $(f_msg INFO)$LINENO" 未检测到expect,请安装expect" >> ${v_log}
  56. exit 0
  57. else
  58. echo $(f_msg INFO)$LINENO" expect的版本为"${v_expect} >> ${v_log}
  59. fi
  60. #判断文件是否存在,若不存在则下载
  61. if [ ${#v_fileName} -eq 0 ]
  62. then
  63. f_checkRpm wget
  64. v_rqmReturn=$?
  65. #检测当前目录下是否有mysql文件
  66. if [ ${v_rqmReturn} -eq 0 ]
  67. then
  68. echo $(f_msg INFO)$LINENO" 未检测到wget,请安装wget" >> ${v_log}
  69. exit 0
  70. fi
  71. v_mysqlC=$(ls -ld mysql* |grep ^-|wc -l)
  72. if [ ${v_mysqlC} -lt 1 ]
  73. then
  74. #若不存在则下载
  75. wget http://dev.MySQL.com/get/Downloads/MySQL-5.7/mysql-5.7.11-Linux-glibc2.5-x86_64.tar.gz
  76. fi
  77. else
  78. if [ ! -f ${v_fileName} ]
  79. then
  80. wget http://dev.MySQL.com/get/Downloads/MySQL-5.7/${v_fileName}
  81. fi
  82. fi
  83. #检验tar是否存在
  84. v_tar=$(rpm -qa tar)
  85. if [ ${#v_tar} -le 0 ]
  86. then
  87. echo $(f_msg ERROR)$LINENO" tar工具不存在,请安装tar工具">>${v_log}
  88. exit 0
  89. else
  90. echo $(f_msg INFO)$LINENO" tar工具的版本号为"${v_tar}>>${v_log}
  91. fi
  92. #获取文件名
  93. for fileName in `ls -d mysql*`
  94. do
  95. if [ -f fileName ]
  96. then
  97. v_fileName=${fileName}
  98. fi
  99. done
  100. echo $(f_msg INFO)$LINENO" 获取mysql压缩包文件名"${v_fileName}>>${v_log}
  101. #解压文件
  102. mkdir mysql
  103. tar -xvf ${v_fileName} -C mysql
  104. cd mysql
  105. v_mysqlChildC=$(ls -l|grep ^d|wc -l)
  106. v_tempFileName=""
  107. for fileName in `ls`
  108. do
  109. v_tempFileName=${fileName}
  110. done
  111. cd ${v_tempFileName}
  112. mv * ../
  113. cd ..
  114. rm -rf ${v_tempFileName}
  115. #创建data目录
  116. mkdir -p /usr/local/mysql/data
  117. #创建mysql用户组及其用户
  118. if [ ! $(grep mysql /etc/group) ]
  119. then
  120. groupadd mysql
  121. echo $(f_msg INFO)$LINENO" 创建mysql组成功">>${v_log}
  122. else
  123. echo $(f_msg INFO)$LINENO" mysql组已存在无需创建">>${v_log}
  124. fi
  125. #判断密码是否输入,若不输入则默认值为root
  126. v_pwd=${2}
  127. if [ ${#v_pwd} -eq 0 ]
  128. then
  129. v_pwd="mmsql"
  130. fi
  131. #创建用户
  132. if [ ! $(grep mysql /etc/passwd) ]
  133. then
  134. useradd -r -s /sbin/nologin -g mysql mysql -p v_pwd
  135. echo $(f_msg INFO)$LINENO" 创建mysql用户成功">>${v_log}
  136. else
  137. echo $(f_msg INFO)$LINENO" mysql组已存在无需创建">>${v_log}
  138. fi
  139. #初始化mysql数据
  140. cd /usr/local/mysql/
  141. ./bin/mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/
  142. echo $(f_msg INFO)$LINENO" 初始化mysql数据">>${v_log}
  143. #判断my.cnf 是否存在若存在则删除
  144. if [ $(ls -l /etc/my.cnf | wc -l) -ne 0 ]
  145. then
  146. rm -rf /etc/my.cnf
  147. fi
  148. if [ $(ls -l /etc/init.d/mysqld | wc -l) -ne 0 ]
  149. then
  150. rm -rf /etc/init.d/mysqld
  151. fi
  152. #拷贝文件
  153. cp -a ./support-files/my-default.cnf /etc/my.cnf
  154. cp -a ./support-files/mysql.server /etc/init.d/mysqld
  155. echo $(f_msg INFO)$LINENO" 拷贝mysql配置文件">>${v_log}
  156. #修改my.cnf内容
  157. sed -i "s/# basedir = ...../basedir = \/usr\/local\/mysql/g" /etc/my.cnf
  158. sed -i "s/# datadir = ...../datadir = \/usr\/local\/mysql\/data/g" /etc/my.cnf
  159. sed -i "s/# port = ...../port = 3306/g" /etc/my.cnf
  160. sed -i "s/# socket = ...../socket = \/tmp\/mysql.sock/g" /etc/my.cnf
  161. #echo "skip-grant-tables " >>/etc/my.cnf
  162. #判断character-set-server 是否存在,若存在则删除
  163. if [ $(grep character-set-server /etc/my.cnf) ]
  164. then
  165. sed -i "/character-set-server/d" /etc/my.cnf
  166. fi
  167. echo "character-set-server = utf8" >> /etc/my.cnf
  168. if [ $(grep client /etc/my.cnf) ]
  169. then
  170. sed -i "/client/d" /etc/my.cnf
  171. fi
  172. #echo "[client]" >> /etc/my.cnf
  173. #echo "host = localhost" >> /etc/my.cnf
  174. #echo "user = root" >> /etc/my.cnf
  175. #echo "password ="123456 >> /etc/my.cnf
  176. #echo $(f_msg INFO)$LINENO" 修改my.cnf文件">>${v_log}
  177. #将mysql 添加到环境变量中
  178. echo "export PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile
  179. #重新加载profile文件
  180. /usr/bin/expect<<EOF
  181. set timeout -1
  182. spawn su - root
  183. expect "*"
  184. send "source /etc/profile\n"
  185. expect "*"
  186. send "exit\n"
  187. expect eof
  188. EOF
  189. #source /etc/profile
  190. #echo $(f_msg INFO)$LINENO" 配置MySQL的环境变量,使mysql命令生效">>${v_log}
  191. #创建In
  192. ln -s /usr/local/mysql/ /usr/bin/
  193. #启动服务
  194. service mysqld start
  195. #获取mysql 初始密码
  196. v_mysqlinitpwd=$(cat /root/.mysql_secret | awk 'END {print}')
  197. #修改MySQL初始密码
  198. <<!
  199. /usr/local/mysql/bin/mysql -u root -p${v_mysqlinitpwd} -e "alter user root@localhost identified by '123456';flush privileges;"
  200. echo $(f_msg INFO)$LINENO" 修改root用户初始密码">>${v_log}
  201. #查询mysql用户是否有外网登录权限
  202. v_msyqlC=$(/usr/local/mysql/bin/mysql -N -s -u root -p${v_mysqlinitpwd} -D mysql -e "select count(1) from user where host ='%' and user='mysql';")
  203. if [ ${v_msyqlC} -le 0 ]
  204. then
  205. #创建用户mysql
  206. /usr/local/mysql/bin/mysql -N -s -u root -p${v_mysqlinitpwd} -D mysql -e "CREATE USER 'mysql'@'host' IDENTIFIED BY '123456';flush privileges;"
  207. #给用户mysql 授权
  208. /usr/local/mysql/bin/mysql -N -s -u root -p${v_mysqlinitpwd} -D mysql -e "grant all privileges on *.* to mysql@'%' identified by '123456';flush privileges;"
  209. fi
  210. echo $(f_msg INFO)$LINENO" 添加mysql用户,并给mysql用户授权">>${v_log}
  211. !
  212. /usr/bin/expect<<EOF
  213. set timeout -1
  214. spawn su - root
  215. expect "*" { send "/usr/local/mysql/bin/mysql -u root -p\n" }
  216. expect "Enter password:" { send "${v_mysqlinitpwd}\n" }
  217. expect "mysql>" { send "alter user root@localhost identified by '123456';\n" }
  218. expect "mysql>" { send "flush privileges;\n" }
  219. expect "mysql>" { send "use mysql;\n"}
  220. expect "mysql>" { send "CREATE USER 'mysql'@'host' IDENTIFIED BY '123456';\n" }
  221. expect "mysql>" { send "flush privileges;\n" }
  222. expect "mysql>" { send "grant all privileges on *.* to mysql@'%' identified by '123456';\n" }
  223. expect "mysql>" { send "flush privileges;\n" }
  224. expect "mysql>" { send "exit\n" }
  225. expect "*"
  226. send "exit\n"
  227. expect eof
  228. EOF
  229. echo $(f_msg INFO)$LINENO" 初始化数据库信息">>${v_log}
  230. #设置开机启动 mysql
  231. systemctl enable mysqld
  232. systemctl daemon-reload
  233. echo $(f_msg INFO)$LINENO" 设置mysql开机启动">>${v_log}
  234. echo "需要手动执行 source /etc/profile"
  235. echo "如外网无法调用请检查防火墙的端口是否打开"
  236. echo "执行完毕"

注意

  • 133 行创建用户时 不要用这里的密码请自行设置防止被攻击
  • 数据库默认密码为123456请提前更换后执行脚本或mysql启动后更改
  • 脚本启动是要设置 chmod -R 777 xx.sh 要不然不能执行sh文件
  • 脚本要放置在 usr/local/文件夹下 要不然会报 No such file or directory 的错
    • 我尝试修改过,没改成功。
  • mysql文件可以使用离线的

shell 问题总汇

https://renwole.com/archives/2215

  1. #!/bin/env bash
  2. # =================================================
  3. # Author: renwole.com #
  4. # Description: MySQL automatic installation tool #
  5. # =================================================
  6. export PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin
  7. Install_MySQL() {
  8. # CentOS 8 mysql: error while loading shared libraries: libncurses.so.5
  9. yum install ncurses ncurses-compat-libs -y
  10. # Install dependencies
  11. setenforce 0
  12. yum -y install epel-release
  13. yum -y install wget tar jemalloc jemalloc-devel gcc gcc-c++
  14. # MySQL configuration
  15. mysql_version="8.0.21"
  16. mysql_password="111"
  17. mkdir -p /apps/server/mysql/data
  18. mysql_install_dir="/apps/server/mysql"
  19. mysql_data_dir="/apps/server/mysql/data"
  20. download_mysql="https://mirrors.tuna.tsinghua.edu.cn/mysql/downloads"
  21. # Download MySQL
  22. cd /mnt
  23. useradd -M -s /sbin/nologin mysql >/dev/null 2>&1
  24. wget -c ${download_mysql}/MySQL-8.0/mysql-${mysql_version}-linux-glibc2.12-x86_64.tar.xz
  25. echo "========== Start installing MySQL ============"
  26. tar xJf mysql-${mysql_version}-linux-glibc2.12-x86_64.tar.xz
  27. \mv mysql-${mysql_version}-linux-glibc2.12-x86_64/* ${mysql_install_dir}
  28. if [[ -d "${mysql_install_dir}/support-files" ]]; then
  29. rm -rf mysql-${mysql_version}-linux-glibc2.12-x86_64
  30. else
  31. rm -rf ${mysql_install_dir}
  32. echo "Error: MySQL install failed, Please contact the author"
  33. kill -9 $$
  34. fi
  35. # Initialize the database
  36. ${mysql_install_dir}/bin/mysqld --initialize-insecure --user=mysql --basedir=${mysql_install_dir} --datadir=${mysql_data_dir}
  37. chown -R root . ${mysql_install_dir}
  38. chown -R mysql.mysql ${mysql_data_dir}
  39. \cp -f ${mysql_install_dir}/support-files/mysql.server /etc/init.d/mysqld
  40. sed -i "s@^basedir=.*@basedir=${mysql_install_dir}@" /etc/init.d/mysqld
  41. sed -i "s@^datadir=.*@datadir=${mysql_data_dir}@" /etc/init.d/mysqld
  42. sed -i "s@/usr/local/mysql@${mysql_install_dir}@g" ${mysql_install_dir}/bin/mysqld_safe
  43. sed -i 's@executing mysqld_safe@executing mysqld_safe\nexport LD_PRELOAD=/usr/lib64/libjemalloc.so@' ${mysql_install_dir}/bin/mysqld_safe
  44. echo 'PATH=$PATH:'${mysql_install_dir}'/bin' >/etc/profile.d/mariadb_renwole.com.sh
  45. # my.conf configuration
  46. cat > /etc/my.cnf << EOF
  47. [client]
  48. port = 3306
  49. socket = /tmp/mysql.sock
  50. default-character-set = utf8mb4
  51. [mysql]
  52. prompt="MySQL [\\d]> "
  53. no-auto-rehash
  54. [mysqld]
  55. port = 3306
  56. socket = /tmp/mysql.sock
  57. default_authentication_plugin = mysql_native_password
  58. basedir = ${mysql_install_dir}
  59. datadir = ${mysql_data_dir}
  60. pid-file = ${mysql_data_dir}/mysql.pid
  61. user = mysql
  62. bind-address = 0.0.0.0
  63. server-id = 1
  64. init-connect = 'SET NAMES utf8mb4'
  65. character-set-server = utf8mb4
  66. collation-server = utf8mb4_0900_ai_ci
  67. skip-name-resolve
  68. #skip-networking
  69. back_log = 300
  70. max_connections = 1000
  71. max_connect_errors = 6000
  72. open_files_limit = 65535
  73. table_open_cache = 128
  74. max_allowed_packet = 500M
  75. binlog_cache_size = 1M
  76. max_heap_table_size = 8M
  77. tmp_table_size = 16M
  78. read_buffer_size = 2M
  79. read_rnd_buffer_size = 8M
  80. sort_buffer_size = 8M
  81. join_buffer_size = 8M
  82. key_buffer_size = 4M
  83. thread_cache_size = 8
  84. ft_min_word_len = 4
  85. log_bin = mysql-bin
  86. binlog_format = mixed
  87. binlog_expire_logs_seconds = 604800
  88. log_error = ${mysql_data_dir}/mysql-error.log
  89. slow_query_log = 1
  90. long_query_time = 1
  91. slow_query_log_file = ${mysql_data_dir}/mysql-slow.log
  92. performance_schema = 0
  93. explicit_defaults_for_timestamp
  94. #lower_case_table_names = 1
  95. skip-external-locking
  96. default_storage_engine = InnoDB
  97. #default-storage-engine = MyISAM
  98. innodb_file_per_table = 1
  99. innodb_open_files = 500
  100. innodb_buffer_pool_size = 64M
  101. innodb_write_io_threads = 4
  102. innodb_read_io_threads = 4
  103. innodb_thread_concurrency = 0
  104. innodb_purge_threads = 1
  105. innodb_flush_log_at_trx_commit = 2
  106. innodb_log_buffer_size = 2M
  107. innodb_log_file_size = 32M
  108. innodb_log_files_in_group = 3
  109. innodb_max_dirty_pages_pct = 90
  110. innodb_lock_wait_timeout = 120
  111. bulk_insert_buffer_size = 8M
  112. myisam_sort_buffer_size = 8M
  113. myisam_max_sort_file_size = 10G
  114. myisam_repair_threads = 1
  115. interactive_timeout = 28800
  116. wait_timeout = 28800
  117. [mysqldump]
  118. quick
  119. max_allowed_packet = 500M
  120. [myisamchk]
  121. key_buffer_size = 8M
  122. sort_buffer_size = 8M
  123. read_buffer = 4M
  124. write_buffer = 4M
  125. EOF
  126. # Set up MySQL
  127. chmod 600 /etc/my.cnf
  128. chmod +x /etc/init.d/mysqld
  129. systemctl enable mysqld
  130. systemctl start mysqld
  131. ${mysql_install_dir}/bin/mysql -uroot -hlocalhost -e "create user root@'127.0.0.1' identified by \"${mysql_password}\";"
  132. ${mysql_install_dir}/bin/mysql -uroot -hlocalhost -e "grant all privileges on *.* to root@'127.0.0.1' with grant option;"
  133. ${mysql_install_dir}/bin/mysql -uroot -hlocalhost -e "grant all privileges on *.* to root@'localhost' with grant option;"
  134. ${mysql_install_dir}/bin/mysql -uroot -hlocalhost -e "alter user root@'localhost' identified by \"${mysql_password}\";"
  135. ${mysql_install_dir}/bin/mysql -uroot -p${mysql_password} -e "reset master;"
  136. rm -rf /etc/ld.so.conf.d/{mysql,mariadb,percona,alisql}*.conf
  137. echo "${mysql_install_dir}/lib" > /etc/ld.so.conf.d/mysql_renwole.com.conf
  138. ldconfig
  139. echo "========== MySQL installing Successfully ====="
  140. echo
  141. echo "=============================================="
  142. echo " Thank you for using Renwole.com script"
  143. echo "=============================================="
  144. echo "MySQL:"
  145. echo "account: root"
  146. echo "password: ${mysql_password}"
  147. echo "database: ${mysql_data_dir}"
  148. echo "=============================================="
  149. }
  150. Install_MySQL
  151. source /etc/profile