Detailed explanation of installing MySQL on CentOS

Introduction
Recently, a certain cloud held an event and bought a server for daily study and testing. The new machine has nothing, and the installation of some commonly used software is inevitable. , so I thought of recording the installation process in detail, firstly as a memo, and secondly as a reference for students in need.
There are several common ways to install software on Linux:
Source code compilation
Compressed package decompression (usually tar. gz)
Compiled installation package (RPM, DPKG, etc.)
Online installation (YUM, APT, etc.)
The convenience of the above methods gradually increases, but the versatility decreases. For example, directly downloading the compressed package for decompression. This method generally requires you to do some additional configuration work, but as long as you master the method, each method can be used. It is basically applicable to all platforms. Although YUM is simple, it is limited by the platform and the network. If necessary, you need to add some specific YUM sources.
It is best to master several installation methods. In principle, use the simplest one if you can: YUM>RPM>tar.gz>Source code
This article introduces MySQL on CentOS For installation, the main steps are based on the official MySQL documentation: dev.mysql.com/doc/refman/…
In order to test different installation methods, I went through it several times and installed and deleted it. , deleted the installation, every step has been successfully tested by myself, every command has been executed by myself, you can use it with confidence
Let’s stop chatting and get back to the main story (there is a lot of gossip) ...)
1. YUM
0. Delete the installed MySQL
Check MariaDB
shell> rpm -qa|grep mariadb mariadb-server-5.5.60-1.el7_5.x86_64 mariadb-5.5.60-1.el7_5.x86_64 mariadb-libs-5.5.60-1.el7_5.x86_64
Delete mariadb
If If it does not exist (the above check result returns empty), skip the step
shell> rpm -e --nodeps mariadb-server shell> rpm -e --nodeps mariadb shell> rpm -e --nodeps mariadb-libs
In fact, you can install it in yum without deleting mariadb. Installing MySQL will overwrite the previously existing mariadb
Check MySQL
shell> rpm -qa|grep mysql
Delete MySQL
If it does not exist (the above check result returns empty), skip step
shell> rpm -e --nodeps xxx
1. Add MySQL Yum Repository
Starting from CentOS 7, MariaDB becomes the default database installation package in the Yum source. That is to say, if you use yum to install MySQL on CentOS 7 and above systems, MariaDB (a branch of MySQL) will be installed by default. If you want to install the official MySQL version, you need to use the Yum source provided by MySQL.
Download MySQL source
Official website address: dev.mysql.com/downloads/r…
View system version:
shell> cat /etc/redhat-release CentOS Linux release 7.6.1810 (Core)
Select the corresponding version to download. For example, for CentOS 7, currently check the latest Yum source download address on the official website: dev.mysql.com/get/mysql80…
shell> wget https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
Installation MySQL source
shell> sudo rpm -Uvh platform-and-version-specific-package-name.rpm
For example, the latest MySQL source installation for CentOS7:
shell> sudo rpm -Uvh mysql80-community-release-el7-3.noarch.rpm
Check whether the installation is successful
After successful execution, it will be in /etc/yum.repos.d Generate two repo files mysql-community.repo and mysql-community-source.repo
directory and pass yum repolistYou can see mysql related resources
shell> yum repolist enabled | grep "mysql.*-community.*" !mysql-connectors-community/x86_64 MySQL Connectors Community 108 !mysql-tools-community/x86_64 MySQL Tools Community 90 !mysql80-community/x86_64 MySQL 8.0 Community Server 113
2. Select the MySQL version
Use MySQL Yum Repository to install MySQL. The latest stable version will be selected by default, such as installing through the MySQL source above. If so, the default installation will select MySQL 8.0 version. If you just want to install this version, you can skip this step directly. If not, for example, if I want to install MySQL 5.7 version here, I need to "switch the version":
View all MySQL versions in the current MySQL Yum Repository (each version is in a different sub-repository)
shell> yum repolist all | grep mysql
Switch version
shell> sudo yum-config-manager --disable mysql80-community shell> sudo yum-config-manager --enable mysql57-community
In addition to using yum-config-manager, you can also Directly edit the /etc/yum.repos.d/mysql-community.repo file
enabled=0disable
[mysql80-community] name=MySQL 8.0 Community Server baseurl=http://repo.mysql.com/yum/mysql-8.0-community/el/7/$basearch/ enabled=0 gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
enabled=1enable
# Enable to use MySQL 5.7 [mysql57-community] name=MySQL 5.7 Community Server baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/$basearch/ enabled=1 gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
Check the currently enabled MySQL warehouse
shell> yum repolist enabled | grep mysql
If multiple warehouses are enabled at the same time, the latest version will be selected during installation
3. Install MySQL
shell> sudo yum install mysql-community-server
This command will install the MySQL server (mysql-community-server) and its required dependencies and related components, including mysql-community-client, mysql-community-common, mysql-community-libs, etc.
If The bandwidth is not enough, this step will take a long time, please wait patiently~
4. Start MySQL
Start
shell> sudo systemctl start mysqld.service
CentOS 6:
shell> sudo service mysqld start
Check the status
shell> sudo systemctl status mysqld.service
CentOS 6:
shell> sudo service mysqld status
Stop
shell> sudo systemctl stop mysqld.service
CentOS 6:
shell> sudo service mysqld stop
Restart
shell> sudo systemctl restart mysqld.service
CentOS 6:
shell> sudo service mysqld restart
5. Change the password
Initial password
After MySQL is started for the first time, a super administrator account root@localhost will be created, and the initial password is stored in the log file. :
shell> sudo grep 'temporary password' /var/log/mysqld.log
Change the default password
shell> mysql -uroot -p
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456'; ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
The above prompt appears because the password is too simple. The solution is as follows:
使用复杂密码,MySQL默认的密码策略是要包含数字、字母及特殊字符;
如果只是测试用,不想用那么复杂的密码,可以修改默认策略,即
validate_password_policy(以及validate_password_length等相关参数),使其支持简单密码的设定,具体方法可以自行百度;修改配置文件
/etc/my.cnf,添加validate_password=OFF,保存并重启MySQL
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456'; Query OK, 0 rows affected (0.00 sec)
6、允许root远程访问
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION; mysql> FLUSH PRIVILEGES;
7、设置编码为utf8
查看编码
mysql> SHOW VARIABLES LIKE 'character%';
设置编码
编辑/etc/my.cnf,[mysqld]节点增加以下代码:
[mysqld] collation-server=utf8_unicode_ci init-connect='SET NAMES utf8'
8、设置开机启动
shell> systemctl enable mysqld shell> systemctl daemon-reload
二、RPM
除安装过程外,其他步骤和yum方式安装相同,不再赘述
0、删除已旧版本
略
1、下载MySQL安装包
下载地址:dev.mysql.com/downloads/m…
选择对应的版本:

shell> wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.26-1.el7.x86_64.rpm-bundle.tar
2、安装MySQL
解压(解打包)
shell> tar -xvf mysql-5.7.26-1.el7.x86_64.rpm-bundle.tar tar -xvf mysql-5.7.26-1.el7.x86_64.rpm-bundle.tar mysql-community-embedded-devel-5.7.26-1.el7.x86_64.rpm mysql-community-libs-5.7.26-1.el7.x86_64.rpm mysql-community-embedded-5.7.26-1.el7.x86_64.rpm mysql-community-test-5.7.26-1.el7.x86_64.rpm mysql-community-embedded-compat-5.7.26-1.el7.x86_64.rpm mysql-community-common-5.7.26-1.el7.x86_64.rpm mysql-community-devel-5.7.26-1.el7.x86_64.rpm mysql-community-client-5.7.26-1.el7.x86_64.rpm mysql-community-server-5.7.26-1.el7.x86_64.rpm
我们主要安装的是这四个(如果有需要也可以一并安装其它的):
mysql-community-libs-5.7.26-1.el7.x86_64.rpm mysql-community-common-5.7.26-1.el7.x86_64.rpm mysql-community-client-5.7.26-1.el7.x86_64.rpm mysql-community-server-5.7.26-1.el7.x86_64.rpm
如果不想下载rpm-bundle,官网也提供单独的rpm下载链接
安装
各rpm包是有依赖关系的,所以需要按照一定顺序进行安装,安装期间如果提示缺少哪些依赖也要先安装相应的包:
shell> rpm -ivh mysql-community-common-5.7.26-1.el7.x86_64.rpm shell> rpm -ivh mysql-community-libs-5.7.26-1.el7.x86_64.rpm shell> rpm -ivh mysql-community-client-5.7.26-1.el7.x86_64.rpm shell> rpm -ivh mysql-community-server-5.7.26-1.el7.x86_64.rpm
还有一种简单的方式,可以自动处理各个包之间的依赖关系并自动下载缺少的依赖:
shell> yum install mysql-community-{server,client,common,libs}-*注意:上面的yum install命令需要在tar解压之后的各个rpm包所在目录内执行,否则就变成yum方式安装了,需要配置MySQL的yum源并且速度很慢,还要当前机器支持外网访问
3、设置
略
三、tar.gz
0、删除旧版本
略
1、下载
下载地址:dev.mysql.com/downloads/m…
选择对应的版本:

