search
HomeDatabaseMysql TutorialHow to install and configure MySQL 5.7 on CentOS 7

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'


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

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


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

  1 # yum install -y mysql-community-server


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


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


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

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


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

  1 # mysql -uroot -p


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!');


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;


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


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


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


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%';


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

  1 # systemctl enable mysqld


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


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


Then use the rpm command to install:

  1 # rpm -ivh *.rpm


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


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


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

  1 # mysql -uroot -p


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

  1 > SET PASSWORD = PASSWORD('******');


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

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


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

  1 # vim /etc/my.cnf


设置数据库字符集为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


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

  1 # systemctl restart mysqld


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

  1 # SHOW VARIABLES LIKE 'character%';


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

  1 # systemctl enable mysqld


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

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
This article is reproduced at:亿速云. If there is any infringement, please contact admin@php.cn delete
How do you handle database upgrades in MySQL?How do you handle database upgrades in MySQL?Apr 30, 2025 am 12:28 AM

The steps for upgrading MySQL database include: 1. Backup the database, 2. Stop the current MySQL service, 3. Install the new version of MySQL, 4. Start the new version of MySQL service, 5. Recover the database. Compatibility issues are required during the upgrade process, and advanced tools such as PerconaToolkit can be used for testing and optimization.

What are the different backup strategies you can use for MySQL?What are the different backup strategies you can use for MySQL?Apr 30, 2025 am 12:28 AM

MySQL backup policies include logical backup, physical backup, incremental backup, replication-based backup, and cloud backup. 1. Logical backup uses mysqldump to export database structure and data, which is suitable for small databases and version migrations. 2. Physical backups are fast and comprehensive by copying data files, but require database consistency. 3. Incremental backup uses binary logging to record changes, which is suitable for large databases. 4. Replication-based backup reduces the impact on the production system by backing up from the server. 5. Cloud backups such as AmazonRDS provide automation solutions, but costs and control need to be considered. When selecting a policy, database size, downtime tolerance, recovery time, and recovery point goals should be considered.

What is MySQL clustering?What is MySQL clustering?Apr 30, 2025 am 12:28 AM

MySQLclusteringenhancesdatabaserobustnessandscalabilitybydistributingdataacrossmultiplenodes.ItusestheNDBenginefordatareplicationandfaulttolerance,ensuringhighavailability.Setupinvolvesconfiguringmanagement,data,andSQLnodes,withcarefulmonitoringandpe

How do you optimize database schema design for performance in MySQL?How do you optimize database schema design for performance in MySQL?Apr 30, 2025 am 12:27 AM

Optimizing database schema design in MySQL can improve performance through the following steps: 1. Index optimization: Create indexes on common query columns, balancing the overhead of query and inserting updates. 2. Table structure optimization: Reduce data redundancy through normalization or anti-normalization and improve access efficiency. 3. Data type selection: Use appropriate data types, such as INT instead of VARCHAR, to reduce storage space. 4. Partitioning and sub-table: For large data volumes, use partitioning and sub-table to disperse data to improve query and maintenance efficiency.

How can you optimize MySQL performance?How can you optimize MySQL performance?Apr 30, 2025 am 12:26 AM

TooptimizeMySQLperformance,followthesesteps:1)Implementproperindexingtospeedupqueries,2)UseEXPLAINtoanalyzeandoptimizequeryperformance,3)Adjustserverconfigurationsettingslikeinnodb_buffer_pool_sizeandmax_connections,4)Usepartitioningforlargetablestoi

How to use MySQL functions for data processing and calculationHow to use MySQL functions for data processing and calculationApr 29, 2025 pm 04:21 PM

MySQL functions can be used for data processing and calculation. 1. Basic usage includes string processing, date calculation and mathematical operations. 2. Advanced usage involves combining multiple functions to implement complex operations. 3. Performance optimization requires avoiding the use of functions in the WHERE clause and using GROUPBY and temporary tables.

An efficient way to batch insert data in MySQLAn efficient way to batch insert data in MySQLApr 29, 2025 pm 04:18 PM

Efficient methods for batch inserting data in MySQL include: 1. Using INSERTINTO...VALUES syntax, 2. Using LOADDATAINFILE command, 3. Using transaction processing, 4. Adjust batch size, 5. Disable indexing, 6. Using INSERTIGNORE or INSERT...ONDUPLICATEKEYUPDATE, these methods can significantly improve database operation efficiency.

Steps to add and delete fields to MySQL tablesSteps to add and delete fields to MySQL tablesApr 29, 2025 pm 04:15 PM

In MySQL, add fields using ALTERTABLEtable_nameADDCOLUMNnew_columnVARCHAR(255)AFTERexisting_column, delete fields using ALTERTABLEtable_nameDROPCOLUMNcolumn_to_drop. When adding fields, you need to specify a location to optimize query performance and data structure; before deleting fields, you need to confirm that the operation is irreversible; modifying table structure using online DDL, backup data, test environment, and low-load time periods is performance optimization and best practice.

See all articles

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

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools