#!/bin/bash
src_dir=$(pwd)
cd $src_dir
rpm -ivh MySQL-shared-compat-5.6.21-1.el6.x86_64.rpm
yum remove mysql-libs -y
yum install libaio -y
rpm -ivh MySQL-devel-5.6.21-1.el6.x86_64.rpm
rpm -ivh MySQL-shared-5.6.21-1.el6.x86_64.rpm
rpm -ivh MySQL-client-5.6.21-1.el6.x86_64.rpm
rpm -ivh MySQL-test-5.6.21-1.el6.x86_64.rpm
rpm -ivh MySQL-server-5.6.21-1.el6.x86_64.rpm
#service mysql start && echo "mysql5.6.21 has installes sucess!"
#mysql_root_pwd=`gawk -F : '{ print $4 }' /root/.mysql_secret`
#echo "A random root password has been set. You will find it in '/root/.mysql_secret'."
#echo "The random root password was:'${mysql_root_pwd// }'"
service mysql stop
[ -d /data1/mysqldb/data ] || mkdir -p /data1/mysqldb/data
[ -d /data1/mysqldb/log ] || mkdir -p /data1/mysqldb/log
chown mysql.mysql /data1/mysqldb -R
cd $src_dir
cp ${src_dir}/my.cnf /etc
cd /usr/bin
./mysql_install_db --user=mysql --basedir=/usr --datadir=/data1/mysqldb/data #迁移数据目录到/data1/mysqldb/data下可自行定制
cd /var/lib
mv mysql /tmp
mkdir mysql
chown mysql.mysql mysql
service mysql start && echo "Mysql root password was empty.Please change when you login mysql."
ln -s /data1/mysqldb/data/mysql.sock /var/lib/mysql/mysql.sock #由于一些mysql工具默认到这里找mysql.sock所以做个软链
四、注意问题
1、关闭selinux否则脚本安装不成功
2、安装后系统自带的postfix和crontab工具被御了,重新yum安装 一次即可
3、mysql服务的root密码为空,请自行修改root密码
或在脚本中添加以下行 在mysql迁移好目录启动后自动修改mysql root密码
mysqladmin -u root password "123.com"
补充部分:
CentOS7.x安装 mysql 5.7.21
到mysql官方下载Linux通用包wget https://cdn.mysql.com/Downloads/MySQL-5.7/mysql-5.7.21-linux-glibc2.12-x86_64.tar.gz
解压:
tar -xvf mysql-5.7.21-linux-glibc2.12-x86_64.tar.gz -C /usr/local
ln -sv mysql-5.7.21-linux-glibc2.12-x86_64 mysql
添加环境变量
cat /etc/profile.d/mysql.sh
export PATH=/usr/local/mysql/bin:$PATH
初始化:
#useradd -r mysql
#mkdir -pv /data/mysql
#chown mysql.mysql /data/mysql
#mysqld --initialize-insecure --datadir=/data/mysql --user=mysql --basedir=/usr/local/mysql/
#mkdir /usr/local/mysql/etc/my.cnf.d
#chown mysql.mysql -R /usr/local/mysql/etc
#cp /etc/my.cnf /usr/local/mysql/etc/
#cat /usr/local/mysql/etc/my.cnf
[mysqld]
datadir=/data/mysql
socket=/tmp/mysql.sock
[mysqld_safe]
log-error=/usr/local/mysql/log/error.log
pid-file=/var/run/mysql/mysql.pid
!includedir /usr/local/mysql/etc/my.cnf.d
复制启动脚本:
#cp /usr/local/mysql/suppor-files/mysql.server /etc/init.d/mysqld
#touch /usr/local/mysql/log/error.log
#chwon mysql.mysql /usr/local/mysql/log/error.log
#chmod 755 /usr/local/mysql/log/error.log
#chkconfig --add mysqld
#chkconfig mysqld on
#service mysqld start
Ubuntu 16.04上安装
下载同样的包
安装的步骤与上面CentOS7类似,不同的是ubuntu 16.04上不支持service 方式管理
因此需要复制
#cp /usr/local/mysql/suppor-files/mysql.server /etc/init.d/mysqld
#sudo systemctl daemon-reload
#chown root.mysql /usr/local/mysql -R
#chmod 775 /usr/local/mysql -R
#systemctl enable mysqld
#systemctl start mysqld
#systemctl status mysqld
如图:
mysql -uroot -p
如图:
注意安装后mysql root密码为空;请自行设置 root密码;以下脚本一键安装亦是如此!
一键安装脚本
以上在CentOS7上安装和Ubuntu 16.04下安装大同小异,均可以安装成功;因此整理成一键安装脚本!
#cat auto_install_mysql.sh