shell脚本-自动安装mysql源码包

   学习linux有段时间了,一直没有写技术博客的习惯,但还是尽量让自己多写写自己学到的一些关于linux方面的东西吧,最近在双星葛老师的指导下开始学习mysql数据库,自己整理笔记,并用shell脚本实现mysql源码包(mysql-5.6.11.tar.gz)的  全自动安装,代码写的一般,但是能满足需求,如安装有什么问题可以互相交流一些,下面进入正题:

   首先整理一下自己手动安装时的一些步骤:


安装和配置mysql的源码包

软件包版本:mysql-5.6.11.tar.gz


1.安装源码包所需要的前提软件包
需要的软件包:cmake make gcc gcc-c++ ncurses-devel mysql

yum install cmake make gcc gcc-c++ ncurses-devel mysql -y


2.解压源码包并进行编译安装


[root@localhost ~]# tar xf mysql-5.6.11.tar.gz
[root@localhost ~]# cd mysql-5.6.11
[root@localhost mysql-5.6.11]# cmake \
-DCMAKE_INSTALL_PREFIX:PATH=/usr/local/mysql12 \
-DMYSQL_DATADIR:PATH=/database \
-DWITH_DEBUG:BOOL=on \
-DWITH_EXTRA_CHARSETS:STRING=all \
-DWITH_INNOBASE_STORAGE_ENGINE:BOOL=on \
-DWITH_PARTITION_STORAGE_ENGINE:BOOL=on
[root@localhost mysql-5.6.11]# make ; make install

cmake make就会调用gcc、gcc-c++编译器来编译源代码文件
make install 安装到/usr/local/mysql12

3.创建并修改配置文件

进入到安装目录,创建配置文件my.cnf


[root@localhost mysql-5.6.11]# cd /usr/local/mysql12
[root@localhost mysql12]# mkdir etc
[root@localhost mysql12]# vim etc/my.cnf
[mysqld]
basedir=/usr/local/mysql12
datadir=/database
socket=/var/run/mysqld/mysql12.sock
pid-file=/var/run/mysqld/mysql12.pid
port=3308
user=mysql
[mysqld_safe]
log-error=/var/log/mysqld/mysql12-error.log


4.创建mysql组和用户,并保证用户uid27 gid27

查看本机是否有mysql用户

[root@localhost mysql12]#cat /etc/passwd |grep mysql
mysql:x:27:27:MySQL Server:/var/lib/mysql:/bin/bash

没有的话则进行创建,命令如下

[root@localhost mysql12]# groupadd mysql -g 27
[root@localhost mysql12]# useradd mysql -u 27 -c "Mysql Server" -d /var/lib/mysql  -s /sbin/nologin -g mysql


5.创建所需文件夹并修改所有者为mysql


[root@localhost mysql12]# mkdir /database
[root@localhost mysql12]# mkdir /var/run/mysqld/
[root@localhost mysql12]# mkdir /var/log/mysqld/
[root@localhost mysql12]# chown mysql:mysql /database /var/run/mysqld/ /var/log/mysqld/ /usr/local/mysql12 -R


6.初始化数据

进入源码包解压後的文件夹下的script目录执行mysql_install_db脚本进行初始化

[root@localhost mysql12]# cd ~/mysql-5.6.11/scripts/
[root@localhost scripts]# chmod +x mysql_install_db
[root@localhost scripts]# ./mysql_install_db --datadir=/database/ --basedir=/usr/local/mysql12/ --user=mysql


7.定义启动脚本的位置并修改脚本

复制解压目录下的support-files目录中的mysql.server到/etc/rc.d/init.d/目录下并将其重命名为mysql56,并进行如下修改作为启动脚本。

[root@localhost scripts]# cd ~/mysql-5.6.11/support-files/
[root@localhost support-files]# chmod +x mysql.server
[root@localhost support-files]# cp mysql.server /etc/rc.d/init.d/mysql12
[root@localhost support-files]# vim  /etc/rc.d/init.d/mysql12
修改 conf=/etc/my.cnf 为conf=/usr/local/mysql12/etc/my.cnf


8.启动mysql

查看系统中是否有mysql的进程

[root@localhost support-files]# netstat -ntaup | grep "mysql"
[root@localhost support-files]# ps aux | grep "mysql"

如果有则将其进程结束,命令如下

[root@localhost support-files]# pkill -9 mysql

启动刚安装好的mysql

[root@localhost support-files]# service mysql12 start
[root@localhost support-files]# mysql -S/var/run/mysqld/mysql12.sock

出现如下提示则说明mysql源码包安装成功并可以使用

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.11-debug Source distribution

Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>


以下为shell脚本安装的内容:


#!/bin/bash
#########################################
#   mysql 源码包安装脚本       #
#           --by sweet  #
#########################################
#每次安装只需修改mysql的值即可
mysql=mysql12
setup_path=/root
install_path=/usr/local/$mysql
#结束所有mysql进程
pkill -9 mysql
#1.安装所需要的前提软件包
yum install cmake make gcc gcc-c++ ncurses-devel -y &>/dev/null
#2.解压源码包并进行编译安装
if [ -d $setup_path/mysql-5.6.11 ];then
    rm -rf $setup_path/mysql-5.6.11
fi
                                      
cd $setup_path
tar xf mysql-5.6.11.tar.gz
cd  mysql-5.6.11
cmake  -DCMAKE_INSTALL_PREFIX:PATH=$install_path  -DMYSQL_DATADIR:PATH=/database -DWITH_DEBUG:BOOL=on -DWITH_EXTRA_CHARSETS:STRING=all  -DWITH_INNOBASE_STORAGE_ENGINE:BOOL=on  -DWITH_PARTITION_STORAGE_ENGINE:BOOL=on &>/dev/null
make ; make install
#3.创建并修改配置文件
mkdir $install_path/etc -p
cat << EOF >$install_path/etc/my.cnf
[mysqld]
basedir=/usr/local/$mysql
datadir=/database
socket=/var/run/mysqld/$mysql.sock
pid-file=/var/run/mysqld/$mysql.pid
#安装不同版本可修改port号
port=3308
user=mysql
[mysqld_safe]
log-error=/var/log/mysqld/$mysql-error.log
EOF
#4.创建mysql组和用户,并保证用户uid27 gid27
user=$(cat /etc/passwd |grep "mysql")
if [[ -z $user ]];then
groupadd mysql -g 27
useradd mysql -u 27 -c "Mysql Server" -d /var/lib/mysql  -s /sbin/nologin -g mysql
fi
#5.创建所需文件夹并修改所有者为mysql
mkdir -p /database
mkdir -p /var/run/mysqld/
mkdir -p /var/log/mysqld/
chown mysql:mysql /database /var/run/mysqld/ /var/log/mysqld/ /usr/local/$mysql -R
#6.初始化数据
cd $setup_path/mysql-5.6.11/scripts/
chmod +x mysql_install_db
./mysql_install_db --datadir=/database/ --basedir=/usr/local/$mysql/ --user=mysql
#7.定义启动脚本的位置并修改脚本
cd $setup_path/mysql-5.6.11/support-files/
chmod +x mysql.server
cp mysql.server /etc/rc.d/init.d/$mysql
sed -i "s/conf=/&\/usr\/local\/$mysql/" /etc/rc.d/init.d/$mysql
#8.启动mysql
service $mysql start
mysql -S/var/run/mysqld/$mysql.sock

如需要在一台主机重复安装,只需要将脚本中的mysql变量进行修改即可。

附件为shell脚本




你可能感兴趣的:(shell脚本-自动安装mysql源码包)