Install MySQL on centos7

Introduction

  1. During installation and uninstallation, all users must switch to root
  2. When using MySQL, try to use root permissions for ease of use (if you don’t know how to enable root permissions, you can go to Baidu to find out how to enable it for your own server type)
  3. In this article I will use xshell7 for the operation. You can also use your own Cloud Server Terminal (not recommended)

1. Uninstall unnecessary environments (if you have not installed them before, you can jump directly to the installation steps)

1.1 Detect whether MySQL exists

$ ps ajx |grep mysql

It is found that MySQL is running, stop it first and then uninstall it.

Stop MySQL:

$ systemctl stop mysqld

Find MySQL:

$ rpm -qa | grep mysql

1.2 MySQL uninstall:

# rpm -qa | grep mysql | xargs yum -y remove

Uninstall complete! ! !

1.3 Check whether MySQL exists:

$ rpm -qa | grep mysql

No results displayed

$ ps ajx |grep mysql

No other processes are displayed

# ls /etc/my.cnf

No configuration file

2. Install MySQL

2.1 Obtain the official source of yum for MySQL

Due to the web page, the display is incomplete.

Right click and click to view web page source code

Find the required rpm file for your corresponding version and click to download.

View your version:

# cat /etc/redhat-release

So I downloaded version 7.6

2.2 Upload MySQL official source

Create the MySQL folder and open it

# mkdir MySQL
# cd MySQL/

Upload the downloaded .rpm file

# rz

(Installation of rz tool)

Select files to transfer

#ll

Check

Already have installation package

2.3 Install MySQL

Install the installer

# rpm -ivh mysql57-community-release-elxx.rpm

Check whether the installer is installed successfully

# ls /etc/yum.repos.d/ -l

The presence of these two means the installation was successful.

Start the formal installation

# yum install -y mysql-community-server

Problems encountered:

The installation encountered a problem with the expired key:
Failing package is: mysql-community-client -5.7.39-1.el7.x86_ 64
GPG Keys are configured as: file:///etc/ pki/ rpm-gpg/RPM-GPG-KEY-mysql

solution:

# rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022

After entering, just execute the formal installation instructions again.

Check whether the installation is successful

# ls /etc/my.cnf
# which mysqld
# which mysql

All three files exist, which means the installation is successful (in the uninstallation part, we have successfully uninstalled)

Start MySQL

# systemctl start mysql

Check whether it is started

# ps ajx | grep mysqld

Login to MySQL

# mysql -u root -p

How to get the root password:

1. Obtain temporary root password

# grep 'temporary password' /var/log/mysqld.log

2. Enter directly (not next)

In the latest version of mysql, there is no so-called temporary password, and root still does not have a password

Just enter

3. Modify the file (this will definitely work, don’t forget to restart mysql when modifying the file)

# vim /etc/my.cnf

Add the following command at the end of the file, press esc and enter :wq to save and exit

skip-grant-tables

Restart mysql service

# systemctl restart mysqld

Log in to mysql

# mysql -u root -p

Just press Enter and you can log in to mysql.

3.MySQL configuration file

Configure my.cnf, which is mainly the encoding format of the database client and server


# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html

[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M

port=3306
character-set-server=utf8
default-storage-engine=innodb

datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
          
         
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
             
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
skip-grant-tables

Just replace the above content directly with the content of the reader’s my.cnf.