Linux系统之安装mysql数据库

Posted 江湖有缘

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux系统之安装mysql数据库相关的知识,希望对你有一定的参考价值。

Linux系统之安装mysql数据库

一、查看系统版本

[root@node ~]# cat /etc/os-release 
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
PRETTY_NAME="CentOS Linux 7 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:7"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"

CENTOS_MANTISBT_PROJECT="CentOS-7"
CENTOS_MANTISBT_PROJECT_VERSION="7"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="7"


二、检查yum仓库

[root@node ~]# yum repolist all |grep enable
!base/7/x86_64                       CentOS-7 - Base             enabled: 10,072
!docker-ce-stable/7/x86_64           Docker CE Stable - x86_64   enabled:    183
!extras/7/x86_64                     CentOS-7 - Extras           enabled:    515
!mongodb-org/7                       MongoDB Repository          enabled:    187
!updates/7/x86_64                    CentOS-7 - Updates          enabled:  4,346

三、检查mysql是否安装

[root@node yum.repos.d]# rpm -qa | grep mysql
[root@node yum.repos.d]# 

四、配置mysql的yum源

1.配置yum仓库

[root@node yum.repos.d]# yum localinstall https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
Loaded plugins: fastestmirror, langpacks
mysql57-community-release-el7-11.noarch.rpm                                                                                 |  25 kB  00:00:00     
Examining /var/tmp/yum-root-pXEqvj/mysql57-community-release-el7-11.noarch.rpm: mysql57-community-release-el7-11.noarch
Marking /var/tmp/yum-root-pXEqvj/mysql57-community-release-el7-11.noarch.rpm to be installed
Resolving Dependencies
--> Running transaction check
---> Package mysql57-community-release.noarch 0:el7-11 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

===================================================================================================================================================
 Package                                 Arch                 Version                 Repository                                              Size
===================================================================================================================================================
Installing:
 mysql57-community-release               noarch               el7-11                  /mysql57-community-release-el7-11.noarch                31 k

Transaction Summary
===================================================================================================================================================
Install  1 Package

Total size: 31 k
Installed size: 31 k
Is this ok [y/d/N]: y
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : mysql57-community-release-el7-11.noarch                                                                                         1/1 
  Verifying  : mysql57-community-release-el7-11.noarch                                                                                         1/1 

Installed:
  mysql57-community-release.noarch 0:el7-11                                                                                                        

Complete!

2.查看mysql-community.repo

[root@node yum.repos.d]# cat mysql-community.repo 
[mysql-connectors-community]
name=MySQL Connectors Community
baseurl=http://repo.mysql.com/yum/mysql-connectors-community/el/7/$basearch/
enabled=1
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

[mysql-tools-community]
name=MySQL Tools Community
baseurl=http://repo.mysql.com/yum/mysql-tools-community/el/7/$basearch/
enabled=1
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

# Enable to use MySQL 5.5
[mysql55-community]
name=MySQL 5.5 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.5-community/el/7/$basearch/
enabled=0
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

# Enable to use MySQL 5.6
[mysql56-community]
name=MySQL 5.6 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.6-community/el/7/$basearch/
enabled=0
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

[mysql57-community]
name=MySQL 5.7 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/$basearch/
enabled=1
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

[mysql80-community]
name=MySQL 8.0 Community Server
baseurl=http://repo.mysql.com/yum/mysql-8.0-community/el/7/$basearch/
enabled=0
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

[mysql-tools-preview]
name=MySQL Tools Preview
baseurl=http://repo.mysql.com/yum/mysql-tools-preview/el/7/$basearch/
enabled=0
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

[mysql-cluster-7.5-community]
name=MySQL Cluster 7.5 Community
baseurl=http://repo.mysql.com/yum/mysql-cluster-7.5-community/el/7/$basearch/
enabled=0
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

[mysql-cluster-7.6-community]
name=MySQL Cluster 7.6 Community
baseurl=http://repo.mysql.com/yum/mysql-cluster-7.6-community/el/7/$basearch/
enabled=0
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

