一键安装mysql shell脚本

#!/bin/bash

#输入mysql压缩文件地址
fileName=$1;
-f是判断文件是否存在
if [ -f ${fileName} ]
then
    #解压mysql
    tar -zxvf ${fileName};
    echo 'mysql解压完成,正在删除/usr/local/mysql文件夹';
    rm -rf /usr/local/mysql;
    echo '删除文件夹完成,正在移动解压后的文件';
	# %%是除去后面.tar.gz的意思
    mv ${fileName%%.tar.gz} /usr/local/mysql;
    cd /usr/local/mysql;
else
    echo '请输入正确的文件';
fi
#如果系统缺少Data:Dumper模块需要打开下面命令
#yum -y install autoconf;

echo '移动完成,针对mysql增加用户';
groupadd mysql;
useradd -g mysql -M mysql;

echo '将文件存入到/keduox/mysql/data,并授权 ';
mkdir -p /keduox/mysql/data;
chown -R mysql.mysql /keduox/mysql/data;

echo '初始化mysql数据库 ';
yum install perl;
yum install -y perl-Module-Install.noarch;
/usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/keduox/mysql/data --user=mysql

echo '修改my.cnf文件';
#先定位到文件内容里面的mysqld,再定位到basedir,
sed -i '/mysqld/a\basedir = \/usr\/local\/mysql' /usr/local/mysql/my.cnf;
sed -i '/mysqld/a\datadir = \/keduox\/mysql\/data' /usr/local/mysql/my.cnf;
sed -i '/mysqld/a\port = 3306' /usr/local/mysql/my.cnf;


echo '设置自启动 ';
mkdir /var/log/mariadb;
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld;
chkconfig --add mysqld;
chkconfig mysqld on;

echo '启动';
/etc/init.d/mysqld start;

echo '设置密码 ';
ln -s /var/lib/mysql/mysql.sock /tmp/mysql.sock;
/usr/local/mysql/bin/mysqladmin -u root password '1234';
service mysqld restart;

echo '配置环境变量 ';
echo 'export MYSQL_HOME=/usr/local/mysql' >> /etc/profile;
echo 'export PATH=$PATH:$MYSQL_HOME/bin' >> /etc/profile;
source /etc/profile;

echo '删除mysql中user表除了%的所有用户';
mysql -uroot -p1234 -e 'DELETE FROM mysql.user WHERE Host <> "%" ';



 

你可能感兴趣的:(Mysql数据库,Linux,工具类)