Fading Coder

One Final Commit for the Last Sprint

Home > Notes > Content

MySQL 5.7 Deployment on CentOS 7: Repository, RPM, and Binary Installation Methods

Notes 1

Post-Installation Verification

After completing any installation method below, verify connectivity and reset credentials:

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'SecureP@ssw0rd!';
Query OK, 0 rows affected (0.01 sec)

mysql> CREATE DATABASE app_database;
Query OK, 1 row affected (0.00 sec)

mysql> USE app_database;
Database changed

mysql> CREATE TABLE employees (emp_id INT, emp_name VARCHAR(20));
Query OK, 0 rows affected (0.02 sec)

mysql> INSERT INTO employees VALUES (1,'Alice'),(2,'Bob'),(3,'Charlie');
Query OK, 3 rows affected (0.01 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> SELECT * FROM employees;
+--------+----------+
| emp_id | emp_name |
+--------+----------+
|      1 | Alice    |
|      2 | Bob      |
|      3 | Charlie  |
+--------+----------+
3 rows in set (0.00 sec)

Method 1: YUM Repository Installation

For servers with internet access, use the official Oracle MySQL repository:

Download and install the release package:

[admin@db-server ~]$ wget https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
[admin@db-server ~]$ sudo rpm -Uvh mysql57-community-release-el7-11.noarch.rpm

Modify repository configuration to enable MySQL 5.7 and disable 8.0:

[admin@db-server ~]$ sudo sed -i 's/enabled=1/enabled=0/' /etc/yum.repos.d/mysql-community.repo
[admin@db-server ~]$ sudo yum-config-manager --enable mysql57-community

Install the database server:

[admin@db-server ~]$ sudo yum install -y mysql-community-server mysql-community-devel

Retrieve the temporary root password from logs:

[admin@db-server ~]$ sudo grep 'temporary password' /var/log/mysqld.log
2024-01-15T08:30:12.123456Z 1 [Note] A temporary password is generated for root@localhost: 9Xk#mP2$vLqR

[admin@db-server ~]$ mysql -u root -p'9Xk#mP2$vLqR'

Method 2: Offline RPM Bundle Installation

For air-gapped enviornments, deploy using the official RPM bundle:

Extract the package collection:

[admin@db-server ~]$ mkdir -p /opt/mysql-packages && cd /opt/mysql-packages
[admin@db-server ~]$ tar -xvf mysql-5.7.44-1.el7.x86_64.rpm-bundle.tar
[admin@db-server ~]$ ls *.rpm
mysql-community-client-5.7.44-1.el7.x86_64.rpm
mysql-community-common-5.7.44-1.el7.x86_64.rpm
mysql-community-libs-5.7.44-1.el7.x86_64.rpm
mysql-community-server-5.7.44-1.el7.x86_64.rpm

Install all packages locally:

[admin@db-server ~]$ sudo yum localinstall -y mysql-community-*.rpm

Complete initial setup:

[admin@db-server ~]$ sudo grep 'password' /var/log/mysqld.log | tail -1
[admin@db-server ~]$ mysql -uroot -p'TemporaryPass123!'

Method 3: Generic Binary Installation

Compile from generic Linux binaries for custom deployment paths:

Prepare system dependencies:

[admin@db-server ~]$ sudo rpm -e mariadb-libs --nodeps
[admin@db-server ~]$ sudo yum install -y libaio numactl-libs ncurses-devel

Deploy binary distribution:

[admin@db-server ~]$ cd /usr/local/src
[admin@db-server ~]$ sudo tar -zxvf mysql-5.7.44-linux-glibc2.12-x86_64.tar.gz
[admin@db-server ~]$ sudo mv mysql-5.7.44-linux-glibc2.12-x86_64 /usr/local/mysql57
[admin@db-server ~]$ sudo ln -s /usr/local/mysql57 /usr/local/mysql

Create data directories and service account:

[admin@db-server ~]$ sudo mkdir -p /var/db/mysql/{data,logs,tmp}
[admin@db-server ~]$ sudo groupadd -r dbadmin
[admin@db-server ~]$ sudo useradd -r -g dbadmin -M -s /sbin/nologin dbadmin
[admin@db-server ~]$ sudo chown -R dbadmin:dbadmin /var/db/mysql /usr/local/mysql
[admin@db-server ~]$ sudo ln -s /usr/local/mysql/bin/* /usr/local/bin/

Initialize the data directory:

[admin@db-server ~]$ sudo /usr/local/mysql/bin/mysqld --initialize-insecure \
  --user=dbadmin --basedir=/usr/local/mysql --datadir=/var/db/mysql/data

Configure instance settings in /etc/mysql57.cnf:

[client]
port=3306
socket=/var/db/mysql/mysql.sock

[mysqld]
user=dbadmin
basedir=/usr/local/mysql
datadir=/var/db/mysql/data
port=3306
socket=/var/db/mysql/mysql.sock
pid-file=/var/db/mysql/logs/mysqld.pid
log-error=/var/db/mysql/logs/error.log
character-set-server=utf8mb4
collation-server=utf8mb4_unicode_ci
default-storage-engine=InnoDB
symbolic-links=0

Create systemd service file /etc/systemd/system/mysql57.service:

[Unit]
Description=MySQL 5.7 Database Server
After=network.target

[Service]
Type=forking
User=dbadmin
Group=dbadmin
ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/etc/mysql57.cnf --daemonize
ExecReload=/bin/kill -HUP $MAINPID
ExecStop=/bin/kill -QUIT $MAINPID
PIDFile=/var/db/mysql/logs/mysqld.pid
LimitNOFILE=65535
Restart=on-failure

[Install]
WantedBy=multi-user.target

Start and enable the service:

[admin@db-server ~]$ sudo systemctl daemon-reload
[admin@db-server ~]$ sudo systemctl enable --now mysql57
[admin@db-server ~]$ mysql -uroot -e "ALTER USER 'root'@'localhost' IDENTIFIED BY 'NewSecurePass123!';"

Troubleshooting Common Isssues

Service Start Failure If systemctl start mysql57 exits with error code 1, verify PID directory permissoins:

[admin@db-server ~]$ sudo mkdir -p /var/run/mysqld
[admin@db-server ~]$ sudo chown dbadmin:dbadmin /var/run/mysqld
[admin@db-server ~]$ sudo systemctl restart mysql57

Missing Temporary Password When the error log contains no temporary password:

[admin@db-server ~]$ sudo rm -rf /var/db/mysql/data/*
[admin@db-server ~]$ sudo /usr/local/mysql/bin/mysqld --initialize \
  --user=dbadmin --datadir=/var/db/mysql/data
[admin@db-server ~]$ sudo grep 'temporary password' /var/db/mysql/logs/error.log

Socket Connection Errors When encountering ERROR 2002 (HY000): Can't connect through socket:

[admin@db-server ~]$ ln -s /var/db/mysql/mysql.sock /tmp/mysql.sock

Production Configuration Reference

Optimized my.cnf template for server deployment:

[client]
port=3306
socket=/var/db/mysql/mysql.sock
default-character-set=utf8mb4

[mysql]
prompt="\\u@\\h [\\d]> "
no-auto-rehash

[mysqld]
# Core Settings
user=dbadmin
port=3306
basedir=/usr/local/mysql
datadir=/var/db/mysql/data
socket=/var/db/mysql/mysql.sock
pid-file=/var/db/mysql/logs/mysqld.pid
default-storage-engine=InnoDB
default-authentication-plugin=mysql_native_password

# Logging Configuration
log-error=/var/db/mysql/logs/error.log
slow-query-log=1
slow-query-log-file=/var/db/mysql/logs/slow.log
long-query-time=1
log-timestamps=SYSTEM
log-queries-not-using-indexes=1

# InnoDB Tuning
innodb-buffer-pool-size=2G
innodb-buffer-pool-instances=4
innodb-log-file-size=256M
innodb-log-files-in-group=3
innodb-flush-log-at-trx-commit=1
innodb-file-per-table=1
innodb-flush-method=O_DIRECT
innodb-large-prefix=1

# Connection Handling
max-connections=300
max-user-connections=100
wait-timeout=600
interactive-timeout=600
max-allowed-packet=64M

# Replication Settings
server-id=1001
log-bin=/var/db/mysql/logs/binlog
binlog-format=ROW
expire-logs-days=7
gtid-mode=ON
enforce-gtid-consistency=ON
log-slave-updates=1

# Query Cache (disabled for 5.7 performance)
query-cache-type=0
query-cache-size=0

# Table Settings
table-open-cache=2048
table-definition-cache=1024
tmp-table-size=64M
max-heap-table-size=64M

# Security
skip-name-resolve
local-infile=0
symbolic-links=0
require-secure-transport=OFF

Related Articles

Deploying a Maven Web Application to Tomcat 9 Using the Tomcat Manager

Tomcat 9 does not provide a dedicated Maven plugin. The Tomcat Manager interface, however, is backward-compatible, so the Tomcat 7 Maven Plugin can be used to deploy to Tomcat 9. This guide shows two...

Skipping Errors in MySQL Asynchronous Replication

When a replica halts because the SQL thread encounters an error, you can resume replication by skipping the problematic event(s). Two common approaches are available. Methods to Skip Errors 1) Skip a...

Spring Boot MyBatis with Two MySQL DataSources Using Druid

Required dependencies application.properties: define two data sources and poooling Java configuration for both data sources MyBatis mappers for each data source Controller endpoints to verify both co...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.