自 CentOS 7 发布以来,系统的软件源中默认为 MariaDB ,那么应该如何在 CentOS 7 上安装 MySQL 呢?
要在 CentOS 7 上安装 MySQL ,需要将 MySQL 的 Yum 安装源,添加到系统软件源列表 。在本文中,牛奇网将向您展示,如何在 CentOS 7 上安装 MySQL 8.0 和 MySQL 5.7。
在 CentOS 7 上安装 MySQL 8.0
要在 CentOS 7 服务器上安装 MySQL 8.0 版,请按照下面的步骤进行操作:
使用以下命令启用 MySQL 8.0 软件源:
sudo yum localinstall https://dev.mysql.com/get/mysql80-community-release-el7-1.noarch.rpm
用 yum 安装 MySQL 8.0 包:
sudo yum install mysql-community-server
在安装过程中,yum 可能会提示您导入 MySQL GPG 密钥。键入 y
并点击回车确认。
在 CentOS 7 上安装 MySQL 5.7
要在 CentOS 7 服务器上安装 MySQL 5.7 版 ,请按照以下步骤操作:
使用以下命令启用 MySQL 5.7 存储库:
sudo yum localinstall https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
安装 MySQL 5.7 包:使用 yum 安装 MySQL :
sudo yum install mysql-community-server
启动 MySQL
安装完成后,启动 MySQL 服务,并使其在系统启动时自动启动:
sudo systemctl enable mysqld
sudo systemctl start mysqld
可以通过执行以下命令,检查 MySQL 服务状态:
sudo systemctl status mysqld
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since Wed 2018-05-23 11:02:43 UTC; 14min ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 4293 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
Main PID: 4310 (mysqld)
Status: "SERVER_OPERATING"
CGroup: /system.slice/mysqld.service
└─4310 /usr/sbin/mysqld
MySQL 安全配置
首次启动 MySQL 服务器时,会为 MySQL 的 root 用户生成一个临时密码。可以通过运行以下命令找到该密码:
sudo grep 'temporary password' /var/log/mysqld.log
输出应如下所示:
2018-05-23T10:59:51.251159Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: q&0)V!?fjksL
记下密码,因为下一个命令会要求输入该临时 root 密码。
运行 mysql_secure_installation
命令,对 MySQL 进行安全配置:
sudo mysql_secure_installation
Securing the MySQL server deployment.
Enter password for user root:
输入临时密码后,系统会要求您为 root 用户设置新密码。密码长度至少为 8 个字符,并且至少包含一个大写字母、一个小写字母、一个数字和一个特殊字符。
The existing password for the user account root has expired. Please set a new password.
New password:
Re-enter new password:
根据安全配置向导的提示,删除匿名用户、限制 root 用户访问本地计算机并删除测试数据库。
从命令行连接到 MySQL
以 root 用户身份登录 MySQL 服务器:
mysql -u root -p
系统将提示输入之前步骤中设置的 root 密码。
输入密码后,将会进入 mysql shell,如下所示:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 8.0.11 MySQL Community Server - GPL
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
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 shell 后,可以通过执行以下命令来创建新数据库:
CREATE DATABASE new_database;
Query OK, 1 row affected (0.00 sec)
创建表
现在我们创建了一个数据库,我们可以创建一个表来存储数据。
在运行创建表的 SQL 语句之前,我们需要连接到数据库:
use new_database;
在这个例子中,我们将创建一个简单的表,将其命名为 contacts
,该表包含三个字段,分别为 id
,name
和 email
:
CREATE TABLE contacts (
id INT PRIMARY KEY,
name VARCHAR(30),
email VARCHAR(30)
);
Query OK, 1 row affected (0.00 sec)
通过上述步骤,牛奇网向您展示了如何在 CentOS 7 服务器上安装和配置 MySQL 服务器。希望能够对您有所帮助。
作者:牛奇网,本站文章均为辛苦原创,在此严正声明,本站内容严禁采集转载,面斥不雅请好自为之,本文网址:https://www.niuqi360.com/mysql/%e5%a6%82%e4%bd%95%e5%9c%a8-centos-7-%e4%b8%8a%e5%ae%89%e8%a3%85-mysql/