微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

6步在CentOS安装和配置MariaDB MySQL

MariaDB MysqL软件包

以下是三个主要的MariaDB包:

  • mariadb-5.5.52-1.el7.x86_64 - 这包含几个MysqL客户端程序和实用程序。

  • mariadb-server-5.5.52-1.el7.x86_64 - 这是主要的MariaDB MysqL数据库服务器。

  • mariadb-libs-5.5.52-1.el7.x86_64 - 包含客户机程序界面所需的共享库。

#yuminfomariadb-server
Loadedplugins:fastestmirror
Loadingmirrorspeedsfromcachedhostfile
*base:repos-va.psychz.net
*extras:linux.cc.lehigh.edu
*updates:mirror.us.leaseweb.net
AvailablePackages
Name:mariadb-server
Arch:x86_64
Epoch:1
Version:5.5.52
Release:1.el7
Size:11M
Repo:base/7/x86_64

2.安装MariaDB MysqL服务器

使用yum install安装如下所示的MariaDB MysqL服务器软件包。

#yuminstallmariadb-server

此时,在这个服务器上,已经安装了mariadb-server以及以下依赖的包。

mariadb-server.x86_641:5.5.52-1.el7
mariadb-libs.x86_641:5.5.52-1.el7
mariadb.x86_641:5.5.52-1.el7
libaio.x86_640:0.3.109-13.el7
perl-dbd-mysql.x86_640:4.023-5.el7
perl-DBI.x86_640:1.627-4.el7
perl-Compress-Raw-Bzip2.x86_640:2.061-3.el7
perl-Compress-Raw-Zlib.x86_641:2.061-4.el7
perl-Data-Dumper.x86_640:2.145-3.el7
perl-IO-Compress.noarch0:2.061-2.el7
perl-Net-Daemon.noarch0:0.48-5.el7
perl-plRPC.noarch0:0.2020-14.el7

验证确保这已经安装了三个重要的MariaDB MysqL包。

#rpm-qa|grep-imaria
mariadb-5.5.52-1.el7.x86_64
mariadb-server-5.5.52-1.el7.x86_64
mariadb-libs-5.5.52-1.el7.x86_64

3.启动MariaDB数据库

如下所示,mariadb数据库服务器模块已加载,但尚未启动。

#systemctlstatusmariadb
?mariadb.service-MariaDBdatabaseserver
Loaded:loaded(/usr/lib/systemd/system/mariadb.service;disabled;vendorpreset:disabled)
Active:inactive(dead)

使用systemctl启动MariaDB MysqL服务器,如下:

#systemctlstartmariadb

验证systemctl状态以确保mariadb数据库服务器已成功启动

#systemctlstatusmariadb
?mariadb.service-MariaDBdatabaseserver
Loaded:loaded(/usr/lib/systemd/system/mariadb.service;disabled;vendorpreset:disabled)
Active:active(running)sinceThu2017-06-2618:26:35UTC;13sago
Process:4049ExecStartPost=/usr/libexec/mariadb-wait-ready$MAINPID(code=exited,status=0/SUCCESS)
Process:3969ExecStartPre=/usr/libexec/mariadb-prepare-db-dir%n(code=exited,status=0/SUCCESS)
MainPID:4048(MysqLd_safe)
CGroup:/system.slice/mariadb.service
+-4048/bin/sh/usr/bin/MysqLd_safe--basedir=/usr
+-4206/usr/libexec/MysqLd--basedir=/usr--datadir=/var/lib/MysqL--plugin-dir=/usr/lib64/MysqL/plugin--log-error=/var/log/mariadb/ma...

Aug1515:20:30deploymariadb-prepare-db-dir[3969]:ThelatestinformationaboutMariaDBisavailableathttp://mariadb.org/.
Aug1515:20:30deploymariadb-prepare-db-dir[3969]:YoucanfindadditionalinformationabouttheMysqLpartat:
Aug1515:20:30deploymariadb-prepare-db-dir[3969]:http://dev.MysqL.com
Aug1515:20:30deploymariadb-prepare-db-dir[3969]:SupportMariaDBdevelopmentbybuyingsupport/newfeaturesfromMariaDB
Aug1515:20:30deploymariadb-prepare-db-dir[3969]:CorporationAb.Youcancontactusaboutthisatsales@mariadb.com.
Aug1515:20:30deploymariadb-prepare-db-dir[3969]:Alternativelyconsiderjoiningourcommunitybaseddevelopmenteffort:
Aug1515:20:30deploymariadb-prepare-db-dir[3969]:http://mariadb.com/kb/en/contributing-to-the-mariadb-project/
Aug1515:20:30deployMysqLd_safe[4048]:17060115:20:33MysqLd_safeLoggingto'/var/log/mariadb/mariadb.log'.
Aug1515:20:30deployMysqLd_safe[4048]:17060115:20:33MysqLd_safeStartingMysqLddaemonwithdatabasesfrom/var/lib/MysqL
Aug1515:20:35deploysystemd[1]:StartedMariaDBdatabaseserver.


