- 环境说明
CentOS Linux release 7.9.2009 (Core) 最小化安装
阿里centos7源
1 下载源码包
https://mirrors.tuna.tsinghua.edu.cn/mysql/downloads/MySQL-5.6/
https://mirrors.aliyun.com/mysql/MySQL-5.7/
- 安装包 :
b4a58f228dc5e2d579eb9ce59e566bd5 mysql-5.6.51-linux-glibc2.12-x86_64.tar.gz
9d4ee25b5907a996e9a8af831a52b5e6 mysql-5.7.34-el7-x86_64.tar.gz
2 安装
2.1 for mysql 5.6
#!/bin/bash
PKG_NAME=mysql-5.6.51-linux-glibc2.12-x86_64.tar.gz && echo ${PKG_NAME}
dnf -y install libaio autoconf &> /dev/null && echo '--> install depends success'
useradd -r -s /bin/false mysql
# wget https://mirrors.aliyun.com/mysql/MySQL-5.6/${PKG_NAME} -P /opt
tar zxf /opt/${PKG_NAME} -C /usr/local/src
ln -s /usr/local/src/mysql-5.6.51-linux-glibc2.12-x86_64 /usr/local/mysql
export PATH=$PATH:/usr/local/mysql/bin
echo 'export PATH=$PATH:/usr/local/mysql/bin' >> /etc/profile
mkdir -p /data/mysql
chown -R mysql.mysql /data/ /usr/local/mysql/
# cd /usr/local/mysql && scripts/mysql_install_db --basedir=/usr/local/mysql/ --user=mysql --datadir=/data/mysql
/usr/local/mysql/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/data/mysql/
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql.server
cat > /etc/my.cnf << eof
[mysqld]
socket=/data/mysql.sock
user=mysql
symbolic-links=0
datadir=/data/mysql
innodb_file_per_table=1
[client]
port=3306
socket=/data/mysql.sock
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/tmp/mysql.sock
eof
/etc/init.d/mysql.server start
chkconfig --add mysql.server
2.2 for mysql 5.7
# env
PKG_NAME=mysql-5.7.34-el7-x86_64.tar.gz && echo ${PKG_NAME}
dnf -y install libaio &> /dev/null && echo ' --> install libaios success'
# dnf -y install libaio numactl-libs
# wget https://mirrors.aliyun.com/mysql/MySQL-5.7/mysql-5.7.34-el7-x86_64.tar.gz -P /tmp
useradd -r -s /bin/false mysql
mkdir -p /data/mysql
chown -R mysql:mysql /data/
echo ' --> 1- set env successful'
# prepare
tar zxf ${PKG_NAME} -C /usr/local/src
ln -s /usr/local/src/mysql-5.7.34-el7-x86_64 /usr/local/mysql
export PATH=$PATH:/usr/local/mysql/bin
echo 'export PATH=$PATH:/usr/local/mysql/bin' >> /etc/profile
echo ' --> 2- pkg prepare successful'
# config
cat > /etc/my.cnf << eof
[mysqld]
datadir=/data/mysql
skip_name_resolve=1
socket=/data/mysql/mysql.sock
log-error=/data/mysql/mysql.log
pid-file=/data/mysql/mysql.pid
[client]
socket=/data/mysql/mysql.sock
eof
echo ' --> 3- add config successful'
# initialize
# 生成数据库文件,并提取root密码
mysqld --initialize --user=mysql --datadir=/data/mysql
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
grep password /data/mysql/mysql.log
chkconfig --add mysqld
service mysqld start
echo ' --> 4- initialize successful'
echo ' --> 5- use `mysqladmin -uroot -p'xxxxxx' password new_password` to set new password'
3 初始化
mysqladmin -u root -h localhost password '123456' # 初始化设置root密码即可使用
mysql_secure_installation #ver5.7必须通过此方式初始化
# 设置数据库管理员root口令
# 禁止root远程登录
# 删除anonymous用户帐号
# 删除test数据库
PS
- binary-install mysql5.6
- binary-install mysql5.7
- mysql_secure_installation初始化提示无法通过socket连接DB - ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
# sed -n 147,158p /usr/local/mysql/bin/mysql_secure_installation
sub make_config {
my $password = shift;
my $esc_pass = basic_single_escape($rootpass);
write_file($config,
"# mysql_secure_installation config file",
"[mysql]",
"user=root",
"socket=/data/mysql.sock",
"password='$esc_pass'",
"connect-expired-password");
}