3.检查yum仓库状态

[root@node yum.repos.d]# yum repolist all |grep enable
base/7/x86_64                        CentOS-7 - Base             enabled: 10,072
docker-ce-stable/7/x86_64            Docker CE Stable - x86_64   enabled:    183
extras/7/x86_64                      CentOS-7 - Extras           enabled:    515
mongodb-org/7                        MongoDB Repository          enabled:    188
mysql-connectors-community/x86_64    MySQL Connectors Community  enabled:    206
mysql-tools-community/x86_64         MySQL Tools Community       enabled:     94
mysql57-community/x86_64             MySQL 5.7 Community Server  enabled:    624
updates/7/x86_64                     CentOS-7 - Updates          enabled:  4,346


五、安装mysql

1.安装mysql

[root@node yum.repos.d]# yum install -y mysql-server
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.tuna.tsinghua.edu.cn
Resolving Dependencies
--> Running transaction check
---> Package mysql-community-server.x86_64 0:5.7.40-1.el7 will be installed
--> Processing Dependency: mysql-community-client(x86-64) >= 5.7.9 for package: mysql-community-server-5.7.40-1.el7.x86_64
--> Running transaction check
---> Package mysql-community-client.x86_64 0:5.7.40-1.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

===================================================================================================================================================
 Package                                   Arch                      Version                            Repository                            Size
===================================================================================================================================================
Installing:
 mysql-community-server                    x86_64                    5.7.40-1.el7                       mysql57-community                    178 M
Installing for dependencies:
 mysql-community-client                    x86_64                    5.7.40-1.el7                       mysql57-community                     28 M

Transaction Summary
===================================================================================================================================================
Install  1 Package (+1 Dependent package)

Total download size: 207 M
Installed size: 882 M
Downloading packages:
(1/2): mysql-community-client-5.7.40-1.el7.x86_64.rpm                                                                       |  28 MB  00:00:07     
(2/2): mysql-community-server-5.7.40-1.el7.x86_64.rpm                                                                       | 178 MB  00:00:36     
---------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                              5.7 MB/s | 207 MB  00:00:36     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : mysql-community-client-5.7.40-1.el7.x86_64                                                                                      1/2 
  Installing : mysql-community-server-5.7.40-1.el7.x86_64                                                                                      2/2 
  Verifying  : mysql-community-client-5.7.40-1.el7.x86_64                                                                                      1/2 
  Verifying  : mysql-community-server-5.7.40-1.el7.x86_64                                                                                      2/2 

Installed:
  mysql-community-server.x86_64 0:5.7.40-1.el7                                                                                                     

Dependency Installed:
  mysql-community-client.x86_64 0:5.7.40-1.el7                                                                                                     

Complete!

2.启动mysql服务

systemctl enable --now mysqld

3.查看mysql服务状态

[root@node yum.repos.d]# systemctl status mysqld
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since Sat 2022-11-05 15:55:26 CST; 24s ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 57435 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
  Process: 57412 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
 Main PID: 57437 (mysqld)
    Tasks: 27
   Memory: 287.2M
   CGroup: /system.slice/mysqld.service
           └─57437 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid

Nov 05 15:55:23 node systemd[1]: Starting MySQL Server...
Nov 05 15:55:26 node systemd[1]: Started MySQL Server.

六、访问mysql

[root@node yum.repos.d]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \\g.
Your MySQL connection id is 2
Server version: 5.7.40 MySQL Community Server (GPL)

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\\h' for help. Type '\\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.01 sec)

mysql> 

以上是关于Linux系统之安装mysql数据库的主要内容,如果未能解决你的问题,请参考以下文章

Linux系统之安装mariadb方法

MySQL之安装管理操作

linux之安装mysql常用配置

Docker 技术系列之安装多版本Mysql5.6和Mysql5.7

Linux之安装MySQL问题:Mysql Daemon failed to start

Linux系统之安装Linux管理工具inpanel