Installing MySQL on CentOS

Before installing MySQL, it's crucial to check if a previous version or a related package like MariaDB is already present on your CentOS system. This step prevents potentila conflicts.

# Check for any installed MySQL packages
rpm -qa | grep mysql

# If found, locate and remove MySQL-related directories
whereis mysql
find / -name mysql | xargs sudo rm -rf

CentOS typical comes with MariaDB, a fork of MySQL. This needs to be uninstalled to avoid conflicts with a fresh MySQL installation.

# List installed MariaDB packages
rpm -qa | grep mariadb

# Remove MariaDB packages (use --nodeps if dependencies prevent removal)
sudo rpm -e --nodeps mariadb-libs*

# Remove any existing MySQL configuration files
sudo rm /etc/my.cnf

Next, ensure that a dedicated user and group for MySQL exist. If not, create them.

# Check if 'mysql' group exists
grep mysql /etc/group

# Check if 'mysql' user exists
grep mysql /etc/passwd

# Create 'mysql' group if it doesn't exist
sudo groupadd mysql

# Create 'mysql' user (system account, belongs to the 'mysql' group)
sudo useradd -r -g mysql mysql

Download the MySQL community server archive (e.g., from the official MySQL website). Extract the downloaded archive.

# Example for MySQL 8.0 (adjust filename as needed)
# tar -xvJf mysql-8.0.XX-linux-glibc2.12-x86_64.tar.xz

# Example for MySQL 5.7 (adjust filename as needed)
tar -zxvf mysql-5.7.XX-linux-glibc2.12-x86_64.tar.gz -C /usr/local/

Rename the extracted directory to a more manageable name, such as 'mysql', and place it in a standard location like /usr/local/.

# Move and rename the extracted directory
sudo mv /usr/local/mysql-5.7.XX-linux-glibc2.12-x86_64 /usr/local/mysql

Set the correct ownership and permissions for the MySQL installation directory to ensure the MySQL user can access and manage its files.

# Recursively change ownership to the 'mysql' user and group
sudo chown -R mysql:mysql /usr/local/mysql

# Set read and execute permissions for all users, and write for owner
sudo chmod -R 755 /usr/local/mysql

Create a custom MySQL configuration file, my.cnf, to define server settings.

# Open or create the configuration file
sudo vi /etc/my.cnf

Add the following configuration to /etc/my.cnf. Adjust settings like datadir, port, and lower_case_table_names based on your specific requirements and system setup.

[mysqld]
datadir=/usr/local/mysql/data
port = 3306
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
symbolic-links=0
max_connections=400
innodb_file_per_table=1
# Set to 1 for case-insensitive table names, 0 for case-sensitive
lower_case_table_names=1

Initialize the MySQL data directory. This step creates the necessary system tables and generates a temporary root password. It's crucial to note this temporary password.

# Navigate to the MySQL binary directory
cd /usr/local/mysql/bin

# Initialize the database, specifying the user, data directory, base directory, and table name case sensitivity
sudo ./mysqld --initialize --user=mysql --datadir=/usr/local/mysql/data --basedir=/usr/local/mysql --lower-case-table-names=1

Start the MySQL server. You can check if the server is running and kill any conflicting processes if necessary.

# Check for running MySQL processes
ps -ef | grep mysql

# If necessary, terminate existing MySQL processes (replace PID with the actual process ID)
# sudo kill -9 PID

# Start the MySQL server using its script
sudo /usr/local/mysql/support-files/mysql.server start

Create symbolic links to make the MySQL server script and client accessible system-wide and from standard service directories.

# Link the server start script to init.d for service management
sudo ln -s /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql

# Link the mysql client executable to a system path
sudo ln -s /usr/local/mysql/bin/mysql /usr/bin/mysql

# Restart the MySQL service to apply changes
sudo service mysql restart

Log in to the MySQL server using the root user and the temporary password generated during initialization.

mysql -u root -p

Immediately change the temporary root password to a secure, memorable one.

SET PASSWORD FOR 'root'@'localhost' = 'YourNewSecurePassword';
FLUSH PRIVILEGES;

To allow remote connections, update the user table to grant access from any host ('%').

USE mysql;
UPDATE user SET Host='%' WHERE User='root';
FLUSH PRIVILEGES;

Finallly, configure MySQL to start automatically on system boot.

# Copy the MySQL server script to the init.d directory and rename it
sudo cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld

# Grant execute permissions to the script
sudo chmod +x /etc/init.d/mysqld

# Add the MySQL service to system startup services
sudo chkconfig --add mysqld

# List available services to verify
chkconfig --list

# Ensure privileges are up-to-date after changes
FLUSH PRIVILEGES;

Tags: MySQL centos database installation server administration

Posted on Wed, 13 May 2026 23:35:45 +0000 by puja