Home >Database >Mysql Tutorial >Detailed explanation of installing MySQL on CentOS

Detailed explanation of installing MySQL on CentOS

步履不停
步履不停Original
2019-06-18 14:11:263189browse

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

in the /

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:

  1. 使用复杂密码,MySQL默认的密码策略是要包含数字、字母及特殊字符;

  2. 如果只是测试用,不想用那么复杂的密码,可以修改默认策略,即validate_password_policy(以及validate_password_length等相关参数),使其支持简单密码的设定,具体方法可以自行百度;

  3. 修改配置文件/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…

选择对应的版本:

Detailed explanation of installing MySQL on CentOS

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…

选择对应的版本:

Detailed explanation of installing MySQL on CentOS

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!

Statement:
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