千家信息网

MySQL学习笔记(一):shell脚本安装配置mysql

发表于:2025-01-24 作者:千家信息网编辑
千家信息网最后更新 2025年01月24日,该脚本用于MySQL二进制安装,仅限于最初安装,如服务器已安装配置有mysql,不建议使用该脚本。操作系统版本:CentOS 7数据库版本:MySQL 5.7.22#!/bin/bashcnf_fil
千家信息网最后更新 2025年01月24日MySQL学习笔记(一):shell脚本安装配置mysql

该脚本用于MySQL二进制安装,仅限于最初安装,如服务器已安装配置有mysql,不建议使用该脚本。

操作系统版本:CentOS 7

数据库版本:MySQL 5.7.22


#!/bin/bashcnf_file="/etc/my.cnf"install_log="mysql_install.log"dbfile="mysql-5.7.22-el7-x86_64.tar.gz"ext_files="mysql-5.7.22-el7-x86_64"bin_path="/usr/local/mysql"pid_path="/data/mysql"data_path="/data/mysql/data"errlog_path="/data/mysql/logs/"binlog_path="/data/mysql/binlog/"slowlog_path="/data/mysql/slow_logs"socket="/data/mysql/mysql3306.sock"port=3306serverid=330601group="mysql"user="mysql"if [ ! -f "$dbfile" ];thenecho "$dbfile not exists"  > $install_logelsetar -zxvf $dbfile > $install_logif [ $? -eq 0 ];thenecho "$dbfile Extract Success" >> $install_logelseecho "$dbfile Extract Failed"  >> $install_logexit 0fifiif [ ! -d "$bin_path" ];thenecho "moving $ext_files to $bin_path ......"  >> $install_logmv $ext_files $bin_pathecho "move $ext_files to $bin_path ......[done]" >> $install_logecho "creating $pid_path ......" >> $install_logif [ ! -d "$pid_path" ];thenmkdir -p $pid_pathecho "create $pid_path ......[done]" >> $install_logelseecho "create $pid_path ......[failed]" >> $install_logexit 0fiecho "creating $data_path ......" >> $install_logif [ ! -d "$data_path" ];thenmkdir -p $data_pathecho "create $data_path ......[done]" >> $install_logelseecho "create $data_path ......[failed]" >> $install_logexit 0fiecho "creating $errlog_path ......" >> $install_logif [ ! -d "$errlog_path" ];thenmkdir -p $errlog_pathecho "create $errlog_path ......[done]" >> $install_logelseecho "create $errlog_path ......[failed]" >> $install_logexit 0fiecho "creating $binlog_path ......" >> $install_logif [ ! -d "$binlog_path" ];thenmkdir -p $binlog_pathecho "create $binlog_path ......[done]" >> $install_logelseecho "create $binlog_path ......[failed]" >> $install_logexit 0fiecho "creating $slowlog_path ......" >> $install_logif [ ! -d "$slowlog_path" ];thenmkdir -p $slowlog_pathecho "create $slowlog_path ......[done]" >> $install_logelseecho "create $slowlog_path ......[failed]" >> $install_logexit 0fielseecho "$bin_path already exists" >> $install_logexit 0fi#create group if not existsegrep "^$group" /etc/group >& /dev/nullif [ $? -ne 0 ]then    echo "group $group not exists" >> $install_logecho "creating group $group ......" >> $install_loggroupadd $groupecho "creat group $group ......[done]" >> $install_logelseecho "group $group already exists ......[skip]" >> $install_logfi#create user if not existsegrep "^$user" /etc/passwd >& /dev/nullif [ $? -ne 0 ]then    echo "user $user not exists" >> $install_logecho "creating user $user ......" >> $install_loguseradd -g $group $userecho "creat user $user ......[done]" >> $install_logelseecho "user $user already exists ......[skip]" >> $install_logfi#Modify directory permissionschown -R $user.$group $bin_pathchown -R $user.$group $pid_path#Add environment variablesresult_grep_path="$( grep   "${bin_path}/bin" /etc/profile  )" || result_grep_path=""if [ -z "${result_grep_path}" ]thenecho "export PATH=\$PATH:${bin_path}/bin:${bin_path}/lib" >> /etc/profile ;source /etc/profileelseecho "${bin_path}/bin already exists in /etc/profile" >> $install_logfi#mysql cnfif [ -f $cnf_file ];thenecho "$cnf_file exists"echo "move $cnf_file to $cnf_file`date '+%Y%m%d%H%M%S'` "  >> $install_logmv $cnf_file $cnf_file`date "+%Y%m%d%H%M%S"`ficat > /etc/my.cnf << EOF[mysql]socket=${socket}[mysqld]server_id=${serverid}user=mysqlsocket=${socket}port=${port}basedir=${bin_path}datadir=${data_path}log_bin=${binlog_path}/mysql01_bin.loglog_error=${errlog_path}/mysql_err.logslow_query_log = on long_query_time = 2  slow_query_log_file = ${slowlog_path}/mysql_slow.logEOFmysqld --defaults-file=/etc/my.cnf --user=mysql --initialize if [ $? -eq 0 ];thenecho "mysql initialize success ">> $install_logelseecho "mysql initialize failed ">> $install_logexit 0ficp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql.serverchmod +x /etc/rc.d/init.d/mysql.serverchkconfig --add mysql.serverchkconfig --list mysql.server >> $install_logsudo -u mysql service mysql.server start if [ $? -eq 0 ];thenecho "mysql install && start success ">> $install_logelseecho "mysql install or start failed">> $install_logfiexit 0


0