shell> wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz
2、安装&配置:
依赖
MySQL依赖libaio库,如果没有先安装一下:
shell> yum install libaio
创建mysql用户
不需要登录的一个系统账号,启动MySQL服务时会使用该账号
shell> groupadd mysql shell> useradd -r -g mysql -s /bin/false mysql
解压并创建链接
shell> cd /usr/local shell> tar zxvf /path/to/mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz shell> ln -s mysql-5.7.26-linux-glibc2.12-x86_64/ mysql
创建mysql-files目录
这一步并不是必须的,可以设置secure_file_priv的值指向该目录(用于限制数据导入导出操作的目录)
shell> cd mysql shell> mkdir mysql-files shell> chown mysql:mysql mysql-files shell> chmod 750 mysql-files
初始化
shell> bin/mysqld --initialize --user=mysql
如果初始化时报错如下:
error while loading shared libraries: libnuma.so.1: cannot open shared object file: No such file or directory
是因为libnuma没有安装(或者默认安装的是32位),我们这里需要64位的:
shell> yum install numactl.x86_64
执行完后重新初始化即可 初始化成功后返回结果中有一行包含初始密码,第一次登录时要用到它:
A temporary password is generated for root@localhost: 8M0ary878s*U
启用SSL(非必须)
shell> bin/mysql_ssl_rsa_setup
启动
shell> bin/mysqld_safe --user=mysql &
查看进程可以看到一些默认参数,可以在配置文件中修改这些参数
shell> ps -ef | grep mysql root 14604 12719 0 00:03 pts/0 00:00:00 /bin/sh bin/mysqld_safe --user=mysql mysql 14674 14604 0 00:03 pts/0 00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=VM_2_24_centos.err --pid-file=VM_2_24_centos.pid
设置环境变量
避免每次执行mysql命令都要加上路径,在/etc/profile中添加:
export PATH=$PATH:/usr/local/mysql/bin
设置为服务
shell> cp support-files/mysql.server /etc/init.d/mysqld shell> service mysqld start|stop|restart|status
开机启动
shell> chkconfig --add mysqld shell> chkconfig --list mysqld mysqld 0:关 1:关 2:开 3:开 4:开 5:开 6:关
其他配置与yum、rpm相同,不再赘述
四、源码安装
就别费这个劲了吧...
结束语
我们不是Linux运维专家,也不是MySQL专家,生在这个年代也不知算是幸福还是不幸,线上的环境已经越来越少有人(主要指平时写代码的人)手动去搞这些数据库、中间件的安装配置了,为什么呢?因为各种云产品实在是太方便了呀,一般的公司也不会差这几个钱,既方便又稳定,何乐而不为呢~但是我们自己搞一搞用于自己测试还是必要的,而且还有不少公司的开发环境、测试环境偶尔还是需要手动搞一下的,当然,还有那些个自己搞机房的巨头们。
那我们既然不是专家,上面所写的内容如果有纰漏也是在所难免的,如果被看到了还希望能够及时批评指正~
更多MySQL相关技术文章,请访问MySQL教程栏目进行学习!
The above is the detailed content of Detailed explanation of installing MySQL on CentOS. For more information, please follow other related articles on the PHP Chinese website!
Hot AI Tools
Undresser.AI Undress
AI-powered app for creating realistic nude photos
AI Clothes Remover
Online AI tool for removing clothes from photos.
Undress AI Tool
Undress images for free
Clothoff.io
AI clothes remover
Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!
Hot Article
Hot Tools
Notepad++7.3.1
Easy-to-use and free code editor
SublimeText3 Chinese version
Chinese version, very easy to use
Zend Studio 13.0.1
Powerful PHP integrated development environment
Dreamweaver CS6
Visual web development tools
SublimeText3 Mac version
God-level code editing software (SublimeText3)
Hot Topics
1389
52
How to optimize CentOS HDFS configuration
Apr 14, 2025 pm 07:15 PM
Improve HDFS performance on CentOS: A comprehensive optimization guide to optimize HDFS (Hadoop distributed file system) on CentOS requires comprehensive consideration of hardware, system configuration and network settings. This article provides a series of optimization strategies to help you improve HDFS performance. 1. Hardware upgrade and selection resource expansion: Increase the CPU, memory and storage capacity of the server as much as possible. High-performance hardware: adopts high-performance network cards and switches to improve network throughput. 2. System configuration fine-tuning kernel parameter adjustment: Modify /etc/sysctl.conf file to optimize kernel parameters such as TCP connection number, file handle number and memory management. For example, adjust TCP connection status and buffer size
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.
How to check CentOS HDFS configuration
Apr 14, 2025 pm 07:21 PM
Complete Guide to Checking HDFS Configuration in CentOS Systems This article will guide you how to effectively check the configuration and running status of HDFS on CentOS systems. The following steps will help you fully understand the setup and operation of HDFS. Verify Hadoop environment variable: First, make sure the Hadoop environment variable is set correctly. In the terminal, execute the following command to verify that Hadoop is installed and configured correctly: hadoopversion Check HDFS configuration file: The core configuration file of HDFS is located in the /etc/hadoop/conf/ directory, where core-site.xml and hdfs-site.xml are crucial. use
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.
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
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
Centos install mysql
Apr 14, 2025 pm 08:09 PM
Installing MySQL on CentOS involves the following steps: Adding the appropriate MySQL yum source. Execute the yum install mysql-server command to install the MySQL server. Use the mysql_secure_installation command to make security settings, such as setting the root user password. Customize the MySQL configuration file as needed. Tune MySQL parameters and optimize databases for performance.


