JDK&MYSQL数据库安装目录

  • 一、解压安装包
  • 二、配置环境变量
  • 三、yum安装
  • 四、rpm安装mysql
  • 五、快捷安装MySQL-配置脚本安装
  • 5.1 编辑脚本
  • 5.2 执行脚本并测试结果

一、解压安装包

       1.首先我在/opt文件目录下新建了两个文件夹,download和software。之后我将下载好的JDK安装包通过Xftp放到了刚刚新建的download文件夹里。

mysql虚拟化 虚拟机mysql安装配置教程_jdk1.8


mysql虚拟化 虚拟机mysql安装配置教程_mysql_02

       2.JDK安装包准备好之后,我将JDK安装包文件解压到了/opt/software文件夹目录下

mysql虚拟化 虚拟机mysql安装配置教程_安装包_03

       3.解压好之后可以到目标目录下查看文件,这里我重命名了jdk文件

mysql虚拟化 虚拟机mysql安装配置教程_安装包_04


二、配置环境变量

       1.进入配置文件

mysql虚拟化 虚拟机mysql安装配置教程_mysql虚拟化_05


       快捷键shift+g直接来到文档底部,按“i”进入编辑模式,把环境变量如下图编辑在内,编辑好之后esc退出编辑模式,:x保存退出。

mysql虚拟化 虚拟机mysql安装配置教程_jdk1.8_06

#JAVA_HOME
JAVA_HOME=/opt/software/jdk180
export PATH=$JAVA_HOME/bin:$PATH
export CLASS_PATH=.:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar

       可以验证下我们的环境变量

mysql虚拟化 虚拟机mysql安装配置教程_MySQL_07

       同样的可以验证下我们的java version,到这里我们的jdk环境变量配置完成

mysql虚拟化 虚拟机mysql安装配置教程_MySQL_08


三、yum安装

       首先更新我们的yum

mysql虚拟化 虚拟机mysql安装配置教程_mysql虚拟化_09

       这里我们安装下wget,后面会后用到这个

mysql虚拟化 虚拟机mysql安装配置教程_jdk1.8_10


四、rpm安装mysql

       安装之前,查看是否存在mysql的旧版本

mysql虚拟化 虚拟机mysql安装配置教程_安装包_11

       如果存在,需要删除再安装:yum -y remove xxxx,之后我们可以通过wget直接下载mysql的包

mysql虚拟化 虚拟机mysql安装配置教程_mysql_12

mysql虚拟化 虚拟机mysql安装配置教程_jdk1.8_13

       安装mysql服务

mysql虚拟化 虚拟机mysql安装配置教程_mysql虚拟化_14

       安装完之后启动mysql并查看状态

mysql虚拟化 虚拟机mysql安装配置教程_安装包_15

       进入mysql并使用

mysql虚拟化 虚拟机mysql安装配置教程_安装包_16

       修改密码 并授权

mysql虚拟化 虚拟机mysql安装配置教程_jdk1.8_17

       重启服务重新进入即可

mysql虚拟化 虚拟机mysql安装配置教程_MySQL_18


五、快捷安装MySQL-配置脚本安装

5.1 编辑脚本

  • 请在/root目录下运行安装的脚本
  • UNINSTALL MARIADB =>卸载mariadb
  • UNINSTALL PAST MYSQL =>卸载旧版本MySQL
  • YUM UPDATE =>更新yum
  • SEARCH WGET AND ISTALL WGET =>安装wget
  • WGET MYSQL PACKAGE =>wget获取MySQL安装包
  • RMP INSTALL MySQL =>安装MySQL
  • INSTALL MYSQL-SERVER I=>安装MySQL服务
  • MODIFY PASSWORD =>修改root用户密码
  • CHINESE GARBLED PROBLEM =>修改MySQL字符集,解决中文乱码问题
#!/bin/bash
#------------------UNINSTALL MARIADB------------------------
echo "First,we need to unistall mariadb"
var=`rpm -qa | grep mariadb`
if [[ $var =~ ^mariadb ]]
then
        echo -e Find "\e[1;31m $var \e[0m"
        `rpm -e --nodeps $var`
        rst=`rpm -qa | grep mariadb`
        if [[ $rst =~ "" ]]
        then
                echo "Mariadb uninstall successfully"
        fi
else
        echo "Not find mariadb,just direct install mysql"
fi

#-------------------------UNINSTALL PAST MYSQL---------------------------
echo
echo "Second,we need to check that you pc has past mysql and uninstall"
echo "Start to uninstall mysql server"
VAR=`rpm -qa|grep -i mysql`
VAR=($VAR)
for server in ${VAR[@]}
do
    echo -e uninstall: "\e[1;31m $VAR \e[0m"
    `rpm -e --nodeps $server`
