Home Database Mysql Tutorial How to install and configure MySQL 5.7 on CentOS 7

How to install and configure MySQL 5.7 on CentOS 7

Jun 01, 2023 pm 09:52 PM
mysql centos

CentOS 7 installation and configuration MySQL 5.7

Overview
The previous article recorded the installation and configuration of MySQL 5.7 in the Windows system. Due to the need for installation and deployment of the big data environment, it is now necessary to install and configure MySQL 5.7 in the CentOS 7 system. The installation and configuration of the CentOS 7 environment have also been recorded, so the installation and configuration are carried out directly here.
Install MySQL 5.7 from yum source
Install MySQL 5.7
In the CentOS 7 system, the default source file of the system does not contain MySQL. If you directly use the yum source to execute the installation command, you will be prompted "There is no available package mysql-community-server.":
CentOS 7如何安装配置MySQL 5.7
Therefore, you need to manually execute the following command to download the source file installation file:

  1 # cd /home
  2 # wget 'https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm'
Copy after login


CentOS 7如何安装配置MySQL 5.7
Then execute the source file installation command:

  1 # rpm -ivh mysql57-community-release-el7-11.noarch.rpm
Copy after login


CentOS 7如何安装配置MySQL 5.7
Now you can install MySQL, execute the following command:

  1 # yum install -y mysql-community-server
Copy after login


Wait for a moment, wait for the download and installation to complete:
CentOS 7如何安装配置MySQL 5.7
Execute as follows Command, start the database and check the database status:

  1 # systemctl start mysqld
  2 # systemctl status mysqld
Copy after login
Copy after login


CentOS 7如何安装配置MySQL 5.7
Configure MySQL 5.7
This version of the database will be installed in /var/log/mysqld Generate a random root user password in the .log file. View the file to obtain the password:

  1 # cat /var/log/mysqld.log
Copy after login


CentOS 7如何安装配置MySQL 5.7
or use the following command:

  1 # grep 'temporary password' /var/log/mysqld.log
Copy after login
Copy after login


CentOS 7如何安装配置MySQL 5.7
Use the following command to log in to the MySQL database:

  1 # mysql -uroot -p
Copy after login
Copy after login


Password Enter the password you just found to log in to the database:
CentOS 7如何安装配置MySQL 5.7
Use the following command to change the root user password:

  1 > SET PASSWORD = PASSWORD('Password@123!');
Copy after login


CentOS 7如何安装配置MySQL 5.7
Remote access to the database is not open by default. Use the following command to configure:

  1 > GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'Password@123!' WITH GRANT OPTION;
Copy after login


Then enter quit, press Enter to exit the database login, and use the command to open the database configuration file:

  1 # vim /etc/my.cnf
Copy after login
Copy after login


CentOS 7如何安装配置MySQL 5.7
Set the database character set to utf8mb4, and set sql_mode to support group by statement. The complete configuration file content is as follows:

  1 [mysqld]
  2 datadir=/var/lib/mysql
  3 socket=/var/lib/mysql/mysql.sock
  4 symbolic-links=0
  5 log-error=/var/log/mysqld.log
  6 pid-file=/var/run/mysqld/mysqld.pid
  7 character-set-server = utf8mb4
  8 collation-server = utf8mb4_unicode_ci
  9 sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
 10 
 11 [mysql]
 12 default-character-set = utf8mb4
 13 
 14 [client]
 15 default-character-set = utf8mb4
 16
Copy after login


Notice:
It is set to utf8mb4 here: First, because utf8 encoding only supports 3-byte data, and the emoticon data on the mobile terminal is 4-byte characters, so directly inserting emoticon data into the utf-8 encoded database will report an exception; Second, I read an article by a master who mentioned that utf8 in MySQL is not real utf8, so utf8mb4 is used.
After the configuration is complete, execute the following command to restart the database service:

  1 # systemctl restart mysqld
Copy after login
Copy after login


Use the modified password to log in to the database, and execute the following command to view the character set settings:

  1 # SHOW VARIABLES LIKE 'character%';
Copy after login
Copy after login


CentOS 7如何安装配置MySQL 5.7
Execute the following command to set up the database service to start:

  1 # systemctl enable mysqld
Copy after login
Copy after login


Compressed package to install MySQL 5.7
If the server cannot be connected to the Internet, you cannot use the yum source for installation. You can use a computer with Internet access, go to the official website to download the compressed package for installation, and then change the server to install the compressed package.
First go to the official website: https://www.mysql.com/ to download the relevant installation package:
CentOS 7如何安装配置MySQL 5.7
Remotely connect to the /usr directory on the server to create mysql57:

  1 # cd /usr
  2 # mkdir mysql57
Copy after login


Use Xftp to upload the compressed package to the mysql57 directory on the server:
CentOS 7如何安装配置MySQL 5.7
Since mariadb is installed by default in the CentOS 7 system, use the following command to view and uninstall mariadb:

  1 # rpm -qa | grep mariadb
  2 # rpm -e --nodeps mariadb-libs-5.5.56-2.el7.x86_64
Copy after login


Then use the rpm command to install:

  1 # rpm -ivh *.rpm
Copy after login


