前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >最全的CentOS MariaDB入门教程

最全的CentOS MariaDB入门教程

原创
作者头像
吴凌云
发布2018-08-14 14:42:39
4.2K0
发布2018-08-14 14:42:39
举报

MariaDB是流行的跨平台MySQL数据库管理系统的分支,被认为是MySQL 的完全替代品。MariaDB是由Sun在Sun Microsystems合并期间被Oracle收购后,于2009年由MySQL的一位原始开发人员创建的。今天,MariaDB由MariaDB Foundation和社区贡献者维护和开发,同时腾讯云提供云数据库 MariaDB(TencentDB for MariaDB)让您轻松在云端部署、使用 MariaDB 数据库

MariaDB将MySQL替换为CentOS 7存储库中的默认数据库系统。虽然将MySQL安装到CentOS 7并不困难,但是如果您只需要一个数据库,建议使用MariaDB进行官方支持,并且与其他存储库软件不兼容的可能性很小。

注意: 本教程是为非root用户编写的。需要有使用sudo的权限。

没有服务器的用户可以购买和使用腾讯云服务器或者直接在腾讯云实验室CentOS服务器直接上机体验安装MariaDB。

开始之前

  1. 要检查您的主机名:
代码语言:txt
复制
   hostname
   hostname -f

第一个命令应显示您的短主机名,第二个命令应显示您的完全限定域名(FQDN)。

  1. 更新您的系统:
代码语言:txt
复制
   sudo yum update

安装并启动MariaDB

代码语言:txt
复制
sudo yum install mariadb-server

启用MariaDB以在启动时启动,然后启动该服务:

代码语言:txt
复制
sudo systemctl enable mariadb
sudo systemctl start mariadb

默认情况下,MariaDB将绑定到localhost(127.0.0.1)。

注意

允许在公共IP上不受限制地访问MariaDB,但是您可以在/etc/my.cnf中通过修改bind-address参数来更改它侦听的地址。如果您决定将MariaDB绑定到公共IP,则应实施仅允许来自特定IP地址连接的防火墙规则。

保护MariaDB服务器

  1. 运行mysql_secure_installation脚本以解决默认MariaDB安装中的几个安全问题:
代码语言:txt
复制
   sudo mysql_secure_installation

您可以选择更改MariaDB root密码,删除匿名用户帐户,禁用localhost之外的root登录,以及删除测试数据库。建议您对这些选项回答yes

使用MariaDB

与MariaDB交互的标准工具是mariadb客户端,它随mariadb-server包一起安装。MariaDB客户端通过终端使用。

Root登录

  1. 以root用户身份登录MariaDB:
代码语言:txt
复制
   mysql -u root -p
  1. 出现提示时,输入当mysql_secure_installation脚本运行时分配的root密码。

然后,您将看到欢迎标题和MariaDB提示,如下所示:

代码语言:txt
复制
   MariaDB [(none)]>
  1. 要为MariaDB提示生成命令列表,请输入\h。然后你会看到:
代码语言:txt
复制
   List of all MySQL commands:
   Note that all text commands must be first on line and end with ';'
   ?         (\?) Synonym for `help'.
   clear     (\c) Clear the current input statement.
   connect   (\r) Reconnect to the server. Optional arguments are db and host.
   delimiter (\d) Set statement delimiter.
   edit      (\e) Edit command with $EDITOR.
   ego       (\G) Send command to mysql server, display result vertically.
   exit      (\q) Exit mysql. Same as quit.
   go        (\g) Send command to mysql server.
   help      (\h) Display this help.
   nopager   (\n) Disable pager, print to stdout.
   notee     (\t) Don't write into outfile.
   pager     (\P) Set PAGER [to_pager]. Print the query results via PAGER.
   print     (\p) Print current command.
   prompt    (\R) Change your mysql prompt.
   quit      (\q) Quit mysql.
   rehash    (\#) Rebuild completion hash.
   source    (\.) Execute an SQL script file. Takes a file name as an argument.
   status    (\s) Get status information from the server.
   system    (\!) Execute a system shell command.
   tee       (\T) Set outfile [to_outfile]. Append everything into given outfile.
   use       (\u) Use another database. Takes database name as argument.
   charset   (\C) Switch to another charset. Might be needed for processing binlog with multi-byte charsets.
   warnings  (\W) Show warnings after every statement.
   nowarning (\w) Don't show warnings after every statement.
   
   For server side help, type 'help contents'
   
   MariaDB [(none)]>

创建一个新的MariaDB用户和数据库

  1. 在下面的示例中,testdb是数据库的名称,testuser是用户,password是用户的密码:
代码语言:txt
复制
   create database testdb;
   create user 'testuser'@localhost identified by 'password';
   grant all on testdb.* to 'testuser' identified by 'password';

您可以通过在分配数据库权限时创建用户来缩短此过程:

代码语言:txt
复制
   create database testdb;
   grant all on testdb.* to 'testuser' identified by 'password';
  1. 然后退出MariaDB:
代码语言:txt
复制
   exit

创建示例表

  1. 重新登录testuser
代码语言:txt
复制
   mysql -u testuser -p
  1. 创建一个名为customers的示例表。这将创建一个表,其中包含INT整数类型的客户ID字段(对于新记录自动递增,用作主键),以及用于存储客户名称的两个字段:
代码语言:txt
复制
   use testdb;
   create table customers (customer_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, first_name TEXT, last_name TEXT);
  1. 查看新表:
代码语言:txt
复制
   show tables;
  1. 然后退出MariaDB:
代码语言:txt
复制
   exit

重置MariaDB Root密码

如果您忘记了root MariaDB密码,则可以重置密码。

  1. 停止当前的MariaDB服务器实例,然后使用不要求输入密码的选项重新启动它:
代码语言:txt
复制
   sudo systemctl stop mariadb
   sudo mysqld_safe --skip-grant-tables &
  1. 使用MariaDB root帐户重新连接到MariaDB服务器:
代码语言:txt
复制
   mysql -u root
  1. 使用以下命令重置root的密码。用强密码替换password
代码语言:txt
复制
   use mysql;
   update user SET PASSWORD=PASSWORD("password") WHERE USER='root';
   flush privileges;
   exit
  1. 然后重启MariaDB:
代码语言:txt
复制
   sudo systemctl start mariadb

优化MariaDB

MySQL Tuning Primer可用于优化MariaDB服务器。理想情况下,MariaDB实例应该在运行Tuner之前至少运行24小时。实例运行的时间越长,MySQL Tuner给出的建议就越好。

  1. 该脚本需要安装bc语言:
代码语言:txt
复制
   sudo yum install bc
  1. 将MySQL Tuner下载到您的主目录并使其可执行:
代码语言:txt
复制
   wget http://www.day32.com/MySQL/tuning-primer.sh
   chmod u+x tuning-primer.sh
  1. 运行它:
代码语言:txt
复制
   sudo ./tuning-primer.sh

系统将询问您是否要针对与/var/lib/mysql/mysql.sock不同的MySQL socket 运行脚本。选择N。然后会询问您是否有登录信息。输入y,然后询问时输入凭据。

MySQL Tuning Primer是优化MariaDB服务器的一个很好的起点。

更多信息

有关此主题的其他信息,您可能需要参考以下资源,想学习更多如何使用MariaDB的用户请前往腾讯云+社区学习更多知识。


参考文献:《How to Install MariaDB on CentOS 7》

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 开始之前
  • 安装并启动MariaDB
  • 保护MariaDB服务器
  • 使用MariaDB
    • Root登录
      • 创建一个新的MariaDB用户和数据库
        • 创建示例表
        • 重置MariaDB Root密码
        • 优化MariaDB
        • 更多信息
        相关产品与服务
        云数据库 SQL Server
        腾讯云数据库 SQL Server (TencentDB for SQL Server)是业界最常用的商用数据库之一,对基于 Windows 架构的应用程序具有完美的支持。TencentDB for SQL Server 拥有微软正版授权,可持续为用户提供最新的功能,避免未授权使用软件的风险。具有即开即用、稳定可靠、安全运行、弹性扩缩等特点。
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档