您的位置:首页 > 数据库 > MariaDB

centos6与centos7自动化安装mariadb脚本

2017-10-12 10:20 411 查看
默认软件包在当前目录下。执行该脚本即可。交互式安装请参考http://amelie.blog.51cto.com/12850951/1971534。另请注意centos6与centos7的微妙差别哦。
#!/bin/bash
# ------------------------------------------
# Filename: mariadb.sh
# Revision: 1.0
# Date: 2017-10-11
# Author: zhangsan
# Email: 798761864@qq.com
# http://www.ardusty.com/ # boke://amelie.blog.51cto.com/
# Description:
# ------------------------------------------

#定义版本变量
ver=`cat /etc/redhat-release |grep -o [0-9]|head -n1`

#初始化安装环境
[ $ver -eq 7 ] && { yum remove mariadb -y &> /dev/null; }
[ $ver -eq 6 ] && { yum remove mysql -y &> /dev/null; }
pkill -9 -U mysql &> /dev/null
userdel -r mysql &> /dev/null

#创建系统用户
useradd -d /app/mysqldb -r -m -s /sbin/nologin mysql

#解压缩到指定文件夹,创建软链接
file=`cd;ls mariadb-*`
cd;tar xf $file -C /usr/local/ &> /dev/null
cd /usr/local
file1=`ls mariadb-* -d`
rm -rf mysql &> /dev/null
ln -s $file1 mysql

#配置配置文件
mkdir /etc/mysql &> /dev/null
cp /usr/local/mysql/support-files/my-huge.cnf /etc/mysql/my.cnf
sed -r -i "/^\[mysqld\]/s/.*/[mysqld] \ndatadir = \/app\/mysqldb \ninnodb_file_per_table = on \nskip_name_resolve = on \n/" /etc/mysql/my.cnf
cd /usr/local/mysql/
./scripts/mysql_install_db --user=mysql --datadir=/app/mysqldb &> /dev/null

#启动服务
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
chkconfig --add mysqld
[ $ver -eq 7 ] && { mkdir /var/log/mariadb &> /dev/null; touch /var/log/mariadb/mariadb.log; chown mysql /var/log/mariadb/mariadb.log; chown mysql /var/log/mariadb; }
[ $ver -eq 6 ] && { touch /var/log/mysqld.log; chown mysql /var/log/mysqld.log; }
service mysqld start &> /dev/null

#PATH变量及安全环境初始化
echo 'PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
echo -e "\ny\nwww.ardusty.com\nwww.ardusty.com\ny\nn\nn\n\y\n"| /usr/local/mysql/bin/mysql_secure_installation &> /dev/null

unset ver file file1


脚本执行结束后,需要执行. /etc/profile.d/mysql.sh重读下配置文件。阿拉有尝试把这条命令添加到脚本里,但因为脚本执行是子进程,所以脚本结束后,PATH变量的值其实还没有更新。记得执行. /etc/profile.d/mysql.sh哦。否则会出现以下报错哦。
[root@CentOS6 ~]# mysql
-bash: mysql: command not found

当然,重读配置文件后我们的基础环境就算配置好了。再执行mysql就不能匿名登录的,正确示例如下。
[root@CentOS6 ~]# mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: N
[root@CentOS6 ~]# mysql -uroot -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 5.5.57-MariaDB MariaDB Server
Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> quit
Bye
阿拉给默认密码设置为www.ardusty.com了,有需要的可以直接修改脚本里倒数第二行的代码。当然也可以执行数据库命令修改密码。那又是另一个大知识了。www.ardusty.com是阿拉的个人网站,目前正在建设中。。请先期待一下吧。哈哈。好了,这次就是这样了。加纳。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Linux