CentOS 7如何安装配置MySQL 5.7
Use the following command to start the MySQL service and check the service running status :

  1 # systemctl start mysqld
  2 # systemctl status mysqld
Copy after login
Copy after login


CentOS 7如何安装配置MySQL 5.7
MySQL 5.7 database installation is completed.
Configuring MySQL 5.7
View the log file to obtain the password:

  1 # grep 'temporary password' /var/log/mysqld.log
Copy after login
Copy after login


CentOS 7如何安装配置MySQL 5.7
使用如下命令登录MySQL数据库:

  1 # mysql -uroot -p
Copy after login
Copy after login


密码输入刚才查到的密码,即可登录数据库:
CentOS 7如何安装配置MySQL 5.7
使用如下命令,修改root用户密码:

  1 > SET PASSWORD = PASSWORD('******');
Copy after login


数据库默认远程访问未开放,使用如下命令进行配置:

  1 > GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '******' WITH GRANT OPTION;
Copy after login


星号为root用户的密码(下图红色覆盖区域):
CentOS 7如何安装配置MySQL 5.7
然后输入quit,回车退出数据库登录,使用命令打开数据库的配置文件:

  1 # vim /etc/my.cnf
Copy after login
Copy after login


设置数据库字符集为utf8mb4,并设置sql_mode支持group by语句,完整的配置文件内容如下:

  1 [mysqld]
  2 character-set-server = utf8mb4
  3 collation-server = utf8mb4_unicode_ci
  4  sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
  5 
  6 [mysql]
  7 default-character-set = utf8mb4
  8 
  9 [client]
 10 default-character-set = utf8mb4
 11
Copy after login


CentOS 7如何安装配置MySQL 5.7
注意:
此处设置为utf8mb4:一是因为utf8编码只支持3字节的数据,而移动端的表情数据是4个字节的字符,所以直接往utf-8编码的数据库中插入表情数据,会报异常;二是看过一位大神的文章提到,MySQL中的utf8并不是真正的utf8,所以使用utf8mb4。
配置完成后,执行如下命令重启数据库服务:

  1 # systemctl restart mysqld
Copy after login
Copy after login


使用修改后的密码,登录数据库,执行如下命令查看字符集设置:

  1 # SHOW VARIABLES LIKE 'character%';
Copy after login
Copy after login


CentOS 7如何安装配置MySQL 5.7
执行如下命令,设置数据库服务开机启动:

  1 # systemctl enable mysqld
Copy after login
Copy after login


为了方便查看不同安装方式的朋友,将记录了两种不同的安装方式的配置都记录下来,避免他们需要回头去查找配置信息。

The above is the detailed content of How to install and configure MySQL 5.7 on CentOS 7. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MySQL's Role: Databases in Web Applications MySQL's Role: Databases in Web Applications Apr 17, 2025 am 12:23 AM

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

Centos shutdown command line Centos shutdown command line Apr 14, 2025 pm 09:12 PM

The CentOS shutdown command is shutdown, and the syntax is shutdown [Options] Time [Information]. Options include: -h Stop the system immediately; -P Turn off the power after shutdown; -r restart; -t Waiting time. Times can be specified as immediate (now), minutes ( minutes), or a specific time (hh:mm). Added information can be displayed in system messages.

How to start mysql by docker How to start mysql by docker Apr 15, 2025 pm 12:09 PM

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

Laravel Introduction Example Laravel Introduction Example Apr 18, 2025 pm 12:45 PM

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

How to install mysql in centos7 How to install mysql in centos7 Apr 14, 2025 pm 08:30 PM

The key to installing MySQL elegantly is to add the official MySQL repository. The specific steps are as follows: Download the MySQL official GPG key to prevent phishing attacks. Add MySQL repository file: rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm Update yum repository cache: yum update installation MySQL: yum install mysql-server startup MySQL service: systemctl start mysqld set up booting

Solve database connection problem: a practical case of using minii/db library Solve database connection problem: a practical case of using minii/db library Apr 18, 2025 am 07:09 AM

I encountered a tricky problem when developing a small application: the need to quickly integrate a lightweight database operation library. After trying multiple libraries, I found that they either have too much functionality or are not very compatible. Eventually, I found minii/db, a simplified version based on Yii2 that solved my problem perfectly.

Centos stops maintenance 2024 Centos stops maintenance 2024 Apr 14, 2025 pm 08:39 PM

CentOS will be shut down in 2024 because its upstream distribution, RHEL 8, has been shut down. This shutdown will affect the CentOS 8 system, preventing it from continuing to receive updates. Users should plan for migration, and recommended options include CentOS Stream, AlmaLinux, and Rocky Linux to keep the system safe and stable.

Centos configuration IP address Centos configuration IP address Apr 14, 2025 pm 09:06 PM

Steps to configure IP address in CentOS: View the current network configuration: ip addr Edit the network configuration file: sudo vi /etc/sysconfig/network-scripts/ifcfg-eth0 Change IP address: Edit IPADDR= Line changes the subnet mask and gateway (optional): Edit NETMASK= and GATEWAY= Lines Restart the network service: sudo systemctl restart network verification IP address: ip addr

See all articles