欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

CentOS6CentOS7二进制安装MYSQL5.7,修改默认密码

程序员文章站 2024-03-14 14:55:10
...

1、可自定义输入ROOT密码(发现从mysql的log-error日志里不见得能记录初始化密码,因此把输出文本保存下来/root/mysql-install.log,后期从中读取);

2、检查CentOS7自带的mariadb并卸载;

3、使用YUM安装了一个依赖,yum -y install numactl;

4、my.cnf位置放在/usr/local/mysql/my.cnf,(配置文件水太深,需要好好研究)

5、执行完安装改掉密码后,CheckInstall{}之前,会停住,用Ctrl+c中断即可继续,希望有大神能告诉我原因。

废话不多说:

 

#!/bin/bash
######二进制自动安装数据库脚本root密码Root123456,将脚本和安装包放在/root目录即可###############
######程序目录/usr/local/mysql############
######数据目录软连接到/data/mysql############
######慢日志目录/data/mysql/slowlog############

echo "################检查本机安装mysql的基本条件########################"
echo "Checking  user :"
if [ $(id -u) != "0" ]; then
	echo "Error: You must be root to run this script, please use root to install"
	exit 1
else
	echo "user is root, this is ok!"
fi

if [ -d /data/mysql ]; then
	echo "mysql datadir  /data/mysql is exist! ,this is fail!"
	exit 1
else
	echo "mysql datadir  /data/mysql is not exist,this is ok!"
fi

os_version=`uname -r|cut -d . -f 6`
if [ "$os_version" = "el7" ] || [ "$os_version" = "el6" ]; then
	echo "os version is el6 or el7, this is ok!"
else
	echo "os version isnot el6 or el7, this is fail!"
	exit 1
fi

#set mysql root password
echo "################设置MYSQL默认密码########################"

mysqlrootpwd="Root123456"
echo -e "Please input the root password of mysql:"
read -p "(Default password: Root123456):" mysqlrootpwd
if [ "$mysqlrootpwd" = "" ]; then
	mysqlrootpwd="Root123456"
fi
echo "========================================="
echo "MySQL root password:$mysqlrootpwd"
echo "========================================="

# Initialize  the installation related content.
function InitInstall()
{
	MemTotal=`free -m | grep Mem | awk '{print  $2}'`  
	echo -e "\n 内存Memory is: ${MemTotal} MB "
	#Set timezone
	#rm -rf /etc/localtime
	#ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

	#Delete Old Mysql mariadb program
	oldmysql=`rpm -qa|grep mysql`
	oldmariadb=`rpm -qa|grep mariadb`
	if [ "$oldmysql" != "" ];then
		echo "已存在mysql,正在卸载"
		rpm -e "$oldmysql" --nodeps
	fi
	if [ "$oldmariadb" != "" ];then
		echo "已存在MariaDB,正在卸载"
		rpm -e "$oldmariadb" --nodeps
	fi

	#Disable SeLinux
	if [ -s /etc/selinux/config ]; then
		sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
	fi
	setenforce 0
	
	#install numactl
	yum -y install numactl
}

#Installation of depend on and optimization options.
function InstallDependsAndOpt()
{
echo "添加用户资源限制/etc/security/limits.conf"
cat >>/etc/security/limits.conf<<EOF
* soft nproc 65535
* hard nproc 65535
* soft nofile 65535
* hard nofile 65535
EOF

echo "添加内核调优 /etc/sysctl.conf"
cat >>/etc/sysctl.conf<<EOF
fs.file-max=6815744
net.ipv4.ip_local_port_range = 9000 65500
net.core.rmem_default = 262144
net.core.rmem_max = 4194304
net.core.wmem_default = 262144
net.core.wmem_max = 1048576
EOF

sysctl -p
}