done
VAR=`rpm -qa|grep -i mysql`
if [ ${#VAR} -eq 0 ]
then
    echo "All mysql server were uninstall!"
else
    echo "Find remains mysql server,please reuninstall remain mysql server"
    exit 1
fi
echo
echo "Next,we need to remove remain mysql properties and denpencies"
VAR=`rm -rf /var/lib/mysql`
echo "Mysql propertied and denpencies were all droped"
echo
echo -e Last,we had to remove: "\e[1;31m my.cnf \e[0m"
VAR=`rm -rf /etc/my.cnf`
echo -e File: "\e[1;31m $VAR \e[0m"  has removed!
echo "Your pc with all about mysql has remove, we start to install mysql"

#-------------------------YUM UPDATE---------------------------------------
echo
echo "start to update yum ..."
`yum -y update>/root/test.log 2>&1`
VAR=`awk -F '!'  'END{print $1}' /root/test.log`
if [[ $VAR =~ ^Complete ]]
then
    echo "update yum success!"
elif [[ $VAR =~ ^No ]]
then
    echo "yum has been the newest!"
else
    echo "update yum failed,please reinstall..."
        exit 1
fi

#------------------SEARCH WGET AND INSTALL WGET--------------------
echo
VAR=`yum search wget|grep wget.|awk -F' ' '{print $1}'`
VAR=${VAR##*=}
echo -e yum search wget find: "\e[1;31m $VAR \e[0m"
echo -e start to install: "\e[1;31m $VAR \e[0m"
VAR="yum -y install $VAR>/root/test.log 2>&1"
`eval $VAR`
#验证wget是否安装成功
VAR=`awk -F '!'  'END{print $1}' /root/test.log`
VAR2=`cat /root/test.log |grep wget.|awk -F' ' '{print $0}'`
VAR2=`echo $var|grep "already installed"`
if [[ $VAR =~ ^Complete ]]
then
    echo "install wget success"
elif [[ $VAR2 != NULL ]]
then
    echo "wget already install"
else
    echo "install wget failed,please reinstall..."
        exit 1
fi

#------------------WGET MYSQL PACKAGE-----------------------
echo
echo "Checking past version of mysql package"
VAR=`ls|grep mysql.*`
VAR=($VAR)
for package in ${VAR[@]}
do
    `rm -rf $package`
done
echo "Old version of package, they all been removed"
echo
echo "Start to download mysql package"
VAR=http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm
`eval "wget $VAR>/root/test.log 2>&1"`
#验证wget获取MySQL package结果
ls>test.log
VAR=`cat test.log`
VAR=($VAR)
COUNT=0
for var in ${VAR[@]}
do
    if [[ $var =~ ^mysql ]]
    then
        echo "Download package $var success!"
        COUNT=0
        break
    else
        ((COUNT++))
    fi
done

if [ $COUNT -ne 0 ]
then
    echo "Download package failed .... please redownload package!"
    exit 1
fi
echo

#----------------RMP INSTALL-----------------------
echo "Start to install mysql"
VAR=`ls ./|grep mysql*`
OPER=`eval "rpm -ivh $VAR>/root/test.log 2>&1"`
#验证MySQL安装是否成功
VAR=`awk -F' ' 'END{print $1}' /root/test.log`
if [[ $VAR =~ ^mysql ]]
then
    echo "Install mysql successfully"
else
    echo "Install mysql failed,please reinstall..."
    echo "Install log in /root/test.log,please check it and find reason"
        exit 1
fi
echo

#---------------MYSQL-SERVER INSTALL-----------------
echo "start to install mysql server..."
VAR="yum -y install mysql-server>/root/test.log 2>&1"
`eval $VAR`
#验证MySQL服务是否安装成功
VAR=`rpm -qa|grep -i mysql`
VAR=($VAR)
if [[ ${#VAR[*]} -ge 4 ]]
then
    echo "mysql server install success"
else
    echo "mysql server install fail"
    echo "Install log in /root/test.log,please check it and find reason"
        exit 1
fi

#------------------MODIFY PASSWORD------------------------------
echo
echo "Start to modify password"
#通过配置文件的方法跳过MySQL的登录检查,修改后要删掉次配置
`sed -i '/sql_mode=/i\\skip-grant-tables' /etc/my.cnf`
`systemctl restart mysql`
#modify password
`mysql -uroot -e "use mysql;update user set password=password('root') where user='root';flush privileges;grant all on *.* to root@'%' identified by 'root';"`
echo "User of 'root' password has changed into 'root'"
`sed -i '/skip-grant-tables/d' /etc/my.cnf`
echo "Modify password successfully"

#------------------CHINESE GARBLED PROBLEM------------------------
echo
echo "Start to modify mysql-my.cnf"
`echo "#mysql-setup" > /etc/my.cnf`
sen="[client]
default-character-set=utf8
[mysqld]
default-storage-engine=INNODB
character-set-server=utf8
collation-server=utf8_general_ci
[mysql]
default-character-set=utf8
"
lines=`echo $sen`
for line in $lines
do
    VAR=`eval "sed -i '/#mysql-setup/i\$line' /etc/my.cnf"`
done
#验证修改是否完成
VAR=`awk -F' ' 'END{print $1}' /etc/my.cnf`
if [[ $VAR =~ ^#mysql ]]
then
        echo "modify my.cnf success"
else
        echo "modify my.cnf failed,please check shell..."
        exit 1
fi
echo
echo "Restart mysql"
`systemctl restart mysql`
echo "Install mysql finished!"

5.2 执行脚本并测试结果

  • 脚本授权

mysql虚拟化 虚拟机mysql安装配置教程_MySQL_19


  • 执行脚本

mysql虚拟化 虚拟机mysql安装配置教程_mysql虚拟化_20


  • 测试连接mysql

mysql虚拟化 虚拟机mysql安装配置教程_mysql虚拟化_21


PS:如果有写错或者写的不好的地方,欢迎各位大佬在评论区留下宝贵的意见或者建议,敬上!

原创作者:wsjslient