centos自动安装mysql

前端之家收集整理的这篇文章主要介绍了centos自动安装mysql前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

###########################请先备份数 #######################


——每次安装MysqL,劳神费力,时间宝贵安能浪费


——CentOS6.5 CentOS7.2 自测成功


——现在自己装MysqL都是用这个,那叫一个舒爽


安装:下载 mysql.rar 文件按照 readme.txt 操作即可


############################# 结束符 ##########################

或者也可以自己倒腾

shell文件

  1. #!/bin/sh
  2.  
  3. #关闭进程
  4. /etc/init.d/MysqLd stop
  5. killall MysqLd
  6. pids=($(ps -ef | grep MysqL | awk '{print $2}'))
  7. for((i=0; i<${#pids[@]}; i++));do
  8. echo "关闭进程: pid ${pids[i]}"
  9. kill -9 ${pids[i]}
  10. done
  11.  
  12. #uninstall MariaDB
  13. mariadb=($(rpm -qa|grep -i MariaDB))
  14. for((i=0; i<${#mariadb[@]}; i++));do
  15. echo "旧版mariadb: ${mariadb[i]}"
  16. echo "删除..."
  17. yum -y remove ${mariadb[i]}
  18. done
  19. #强制卸载,防止残留
  20. mariadb=($(rpm -qa|grep -i MariaDB))
  21. for((i=0; i<${#mariadb[@]}; i++));do
  22. echo "旧版mariadb: ${mariadb[i]}"
  23. echo "强制删除..."
  24. rpm -e --nodeps ${mariadb[i]}
  25. rpm -e --noscripts ${mariadb[i]}
  26. done
  27.  
  28. #uninstall old MysqL
  29. old_MysqL=($(rpm -qa|grep -i MysqL))
  30. for((i=0; i<${#old_MysqL[@]}; i++));do
  31. echo "旧版MysqL: ${old_MysqL[i]}"
  32. echo "删除..."
  33. yum -y remove java ${old_MysqL[i]}
  34. done
  35. #remove old MysqL files
  36. old_MysqL_file=($(find / -name MysqL))
  37. for((i=0; i<${#old_MysqL_file[@]}; i++));do
  38. echo "旧版MysqL文件: ${old_MysqL_file[i]}"
  39. if [[ ${old_MysqL_file[i]} =~ "/usr/MysqL" ]]
  40. then
  41. continue
  42. fi
  43. echo "删除..."
  44. rm -rf ${old_MysqL_file[i]}
  45. done
  46. #remove setting files
  47. rm -rf /var/log/MysqLd.log
  48. rm -rf /etc/my.cnf*
  49. rm -rf /run/MysqLd/*
  50. rm -rf /usr/bin/MysqL*
  51.  
  52. #find zip MysqL file
  53. srcPath=$(find /usr/MysqL -maxdepth 1 -type f|grep MysqL*.tar)
  54. if [[ $srcPath =~ "MysqL" ]]
  55. then
  56. echo "安装包:$srcPath"
  57. else
  58. echo "下载MysqL..."
  59. yum -y install wget
  60. wget https://downloads.MysqL.com/archives/get/file/MysqL-5.7.13-1.el6.x86_64.rpm-bundle.tar -P /usr/MysqL/
  61. srcPath=$(find /usr/MysqL -maxdepth 1 -type f|grep MysqL*.tar)
  62. fi
  63.  
  64. #unzip
  65. echo "解压..."
  66. tar xvf $srcPath
  67.  
  68. #安装依赖
  69. echo "安装 perl-DBI..."
  70. yum install -y perl-DBI
  71. echo "安装 libaio..."
  72. yum install -y libaio
  73.  
  74. #install
  75. rpms=($(find /usr/MysqL -maxdepth 1 -type f|grep MysqL-community))
  76. for((r=${#rpms[@]}; r>0; r--));do
  77. echo "$r"
  78. for((i=0; i<${#rpms[@]}; i++));do
  79. echo ""
  80. echo "安装: ${rpms[i]}"
  81. echo "安装..."
  82. rpm -ivh ${rpms[i]}
  83. done
  84. done
  85.  
  86. #开机启动
  87. chkconfig MysqLd on
  88.  
  89. echo ""
  90. echo "安装完毕,初始化设置"
  91. /etc/init.d/MysqLd start
  92.  
  93. tmpPwd=$(grep 'temporary password' /var/log/MysqLd.log |awk -F ': ' '{print $2}')
  94. echo "Temp Password is $tmpPwd"
  95.  
  96. echo "#1.停止MysqL数据库"
  97. /etc/init.d/MysqLd stop
  98. echo "#2.启动MysqLd_safe"
  99. MysqLd_safe --user=MysqL --skip-grant-tables --skip-networking &
  100.  
  101. for((i=0; i<10; i++));do
  102. sleep 4s
  103. ps=($(ps -ef | grep MysqLd_safe | awk '{print $1}'))
  104. if [ ${#ps[@]} -gt 1 ];then
  105. echo "MysqLd_safe started"
  106. echo "#3.使用root登录MysqL数据库并设置密码为"root" "
  107. MysqL -u root MysqL <<EOF
  108. /* 更新root密码 */
  109. UPDATE user SET authentication_string=PASSWORD('root') where USER='root';
  110. /* 刷新权限 */
  111. FLUSH PRIVILEGES;
  112. EOF
  113.  
  114. tmpPwd="root"
  115. break
  116. else
  117. echo "${i}..."
  118. sleep 1s
  119. if [ ${i} -ge 5 ];then
  120. echo "MysqLd_safe : not started"
  121. break;
  122. fi
  123. fi
  124. done
  125.  
  126. echo "重启MysqL"
  127. /etc/init.d/MysqLd restart
  128.  
  129. #将port=3306写入/etc/my.cnf文件
  130. echo "设置端口"
  131. sed -i '/datadir=/i\port=3306' /etc/my.cnf
  132.  
  133. echo "success restart..."
  134. service MysqLd restart
  135.  
  136. echo "MysqL has been installed and started"
  137. echo ""
  138. echo "
  139. You probably need to do operations as follows by yourself for some security reasons.
  140. /* 进入MysqL */
  141. MysqL -uroot -p${tmpPwd}
  142. /* 修改密码长度策略 */
  143. set global validate_password_policy=0;
  144. set global validate_password_length=0;
  145. /* 重置密码 */
  146. alter user 'root'@'localhost' identified by 'root';
  147. FLUSH PRIVILEGES;
  148. /* 设置外网访问 */
  149. use MysqL;
  150. select 'host' from user where user='root';
  151. update user set host = '%' where user='root';
  152. FLUSH PRIVILEGES;
  153. "

1. 放置到 /usr/MysqL 下运行

2. 首次安装自动下载MysqL-5.7.13-1.el6.x86_64.rpm-bundle.tar,或者自己下载新版rpm-bundle.tarMysqL文件放入/usr/MysqL/

3. 初始密码设置为root,请按安装提示激活

猜你在找的CentOS相关文章