4.连接并验证MariaDB Server

使用如下所示的MySQL命令使用MysqL的root用户连接到数据库

#MysqL-uroot
WelcometotheMariaDBmonitor.Commandsendwith;or\g.
YourMariaDBconnectionidis2
Serverversion:5.5.52-MariaDBMariaDBServer

copyright(c)2000,2016,Oracle,MariaDBCorporationAbandothers.

Type'help;'or'\h'forhelp.Type'\c'toclearthecurrentinputstatement.
MariaDB[(none)]>


以下show database命令将显示认的MysqL数据库

MariaDB[(none)]>showdatabases;
+--------------------+
|Database|
+--------------------+
|information_schema|
|MysqL|
|performance_schema|
|test|
+--------------------+


5.执行MariaDB Post Installation步骤

从上方可以看出,认情况下,安装不会为MysqL root帐户分配任何密码。

要设置MysqL root用户密码并在数据库上执行其他安全配置,请执行如下所示的MysqL_secure_installation脚本。

#/usr/bin/MysqL_secure_installation

NOTE:RUNNINgalLPARTSOFTHISSCRIPTISRECOMMENDEDFORALLMariaDB
SERVERSINPRODUCTIONUSE!PLEASEREADEACHSTEPCAREFULLY!

InordertologintoMariaDBtosecureit,we'llneedthecurrent
passwordfortherootuser.Ifyou'vejustinstalledMariaDB,and
youhaven'tsettherootpasswordyet,thepasswordwillbeblank,soyoushouldjustpressenterhere.


由于这是我们第一次运行此脚本,因此没有为MysqL root帐户分配密码。所以,请在这里输入

Entercurrentpasswordforroot(enterfornone):
OK,successfullyusedpassword,movingon...

SettingtherootpasswordensuresthatnobodycanlogintotheMariaDB
rootuserwithouttheproperauthorisation.



在这个阶段,说“y”为MysqL根帐户分配密码。之后输入密码。

请注意,这个MysqL root帐户与linux root帐户不同。所以,我们在这里设置MysqL root帐号的密码,这与Linux root帐号无关。

Setrootpassword?[Y/n]y
Newpassword:
Re-enternewpassword:
Passwordupdatedsuccessfully!
Reloadingprivilegetables..
...Success!

作为认安装的一部分,MysqL安装匿名用户,无需真实用户即可登录数据库所以我们应该删除这个用户

Removeanonymoususers?[Y/n]y
...Success!

normally,rootshouldonlybeallowedtoconnectfrom'localhost'.This
ensuresthatsomeonecannotguessattherootpasswordfromthenetwork.

可以想像,MysqL root帐号将可以访问所有的MysqL数据库所以,保持这个安全是很重要的。此外,我们应该确保来自其他服务器的远程客户端不允许使用此MysqL root帐户进行连接。

相反,只有本地主机(其中安装了MysqL服务器)可以使用root帐户进行连接。所以我们应该远程禁止root登录

disallowrootloginremotely?[Y/n]y
...Success!


这是认的测试数据库,我们应该删除它。

Removetestdatabaseandaccesstoit?[Y/n]y
-Droppingtestdatabase...
...Success!
-Removingprivilegesontestdatabase...
...Success!

在这里输入y,以确保我们所做的所有更改将立即生效。

ReloadprivilegetablesNow?[Y/n]y
...Success!

Cleaningup...

Alldone!Ifyou'vecompletedalloftheabovesteps,yourMariaDB
installationshouldNowbesecure.

ThanksforusingMariaDB

6.验证MysqL根访问

现在,如果您连接到没有root密码的MysqL,您将收到以下访问被拒绝的错误消息。

#MysqL-uroot
ERROR1045(28000):Accessdeniedforuser'root'@'localhost'(usingpassword:NO)

要指定密码,请使用-p选项,如下所示。这将提示用户输入密码。

#MysqL-uroot-p
Enterpassword:

另外,从下面的show databases命令可以看出,测试数据库现在被删除了。

MariaDB[(none)]>showdatabases;
+--------------------+
|Database|
+--------------------+
|information_schema|
|MysqL|
|performance_schema|
+--------------------+

如果要在MySQL命令行中传递密码,请在-p选项旁边指定,如下所示。


#MysqL-uroot-pMySecurePassword

注意:-p和密码之间没有空格。这可能会导致一些混淆,因为我们在-u和username之间有空格。但是,-p和密码之间没有空格。

原文地址:https://www.jb51.cc/centos/376201.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。