脚本须知:
1. 该脚本目前只测试过mysql版本为5.6.x的源码,其他源码可以对本脚本稍作修改即可
2. 本脚本也可以使用wget mysql源码的方式进行,但考虑到后期提供源码的地址不可用,所以需要手动下载mysql 5.6.x的源码包并放入到/usr/local/src
3. 此脚本在编译安装过程中禁用了iptables和SElinux策略,如果你在使用iptalbes相关功能请先完成相关配置。
4. 该脚本主要是对此前尚未装过mysql的linux系统环境而写的,如果此前装过mysql请根据脚本适当修改,主要是在脚本添加判断语句,或者你也可以移除之前的mysql
版本 (mysql安装路径 配置文件)
5. 本脚本实现的主要功能是mysql 安全初始化安装;提供sysv风格的启动脚本;最基本的配置文件(你可以根据功能需要通过配置然后覆盖/etc/my.cnf实现定制)
6. mysql 5.6.30源码编译安装 默认没有开启查询缓存,你可以在my.cnf 通过此选项 query_cache_type = ON 来启用
1 #!/bin/bash 2 # 3 # Description: Automated Deployment compilation MySQL 4 # Author: jiajunwei 5 # Version: 2016-06-23-01 6 # Environment: 7 # 1. Mysql-Version: mysql-5.6.x 8 # 2. Operating system: CentOS 6.x 9 # Advise: The MySQL data directory on a logical volume 10 # Preparation: Need to put the source package mysql-5.6.x.tar.gz to /usr/local/src 11 12 #####################[Preparatory work before compiling]###################### 13 14 # 1. Provide the compiler environment 15 echo -e "\033[31mThe First step:Check and Configure compile environment ......\033[0m" 16 17 #if ! ping -c2 -w3 www.baidu.com &> /dev/null;then 18 # mkdir -p /etc/yum.repos.d/repo 19 # mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/repo 20 # cat << EOF > /etc/yum.repos.d/CentOS-Media.repo 21 # [c6-media] 22 # name=CentOS-$releasever - Media 23 # baseurl=file:///media/cdrom/ 24 # gpgcheck=1 25 # enabled=1 26 # gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6 27 #EOF 28 # mkdir -p /media/cdrom 29 # if od -ax /dev/cdrom | head -1 &> /dev/null;then 30 # umount /dev/cdrom &> /dev/null 31 # mount -r /dev/cdrom /media/cdrom 32 # else 33 # echo "Please mount the disk and load the system image" 34 # exit 2 35 # fi 36 #fi 37 yum -y groupinstall "Development Tools" &> /dev/null 38 yum -y install cmake bison ncurses-devel openssl-devel &> /dev/null 39 if [ $? -eq 0 ];then 40 echo "--------->Enviroment is ok " 41 else 42 echo "--->Failed to compile environment testing, please check" 43 exit 3 44 fi 45 46 # 2. Add the operation of database users 47 echo -e "\033[31mThe second step:Add the operation of database users with user mysql......\033[0m" 48 [ ! `grep "mysql" /etc/group` ] && groupadd -r mysql 49 [ ! `grep "mysql" /etc/passwd` ] && useradd -r -d /mydata/data -g mysql -s /bin/false mysql 50 if id mysql &> /dev/null;then 51 echo "--------->Add user complete " 52 else 53 echo "--->Add user failed " 54 exit 4 55 fi 56 #[ -f /etc/yum.repos.d/repo/CentOS-Base.repo ] && mv /etc/yum.repos.d/repo/*.repo /etc/yum.repos.d/ 57 # 3. To determine the database directory and modify it belongs to the main group 58 echo -e "\033[31mThe third step:Determine the data directory and modify the permissions......\033[0m" 59 mkdir -p /mydata/data 60 chown -R mysql.mysql /mydata/data 61 echo "--->OK" 62 63 ################################[Compile and install MySQL]####################### 64 65 echo -e "\033[31mThe forth step:Compile and install MySQL......\033[0m" 66 if ! ls -l /usr/local/src/mysql*.tar.* &> /dev/null;then 67 echo " Copy your source package to this path:/usr/local/src" 68 exit 5 69 fi 70 tar xf /usr/local/src/mysql*.tar.* -C /usr/local/src 71 dir_count=`find /usr/local/src/ -maxdepth 1 -type d -name "mysql*" | wc -l` 72 if [ $dir_count -eq 1 ];then 73 dir=`find /usr/local/src/ -maxdepth 1 -type d -name "mysql*"` 74 else 75 echo "This path cannot have two MySQL directories at the same time" 76 exit 5 77 fi 78 cd $dir 79 cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql 80 -DMYSQL_DATADIR=/mydata/data/ 81 -DSYSCONFDIR=/etc/ -DWITH_INNOBASE_STORAGE_ENGINE=1 82 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 83 -DWITH_FEDERATED_STORAGE_ENGINE=1 84 -DWITH_ARCHIVE_STORAGE_ENGINE=1 85 -DWITH_READLINE=1 86 -DWITH_SSL=system 87 -DWITH_ZLIB=system 88 -DWITH_LIBWRAP=0 89 -DMYSQL_UNIX_ADDR=/mydata/data/mysql.sock 90 -DDEFAULT_CHARSET=utf8 91 -DDEFAULT_COLLATION=utf8_general_ci 92 -DMYSQL_TCP_PORT=3306 93 -DENABLED_LOCAL_INFILE=1 94 -DWITH_DEBUG=0 -DENABLED_PROFILING=1 95 -DWITH_EXTRA_CHARSETS=all 96 -DWITH_INNODB_MEMCACHED=1 97 cpu_count=`cat /proc/cpuinfo | grep "processor" |wc -l` 98 make -j $cpu_count && make install 99 if [ $? -eq 0 ];then 100 echo "------->installation is complete" 101 else 102 echo "--->Compile failed, please check" 103 exit 6 104 fi 105 106 ############################[boot startup script]####################### 107 echo -e "\033[31mThe Fifth step:Provide configuration files and sysV boot startup script for MySQL......\033[0m" 108 [ -f /etc/my.cnf ] && mv -f /etc/my.cnf{,.bak} 109 cp /usr/local/mysql/support-files/my-default.cnf /etc/my.cnf 110 sed -i ‘/^\[mysqld\]/ a \datadir=/mydata/data\nsocket=/mydata/data/mysql.sock\n 111 pid-file=/var/run/mysqld/mysqld.pid\n 112 log-error=/var/log/mysqld.log\n 113 user=mysql\n 114 innodb_file_per_table=ON\n 115 skip_name_resolve=ON\n 116 log-bin=/mydata/log-bin/mysql-bin‘ /etc/my.cnf 117 mkdir -p /var/run/mysqld 118 chown -R mysql.mysql /var/run/mysqld 119 mkdir -p /mydata/log-bin 120 chown -R mysql.mysql /mydata/log-bin 121 [ -f /etc/init.d/mysqld ] && mv -f /etc/init.d/mysqld{,.bak} 122 cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld 123 chmod 755 /etc/init.d/mysqld 124 chkconfig --add mysqld 125 chkconfig --level 35 mysqld on 126 echo "------->OK" 127 128 #############################[Export binary files]######################## 129 echo -e "\033[31mThe sixth step:Export MySQL related binary files......\033[0m" 130 echo "export PATH=/usr/local/mysql/bin:$PATH" > /etc/profile.d/mysql-5.6.sh 131 #source /etc/profile 132 exec bash 133 if which mysqld_safe &> /dev/null;then 134 echo "------>export is successful" 135 else 136 echo "--->export is faild" 137 fi 138 139 ###########################[MySQL database initialization]################## 140 echo -e "\033[31mThe seventh step:MySQL database initialization .......\033[0m" 141 cd /mydata/data 142 tar -jcf mydata-data.tar.bz2 * 143 mv -f mydata-data.tar.bz2 /tmp/ 144 rm -rf * 145 /usr/local/mysql/scripts/mysql_install_db --defaults-file=/etc/my.cnf 146 --basedir=/usr/local/mysql 147 --datadir=/mydata/data/ 148 --user=mysql 149 if [ $? -eq 0 ];then 150 echo "--->initiate is finished " 151 else 152 echo "------->initiate is failed " 153 exit 7 154 fi 155 156 ###########################[Database Startup Test]#################### 157 echo -e "\033[31mThe eighth step:Database Startup Test......\033[0m" 158 # 1. Set firewall and SELinux 159 service iptables stop 160 setenforce 0 161 # 2. Run mysql service 162 service mysqld start 163 if netstat -lntp | grep "mysql" &> /dev/null;then 164 echo "------->Service startup normal " 165 else 166 echo "---->Service startup failed " 167 exit 8 168 fi 169 170 ########################[Run security setup script]##################### 171 echo -e "\033[31mThe last step: Run security setup script......\033[0m" 172 echo -e "\ny\nmysqldba\nmysqldba\ny\nn\ny\ny\n" | mysql_secure_installation 173 if [ ! $? -eq 0 ];then 174 echo "Security initialization failed, please run manually" 175 fi 176 echo -e "\033[31mPlease re check and configure the firewall policy and selinux, now they are disabled.\033[0m"
时间: 2024-10-15 04:29:34