Installing MySQL 5.7 database through binary package

Keywords: MySQL RPM CentOS firewall

1. Download mysql binary installation package and dependency package

wget http://mirror.centos.org/centos/7/os/x86_64/Packages/libaio-devel-0.3.109-13.el7.x86_64.rpm
wget http://mirror.centos.org/centos/7/os/x86_64/Packages/libaio-0.3.109-13.el7.x86_64.rpm
wget https://mirrors.tuna.tsinghua.edu.cn/mysql/downloads/MySQL-5.7/mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz

2. Install the dependency package and extract the mysql binary installation package

#yum -y install numactl  ##error while loading shared libraries: libnuma.so.1
#yum -y install openssl  ##[ERROR]   Could not find OpenSSL on the system
rpm -ivh libaio-0.3.109-13.el7.x86_64.rpm
rpm -ivh libaio-devel-0.3.109-13.el7.x86_64.rpm
tar -xzvf mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz
mv ./mysql-5.7.25-linux-glibc2.12-x86_64  /usr/local/mysqlrun

3. Create mysql running users and user groups

groupadd mysql
useradd -M -g mysql -s /sbin/nologin mysql

4. Create data directory and log directory and grant permission

mkdir -p /usr/local/mysqldata/data
mkdir -p /usr/local/mysqldata/binlogs
mkdir -p /usr/local/mysqldata/relaylogs
touch /usr/local/mysqldata/mysql-error.log
chown -R mysql:mysql  /usr/local/mysqldata
chmod -R 770  /usr/local/mysqldata
chown -R mysql:mysql  /usr/local/mysqlrun
chmod -R 770  /usr/local/mysqlrun

5. Create and configure my.cnf

mv  /etc/my.cnf  /etc/my.cnf_bak`date "+%Y%m%d%H%M"`
cat << EOF >  /etc/my.cnf
[mysqld]
user=mysql
port=3306
#server-id=1
#binlog_format = ROW
#max_binlog_size = 1G
#expire_logs_days = 7
#log-bin=/usr/local/mysqldata/binlogs/mysql-bin
#relay-log=/usr/local/mysqldata/relaylogs/slave-relay-bin
skip-grant-tables
log-error=/usr/local/mysqldata/mysql-error.log
datadir=/usr/local/mysqldata/data
basedir=/usr/local/mysqlrun
socket=/usr/local/mysqlrun/mysql.sock
pid-file=/usr/local/mysqlrun/mysql.pid
skip-name-resolve
symbolic-links = 0
lower_case_table_names = 1
character-set-server = utf8
default-storage-engine = InnoDB
max_connections = 2000
max_allowed_packet = 1G
interactive_timeout = 120
wait_timeout = 864000
[mysql]
socket=/usr/local/mysqlrun/mysql.sock
EOF

6. Initialize mysql database

/usr/local/mysqlrun/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysqlrun --datadir=/usr/local/mysqldata/data
/usr/local/mysqlrun/bin/mysql_ssl_rsa_setup  --datadir=/usr/local/mysqldata/data

7. Set the path of basedir and datadir in the startup script

sed   -i   '/^basedir/c\basedir=\/usr\/local\/mysqlrun'   /usr/local/mysqlrun/support-files/mysql.server
sed   -i   '/^datadir/c\datadir=\/usr\/local\/mysqldata\/data'   /usr/local/mysqlrun/support-files/mysql.server

8. Add mysql tool to environment variable

echo 'export PATH=/usr/local/mysqlrun/bin:$PATH' >> /etc/profile
source /etc/profile

9. Set mysql service to start and start automatically

ln -s /usr/local/mysqlrun/support-files/mysql.server  /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld
chkconfig --add mysqld
chkconfig mysqld on
chkconfig --list
systemctl enable mysqld
systemctl restart mysqld && systemctl status mysqld
#/usr/local/mysqlrun/bin/mysqld_safe --defaults-file=/etc/my.cnf &
#ps -ef | grep mysql | grep -v grep | awk '{print $2}' |xargs kill -9

10. Reset mysql database root password (no need to restart)

ROOT_PASSWD='m4r!adbOP'
ROOT_PASSWD_T='m4r\!adbOP'
mysql -uroot -e "update mysql.user set authentication_string=password('${ROOT_PASSWD_T}') where user='root';"
mysql -uroot -e "flush privileges;"
sed -i 's/^skip-grant-tables/#&/'   /etc/my.cnf
mysql --connect-expired-password -uroot -p"${ROOT_PASSWD}" -e "ALTER USER 'root'@'localhost' IDENTIFIED BY '${ROOT_PASSWD_T}';"
mysql --connect-expired-password -uroot -p"${ROOT_PASSWD}" -e "flush privileges;"
mysql -uroot -p"${ROOT_PASSWD}" -e "show databases;"

11. Set mysql remote access

mysql -uroot -p"${ROOT_PASSWD}" -e "update mysql.user set host='%' where user='root' and host='localhost';"
mysql -uroot -p"${ROOT_PASSWD}" -e "select user,host from mysql.user;"

12. Let go of the firewall (the firewall is closed without any operation)

firewall-cmd --permanent --zone=public --add-port=3306/tcp
firewall-cmd --reload

Posted by uproa on Wed, 23 Oct 2019 13:51:44 -0700