CentOS/RadHat一键安装MySQL5.5.37
新到的服务器,部署安装生产环境,找到了LNMP的一键安装包。官方网址为:http://lnmp.org/
但是服务器部署要应用与数据库分开,本来应用服务器和数据库服务器直接都安装lnmp也可以,可强迫症的我不希望看到应用服务器里安装了mysql数据库,同时也不希望数据库服务器里安装nginx、php等。
所以,我就针对LNMP的安装脚本做了修改整理,把安装数据库的东西摘出来了。
mysql-install.sh 脚本:
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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
#!/bin/bash PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin export PATH # Check if user is root if [ $(id -u) != "0" ]; then echo "Error: You must be root to run this script, please use root to install MySQL" exit 1 fi clear echo "=========================================================================" echo "A tool to auto-compile & install MySQL on Linux " echo "=========================================================================" cur_dir=$(pwd) #set mysql root password echo "===========================" mysqlrootpwd="root" echo "Please input the root password of mysql:" read -p "(Default password: root):" mysqlrootpwd if [ "$mysqlrootpwd" = "" ]; then mysqlrootpwd="root" fi echo "===========================" echo "MySQL root password:$mysqlrootpwd" echo "===========================" #do you want to install the InnoDB Storage Engine? echo "===========================" installinnodb="n" echo "Do you want to install the InnoDB Storage Engine?" read -p "(Default no,if you want please input: y ,if not please press the enter button):" installinnodb case "$installinnodb" in y|Y|Yes|YES|yes|yES|yEs|YeS|yeS) echo "You will install the InnoDB Storage Engine" installinnodb="y" ;; n|N|No|NO|no|nO) echo "You will NOT install the InnoDB Storage Engine!" installinnodb="n" ;; *) echo "INPUT error,The InnoDB Storage Engine will NOT install!" installinnodb="n" esac #which MySQL Version do you want to install? echo "===========================" isinstallmysql55="y" get_char() { SAVEDSTTY=`stty -g` stty -echo stty cbreak dd if=/dev/tty bs=1 count=1 2> /dev/null stty -raw stty echo stty $SAVEDSTTY } echo "" echo "Press any key to start...or Press Ctrl+c to cancel" char=`get_char` function InitInstall() { cat /etc/issue uname -a MemTotal=`free -m | grep Mem | awk '{print $2}'` echo -e "\n Memory is: ${MemTotal} MB " #Set timezone rm -rf /etc/localtime ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime yum install -y ntp ntpdate -u pool.ntp.org date rpm -qa|grep mysql rpm -e mysql yum -y remove mysql-server mysql mysql-libs yum -y remove php-mysql yum -y install yum-fastestmirror #Disable SeLinux if [ -s /etc/selinux/config ]; then sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config fi cp /etc/yum.conf /etc/yum.conf.lnmp sed -i 's:exclude=.*:exclude=:g' /etc/yum.conf for packages in patch make cmake gcc gcc-c++ gcc-g77 flex bison file libtool libtool-libs autoconf kernel-devel libjpeg libjpeg-devel libpng libpng-devel libpng10 libpng10-devel gd gd-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glib2 glib2-devel bzip2 bzip2-devel libevent libevent-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel vim-minimal nano fonts-chinese gettext gettext-devel ncurses-devel gmp-devel pspell-devel unzip libcap diffutils; do yum -y install $packages; done mv -f /etc/yum.conf.lnmp /etc/yum.conf } function CheckAndDownloadFiles() { echo "============================check files==================================" if [ -s mysql-5.5.37.tar.gz ]; then echo "mysql-5.5.37.tar.gz [found]" else echo "Error: mysql-5.5.37.tar.gz not found!!!download now......" wget -c http://soft.vpser.net/datebase/mysql/mysql-5.5.37.tar.gz fi if [ -s mysql-openssl.patch ]; then echo "mysql-openssl.patch [found]" else echo "Error: mysql-openssl.patch not found!!!download now......" wget -c http://soft.vpser.net/lnmp/ext/mysql-openssl.patch fi echo "============================check files==================================" } function InstallMySQL55() { echo "============================Install MySQL 5.5.26==================================" cd $cur_dir rm -f /etc/my.cnf tar zxf mysql-5.5.37.tar.gz cd mysql-5.5.37/ patch -p1 < $cur_dir/mysql-openssl.patch cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_READLINE=1 -DWITH_SSL=system -DWITH_ZLIB=system -DWITH_EMBEDDED_SERVER=1 -DENABLED_LOCAL_INFILE=1 make && make install groupadd mysql useradd -s /sbin/nologin -M -g mysql mysql cp support-files/my-medium.cnf /etc/my.cnf sed '/skip-external-locking/i\datadir = /usr/local/mysql/var' -i /etc/my.cnf if [ $installinnodb = "y" ]; then sed -i 's:#innodb:innodb:g' /etc/my.cnf sed -i 's:/usr/local/mysql/data:/usr/local/mysql/var:g' /etc/my.cnf else sed '/skip-external-locking/i\default-storage-engine=MyISAM\nloose-skip-innodb' -i /etc/my.cnf fi /usr/local/mysql/scripts/mysql_install_db --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql --datadir=/usr/local/mysql/var --user=mysql chown -R mysql /usr/local/mysql/var chgrp -R mysql /usr/local/mysql/. cp support-files/mysql.server /etc/init.d/mysql chmod 755 /etc/init.d/mysql cat > /etc/ld.so.conf.d/mysql.conf<<EOF /usr/local/mysql/lib /usr/local/lib EOF ldconfig ln -s /usr/local/mysql/lib/mysql /usr/lib/mysql ln -s /usr/local/mysql/include/mysql /usr/include/mysql if [ -d "/proc/vz" ];then ulimit -s unlimited fi /etc/init.d/mysql start ln -s /usr/local/mysql/bin/mysql /usr/bin/mysql ln -s /usr/local/mysql/bin/mysqldump /usr/bin/mysqldump ln -s /usr/local/mysql/bin/myisamchk /usr/bin/myisamchk ln -s /usr/local/mysql/bin/mysqld_safe /usr/bin/mysqld_safe /usr/local/mysql/bin/mysqladmin -u root password $mysqlrootpwd cat > /tmp/mysql_sec_script<<EOF use mysql; update user set password=password('$mysqlrootpwd') where user='root'; delete from user where not (user='root') ; delete from user where user='root' and password=''; drop database test; DROP USER ''@'%'; flush privileges; EOF /usr/local/mysql/bin/mysql -u root -p$mysqlrootpwd -h localhost < /tmp/mysql_sec_script rm -f /tmp/mysql_sec_script /etc/init.d/mysql restart /etc/init.d/mysql stop echo "============================MySQL 5.5.26 install completed=========================" } function AddAndStartup() { /etc/init.d/mysql start #add iptables firewall rules if [ -s /sbin/iptables ]; then /sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT /sbin/iptables -I INPUT -p tcp --dport 3306 -j DROP /sbin/iptables-save fi } function CheckInstall() { echo "===================================== Check install ===================================" clear ismysql="" echo "Checking..." if [ -s /usr/local/mysql/bin/mysql ] && [ -s /usr/local/mysql/bin/mysqld_safe ] && [ -s /etc/my.cnf ]; then echo "MySQL: OK" ismysql="ok" else echo "Error: /usr/local/mysql not found!!!MySQL install failed." fi if [ "$ismysql" = "ok" ] ; then echo "Install MySQL completed! enjoy it." echo "default mysql root password:$mysqlrootpwd" echo "The path of some dirs:" echo "mysql dir: /usr/local/mysql" echo "" echo "=========================================================================" netstat -ntl else echo "Sorry,Failed to install mysql!" fi } InitInstall 2>&1 | tee /root/lnmp-install.log CheckAndDownloadFiles 2>&1 | tee -a /root/lnmp-install.log InstallMySQL55 2>&1 | tee -a /root/lnmp-install.log AddAndStartup 2>&1 | tee -a /root/lnmp-install.log CheckInstall 2>&1 | tee -a /root/lnmp-install.log |
执行上述脚本也可以自动下载安装程序包,自动完成安装。为了节省时间也可以提前下载。
自己整理了一个安装包,分享出来下载网址:http://pan.baidu.com/s/1i3l6KK1
不过可能会安装一些没用的依赖文件,因为我也没有仔细的删除掉,但是安装就安装了吧,无所谓也不会影响服务器性能。