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 repolist
You 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!

MySQL is suitable for beginners to learn database skills. 1. Install MySQL server and client tools. 2. Understand basic SQL queries, such as SELECT. 3. Master data operations: create tables, insert, update, and delete data. 4. Learn advanced skills: subquery and window functions. 5. Debugging and optimization: Check syntax, use indexes, avoid SELECT*, and use LIMIT.

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.

SQL is used to interact with MySQL database to realize data addition, deletion, modification, inspection and database design. 1) SQL performs data operations through SELECT, INSERT, UPDATE, DELETE statements; 2) Use CREATE, ALTER, DROP statements for database design and management; 3) Complex queries and data analysis are implemented through SQL to improve business decision-making efficiency.

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA

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.

The steps to build a MySQL database include: 1. Create a database and table, 2. Insert data, and 3. Conduct queries. First, use the CREATEDATABASE and CREATETABLE statements to create the database and table, then use the INSERTINTO statement to insert the data, and finally use the SELECT statement to query the data.

MySQL is suitable for beginners because it is easy to use and powerful. 1.MySQL is a relational database, and uses SQL for CRUD operations. 2. It is simple to install and requires the root user password to be configured. 3. Use INSERT, UPDATE, DELETE, and SELECT to perform data operations. 4. ORDERBY, WHERE and JOIN can be used for complex queries. 5. Debugging requires checking the syntax and use EXPLAIN to analyze the query. 6. Optimization suggestions include using indexes, choosing the right data type and good programming habits.


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

WebStorm Mac version
Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download
The most popular open source editor