本文就介绍了数据库5.7在Linux系统下一键安装的相关内容
前言
例如:随着人工智能和大数据的不断发展,数据库的这门技术也越来越重要,很多人都开启了数据库技术学习,本文就介绍了数据库5.7在Linux系统下一键安装的相关内容。
提示:以下是本篇文章正文内容,下面案例可供参考
一、前置准备
1. 卸载旧版MySQL
查看rpm包 rpm -qa|grep mysql
若有可用 rpm -e 卸载
2. 查找mysql残留包,有则删除,没有则忽略
find / -name mysql
3.下载二进制安装包并解压
链接:https://pan.baidu.com/s/1jGMOpKniY4McQZdFau4Dkg
提取码:oa10
解压数据库安装包
tar -zxvf mysql-5.7.37-el7-x86_64.tar.gz mysql-5.7.37/
截图如下:
二、写自动化脚本
1.创建一个install.sh脚本文件:
vim install.sh 创建自动化脚本
2.自动化脚本代码:
#!/bin/bash
echo "---------Start checking net-tools.---------"
ns=$(rpm -qa |grep net-tools)
if [ -z "$ns" ];then
echo "net-tools is not installed, please install net-tools first!"
exit 1
fi
cd $(dirname $0)
basedir=`pwd`
line=`netstat -ntpl|grep mysql|grep -v mysql-proxy|wc -l`
if [ ! $line -eq 0 ];then
echo "There is a mysql server running now! Please make sure it!"
exit 1
fi
echo "please input the mysql service port(default is 3306)":
read mysqlport
if [ -z $mysqlport ];then
mysqlport=3306
fi
portline=`netstat -ntpl|grep :$mysqlport|wc -l`
if [ ! $portline -eq 0 ];then
echo "the port $mysqlport is in use! Please make sure it!"
exit 1
fi
mysqlmem=$(echo `free -g|grep Mem|awk '{printf("%d \n",$2 * 0.8)}'`)
echo "please input the mysql root password(default is inspur135?!)":
read mysqlrootpwd
if [ -z $mysqlrootpwd ];then
mysqlrootpwd=inspur135?!
fi
echo "this mysql is master or slave (master/slave)(default is master)":
read mysql_type
if [ -z $mysql_type ];then
mysql_type=master
fi
if [ $mysql_type = master ];then
echo "please input the slave-server's number(e.g.:0 or 1 or 2)(default is 0)":
read slave_num
if [ -z $slave_num ];then
slave_num=0
fi
for ((i=0;$i<slave_num;i++)); do
echo "please input the slave-server[$[$i+1]] ip":
read slave_ip[$i]
done
elif [ $mysql_type = slave ];then
echo please input the master ip:
read masterip
echo "please input the master port(default is 3306)":
read masterport
if [ -z $masterport ];then
masterport=3306
fi
else
echo "what you input about mysql type is invalid!"
exit 1
fi
echo "======================Installation start======================="
if [ -e /etc/my.cnf ]; then
mv /etc/my.cnf /etc/my.cnf.back
fi
groupadd mysql
useradd -r -g mysql -s /sbin/false mysql
mkdir data tmp run
chown -R mysql:mysql $basedir
chmod 777 /tmp
cp ./support-files/my.cnf.example ./my.cnf
serverid=`ifconfig |grep -w inet|grep -v 127.0.0.1|awk '{print $2}'|awk -F. '{print $4}'|head -n 1`
sed -i 's#basedir_conf#'$basedir'#g' ./my.cnf
sed -i 's#mysqlport_conf#'$mysqlport'#g' ./my.cnf
sed -i 's#serverid_conf#'$serverid'#g' ./my.cnf
sed -i 's#mysql_mem#'$mysqlmem'#g' ./my.cnf
./bin/mysqld \
--defaults-file=$basedir/my.cnf \
--initialize-insecure \
--user=mysql \
--basedir=$basedir \
--datadir=$basedir/data
./bin/mysql_ssl_rsa_setup \
--defaults-file=$basedir/my.cnf \
--user=mysql \
--basedir=$basedir \
--datadir=$basedir/data
cp ./support-files/mysql.server.example ./mysql.server
sed -i 's#basedir_conf#'$basedir'#g' ./mysql.server
ln -s $basedir/mysql.server /etc/init.d/mysql
$basedir/mysql.server start
ln -s $basedir/bin/mysql /usr/bin/mysql
ln -s $basedir/bin/mysqldump /usr/bin/mysqldump
ln -s $basedir/bin/mysqladmin /usr/bin/mysqladmin
$basedir/bin/mysqladmin -hlocalhost -P$mysqlport -uroot password $mysqlrootpwd
if [ $mysql_type = master ];then
cat > ./tmp/mysql_sec_script<<EOF
use mysql;
update user set authentication_string=password('$mysqlrootpwd') where user='root';
grant all privileges on *.* to root@'%' identified by '$mysqlrootpwd';
flush privileges;
EOF
for ((i=0;$i<$slave_num;i++)); do
cat >> ./tmp/mysql_sec_script <<EOF
grant replication slave on *.* to slave@${slave_ip[$i]} identified by 'inspur135?!';
flush privileges;
EOF
done
cat >> ./tmp/mysql_sec_script <<EOF
reset master;
EOF
$basedir/bin/mysql -hlocalhost -P$mysqlport -uroot -p$mysqlrootpwd < ./tmp/mysql_sec_script
rm -f ./tmp/mysql_sec_script
else
cat > ./tmp/mysql_sec_script<<EOF
use mysql;
update user set authentication_string=password('$mysqlrootpwd') where user='root';
grant all privileges on *.* to root@'%' identified by '$mysqlrootpwd';
flush privileges;
reset master;
change master to master_host='$masterip',master_port=$masterport,master_user='slave',master_password='inspur135?!';
start slave;
EOF
$basedir/bin/mysql -hlocalhost -P$mysqlport -uroot -p$mysqlrootpwd < ./tmp/mysql_sec_script
rm -f ./tmp/mysql_sec_script
fi
$basedir/mysql.server restart
echo "======================Installation complete and mysql is running now!======================="
3.执行脚本文件
直接敲回车即可,简直不要太爽。就连最后的程序服务也给大家启动啦,直接使用即可!
注意:自动化脚本,默认端口3306,默认账号root,默认密码:inspur135?!
三、检查mysql启动情况并连接
1.查看mysql启动情况
ps -ef | gerp mysql 查询mysql进程情况
2.navicat连接使用
总结
例如:以上就是今天要讲的内容,本文仅仅简单介绍了在Linux系统下面自动化安装Mysql的方法,如果大家用得到,觉得可以请一键三连。