function InstallMySQL57()
{
echo "============================安装 MySQL 5.7.25=================================="

#mysql directory configuration
groupadd mysql
useradd -r -g mysql -s /bin/false mysql
tar zxf /root/mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz
echo "==========解压MySQL完成=========="
mv /root/mysql-5.7.25-linux-glibc2.12-x86_64 /usr/local/mysql
cd /usr/local/mysql
mkdir mysql-files
chown mysql:mysql mysql-files
chmod 750 mysql-files
bin/mysqld --initialize --user=mysql
bin/mysql_ssl_rsa_setup
#bin/mysqld_safe --user=mysql &
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld

chown -R mysql:mysql /usr/local/mysql
mkdir -p /data/mysql
mv /usr/local/mysql/data /data/mysql/
ln -s /data/mysql/data /usr/local/mysql/
chown -R mysql:mysql /data/mysql

chkconfig --add mysqld
chkconfig --level 2345 mysqld on

cat >>/usr/local/mysql/my.cnf<<EOF
[client]
port=3306
default-character-set=utf8

[mysql]


[mysqld]
port=3306
character_set_server=utf8
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
pid-file=/usr/local/mysql/data/mysqld.pid
#socket=/usr/local/mysql/data/mysql.sock

sql_mode = "STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION,NO_ZERO_DATE,NO_ZERO_IN_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER"
ssl=0
max_connections=50000
connect_timeout=15
query_cache_size=0
sort_buffer_size=20m
tmp_table_size=96m
########log settings########
log_error=/usr/local/mysql/data/mysqld.log
slow_query_log = 1
slow_query_log_file = /usr/local/mysql/data/slow.log

#skip_name_resolve=ON

########innodb settings########
innodb_buffer_pool_size=128m
innodb_buffer_pool_instances=5
innodb_log_file_size=128m
innodb_thread_concurrency=0
innodb_flush_log_at_trx_commit=2
#sync_binlog=0
#binlog_order_commits=ON
#transaction_isolation=READ-COMMITTED
#slave_parallel_workers=10

EOF
echo "=================================================================="
}
function ChangePasswd()
{
read -p "是否将mysql/bin加入环境变量(输入y/Y加入,其他不加入):" inputMsg
if [ "$inputMsg" = 'y' ] || [ "$inputMsg" = 'Y' ] 
then
    echo 'export PATH=$PATH:/usr/local/mysql/bin'>>/etc/profile
	sleep 2
	source /etc/profile
	echo "添加完成"
else
    echo "不添加"
fi

echo "################启动MYSQL#################"
/etc/init.d/mysqld start
if [ "$?" == "0" ] 
then 
	echo "mysql启动成功"
else
	echo "mysql启动失败,退出"
	exit 1
fi


#查看root随机密码
echo "================================修改密码================================="
passwd=`grep 'A temporary password' ~/mysql-install.log | awk -F"[email protected]: " '{ print $2}' `
echo "初始密码为$passwd"
#登录数据库
/usr/local/mysql/bin/mysql -uroot -p${passwd} --connect-expired-password <<EOF
set password = '${mysqlrootpwd}';
grant all privileges on *.* to 'system'@'%' identified by '${mysqlrootpwd}';
flush privileges;
exit
EOF

# cat > /tmp/mysql_sec_script<<EOF
# use mysql;
# delete from mysql.user where user!='root' or host!='localhost';
# grant all privileges on *.* to 'sys_admin'@'%' identified by 'MANAGER';
# flush privileges;
# EOF

# /usr/local/mysql/bin/mysql -u root -p$mysqlrootpwd -h localhost < /tmp/mysql_sec_script
#rm -f /tmp/mysql_sec_script
#/etc/init.d/mysqld restart

echo "============================MySQL 5.7.25 安装完成========================="
}

function CheckInstall()
{
echo "============================== Check install ============================="
echo "Checking..."

if [ -s /usr/local/mysql/bin/mysql ] && [ -s /usr/local/mysql/bin/mysqld_safe ] ; then
  echo "MySQL: OK"
else
  echo "Error: /usr/local/mysql not found!!!MySQL install failed."
fi

echo "Install MySQL 5.7.25 completed! enjoy it."
echo "===========================查看进程================================"
ps -ef |grep mysql
echo "===========================查看端口================================"
if [ "$os_version" = "el7" ] ; then
	ss -nptl |column -t
else
	netstat -nptl
fi

}
 
#The installation log
InitInstall 2>&1 | tee /root/mysql-install.log
InstallDependsAndOpt 2>&1 | tee -a /root/mysql-install.log
InstallMySQL57 2>&1 | tee -a /root/mysql-install.log
ChangePasswd 2>&1 | tee -a /root/mysql-install.log
CheckInstall 2>&1 | tee -a /root/mysql-install.log