天天看點

linux centos mysql-mariadb10.5 shell一鍵安裝腳本

#!/bin/bash
repo="/etc/yum.repos.d/epel-7.repo"
if [ ! -e $repo ]; then                 # -e 判斷檔案存在;參考:Shell if 條件判斷檔案或目錄
yum install -y wget
cd /etc/yum.repos.d/
mkdir bak
mv * bak
wget http://mirrors.aliyun.com/repo/Centos-7.repo
wget http://mirrors.aliyun.com/repo/epel-7.repo
                                        # 移動兩個檔案到 目錄
#wget http://mirrors.163.com/.help/CentOS7-Base-163.repo
#sed -i  's/$releasever/7/g' /etc/yum.repos.d/CentOS-Base.repo
                                        # yum源沒有對應版本包比對;修改yum為centos7
yum clean all                           # 清除原有緩存
yum makecache                           # 生成新的緩存
                                        # make cache [meɪk]  [kaʃ]  制作 緩存{ka xi}
yum  list                               # 校驗yum
cd
else 
echo "yum已經切換"
fi
############################################  azmariadb  ######################################################
azmariadb(){
lsphp=(`find / -name "mysql.server" | sed "s/ //g"`) # shell 小括号屬性是 定義數組變量 lsnginx   它的值是一條指令的執行結果
echo "
nginx 配置檔案總個數為:${#lsphp[*]}
列出 nginx 配置檔案"    ${lsphp[*]}             # [*]  星号列出所有元素, 星号改成1 為指定角标1,含義為第二個元素,預設從0開始
if [ ${#lsphp[*]} -lt 1 ]                       # -lt 小于; $ 為空,! 非運算; $? 判斷上一條指令執行狀态,0正常;  參考:linux shell $$,$! 等的特殊用法
then
echo "配置檔案為空,正在安裝mariadb"
yum install -y openssl openssl-devel ncurses ncurses-devel bison bison-devel jemalloc jemalloc-devel centos-release-scl devtoolset-7  gcc gcc-c++
                                                # 安裝gcc等依賴
yum remove cmake -y
ln -s /usr/local/bin/cmake /usr/bin/            # 移除舊版本cmake
cd
wget work.eisc.cn/ruanjian/rpm/lnmp/cmake-3.19.1.tar.gz
tar -xvf cmake-3.19.1.tar.gz                            # cmake版本要高于3.10
cd cmake-3.19.1 
rm -f CMakeCache.txt                                    # 清除曆史編譯
./bootstrap && gmake && gmake install
/usr/local/bin/cmake --version                  # 檢視編譯後的cmake版本
# wget https://github.com/libevent/libevent/releases/download/release-2.1.10-stable/libevent-2.1.10-stable.tar.gz
                                                                        # 安裝libevent
cd
wget work.eisc.cn/ruanjian/rpm/lnmp/libevent-2.1.10-stable.tar.gz
tar -xvf libevent-2.1.10-stable.tar.gz 
cd tar -xvf libevent-2.1.10-stable
./configure --prefix=/usr --disable-static && make && make install
yum install -y centos-release-scl devtoolset-7 # 安裝C++高版本
gcc --version
scl enable devtoolset-7 bash & echo " 單個 & 符号并列執行 後面的指令"                               
### 使用新版本 gcc 重新開機失效 ###
gcc --version 
echo "後面的指令沒有scl 那樣要求中斷,是以得到了繼續執行"
cd  
wget http://work.eisc.cn/ruanjian/rpm/lnmp/mariadb-10.5.8.tar.gz
# https://downloads.mariadb.org/                                # 官網鏡像
tar -zxvf mariadb-10.5.8.tar.gz
cd mariadb-10.5.8
sed -i "s/MYSQL_VERSION_MAJOR.*/MYSQL_VERSION_MAJOR=101/g" VERSION
                                                                # 修改版本号
sed -i "s/MYSQL_VERSION_MINOR.*/MYSQL_VERSION_MINOR=42/g" VERSION 
sed -i "s/MYSQL_VERSION_PATCH.*/MYSQL_VERSION_PATCH=63/g" VERSION 
groupadd -g 41 mysql && useradd -c "MySQL Server" -d /srv/mysql -g mysql -s /bin/false -u 41 mysql
                                                                                # 為mariadb建立使用者組和使用者
# sed -i "s@data/test@\${INSTALL_MYSQLTESTDIR}@g" sql/CMakeLists.txt  && mkdir build && cd    build
cmake -DCMAKE_BUILD_TYPE=Release                      \
      -DCMAKE_INSTALL_PREFIX=/usr                     \
      -DINSTALL_DOCDIR=share/doc/mariadb-10.5.8       \
      -DINSTALL_DOCREADMEDIR=share/doc/mariadb-10.5.8 \
      -DINSTALL_MANDIR=share/man                      \
      -DINSTALL_MYSQLSHAREDIR=share/mysql             \
      -DINSTALL_MYSQLTESTDIR=share/mysql/test         \
      -DINSTALL_PLUGINDIR=lib/mysql/plugin            \
      -DINSTALL_SBINDIR=sbin                          \
      -DINSTALL_SCRIPTDIR=bin                         \
      -DINSTALL_SQLBENCHDIR=share/mysql/bench         \
      -DINSTALL_SUPPORTFILESDIR=share/mysql           \
      -DMYSQL_DATADIR=/srv/mysql                      \
      -DMYSQL_UNIX_ADDR=/run/mysqld/mysqld.sock       \
      -DWITH_EXTRA_CHARSETS=complex                   \
      -DWITH_EMBEDDED_SERVER=ON                       \
      -DSKIP_TESTS=ON                                 \
      -DTOKUDB_OK=0                       
make && make install
:<<!                                                            # 多行注釋
-DCMAKE_INSTALL_PREFIX=                                 指向mysql安裝目錄
-DINSTALL_SBINDIR=sbin                          指向可執行檔案目錄(prefix/sbin)
-DMYSQL_DATADIR=/var/lib/mysql          指向mysql資料檔案目錄(/var/lib/mysql)
-DSYSCONFDIR=/etc/mysql                                 指向mysql配置檔案目錄(/etc/mysql)
-DINSTALL_PLUGINDIR=lib/mysql/plugin    指向插件目(prefix/lib/mysql/plugin)
-DINSTALL_MANDIR=share/man                      指向man文檔目錄(prefix/share/man)
-DINSTALL_SHAREDIR=share                        指向aclocal/mysql.m4安裝目錄(prefix/share)
-DINSTALL_LIBDIR=lib/mysql                      指向對象代碼庫目錄(prefix/lib/mysql)
-DINSTALL_INCLUDEDIR=include/mysql      指向頭檔案目錄(prefix/include/mysql)
-DINSTALL_INFODIR=share/info                    指向info文檔存放目錄(prefix/share/info)
prefix官方推薦設為/usr
!
install -v -dm 755 /etc/mysql                                   # 建立檔案/etc/mysql/my.cnf
cat > /etc/mysql/my.cnf << "EOF"
# Begin /etc/mysql/my.cnf
# The following options will be passed to all MySQL clients
[client]
#password       = your_password
port            = 3306
socket          = /run/mysqld/mysqld.sock
# The MySQL server
[mysqld]
port            = 3306
socket          = /run/mysqld/mysqld.sock
datadir         = /srv/mysql
skip-external-locking
key_buffer_size = 16M
max_allowed_packet = 1M
sort_buffer_size = 512K
net_buffer_length = 16K
myisam_sort_buffer_size = 8M
# Don't listen on a TCP/IP port at all.
#skip-networking
# 注意此處 skip 打開會導緻 3306端口異常
# required unique id between 1 and 2^32 - 1
server-id       = 1
# Uncomment the following if you are using BDB tables
#bdb_cache_size = 4M
#bdb_max_lock = 10000
# InnoDB tables are now used by default
innodb_data_home_dir = /srv/mysql
innodb_log_group_home_dir = /srv/mysql
# All the innodb_xxx values below are the default ones:
innodb_data_file_path = ibdata1:12M:autoextend
# You can set .._buffer_pool_size up to 50 - 80 %
# of RAM but beware of setting memory usage too high
innodb_buffer_pool_size = 128M
innodb_log_file_size = 48M
innodb_log_buffer_size = 16M
innodb_flush_log_at_trx_commit = 1
innodb_lock_wait_timeout = 50
[mysqldump]
quick
max_allowed_packet = 16M
[mysql]
no-auto-rehash
# Remove the next comment character if you are not familiar with SQL
#safe-updates
[isamchk]
key_buffer = 20M
sort_buffer_size = 20M
read_buffer = 2M
write_buffer = 2M
[myisamchk]
key_buffer_size = 20M
sort_buffer_size = 20M
read_buffer = 2M
write_buffer = 2M
[mysqlhotcopy]
interactive-timeout
# End /etc/mysql/my.cnf
EOF
:<<!
cd /usr/bin
mv mysql_install_db mysql_install_db_bak
cp /root/mysql_install_db /usr/bin
!
mysql_install_db --basedir=/usr --datadir=/srv/mysql --user=mysql &&
chown -R mysql:mysql /srv/mysql                         # 初始化資料庫以及修改所有者權限
cp /root/mariadb-10.5.8/support-files/mysql.server /etc/init.d/mysqld
 chmod +x /etc/init.d/mysqld
 # update-rc.d mysqld defaults
 chkconfig --add mysqld
 /etc/init.d/mysqld restart
 systemctl status mysqld
'/usr/bin/mysqladmin' -u root password 'root'           # 為mysql 設定密碼
#mysql                                                  # 登入,預設沒有密碼
mysql -e "set password = password('WWWeisccn')"         # 為root 使用者設定密碼
mysql -uroot -pWWWeisccn -e "create database eisc character set utf8 collate utf8_bin;"
                                                        # 登入資料庫并且建立資料庫名字為
##################################################################################################################################
####  防火牆配置  ####
systemctl restart firewalld
firewall-cmd --permanent --add-port=3306/tcp            # 開放端口 3306
firewall-cmd --permanent --add-port=9000/tcp
firewall-cmd --permanent --add-port=80/tcp
systemctl restart firewalld                             # 重新開機防火牆
firewall-cmd --zone=public --list-ports                 # 列出防火牆已經開放的端口
netstat -ntlp                                           # 監聽運作中的端口運用
####  自動啟動配置  ####
#說明:編譯安裝的運用reboot 後不會自動啟動,是以需要加入開機啟動配置檔案 /etc/rc.d/rc.local
echo "
source /etc/profile
bash /www/server/StartLnmp.sh
" >> /etc/rc.d/rc.local
chmod +x /etc/rc.d/rc.local

echo "
nginx
systemctl restart php-fpm
/etc/init.d/mysqld restart
echo '   LNMP 啟動時間 ==========>> `date`' >> /root/LNMPlog.txt
" >> /www/server/StartLnmp.sh
##################################################################################################################################
else
echo "資料庫已經安裝"
fi
}
azmariadb