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。
hostname
hostname -f
第一个命令应显示您的短主机名,第二个命令应显示您的完全限定域名(FQDN)。
sudo yum update
sudo yum install mariadb-server
启用MariaDB以在启动时启动,然后启动该服务:
sudo systemctl enable mariadb
sudo systemctl start mariadb
默认情况下,MariaDB将绑定到localhost(127.0.0.1
)。
注意
允许在公共IP上不受限制地访问MariaDB,但是您可以在
/etc/my.cnf
中通过修改bind-address
参数来更改它侦听的地址。如果您决定将MariaDB绑定到公共IP,则应实施仅允许来自特定IP地址连接的防火墙规则。
mysql_secure_installation
脚本以解决默认MariaDB安装中的几个安全问题: sudo mysql_secure_installation
您可以选择更改MariaDB root密码,删除匿名用户帐户,禁用localhost之外的root登录,以及删除测试数据库。建议您对这些选项回答yes
。
与MariaDB交互的标准工具是mariadb
客户端,它随mariadb-server
包一起安装。MariaDB客户端通过终端使用。
mysql -u root -p
mysql_secure_installation
脚本运行时分配的root密码。然后,您将看到欢迎标题和MariaDB提示,如下所示:
MariaDB [(none)]>
\h
。然后你会看到: 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)]>
testdb
是数据库的名称,testuser
是用户,password
是用户的密码: create database testdb;
create user 'testuser'@localhost identified by 'password';
grant all on testdb.* to 'testuser' identified by 'password';
您可以通过在分配数据库权限时创建用户来缩短此过程:
create database testdb;
grant all on testdb.* to 'testuser' identified by 'password';
exit
testuser
: mysql -u testuser -p
customers
的示例表。这将创建一个表,其中包含INT
整数类型的客户ID字段(对于新记录自动递增,用作主键),以及用于存储客户名称的两个字段: use testdb;
create table customers (customer_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, first_name TEXT, last_name TEXT);
show tables;
exit
如果您忘记了root MariaDB密码,则可以重置密码。
sudo systemctl stop mariadb
sudo mysqld_safe --skip-grant-tables &
mysql -u root
password
: use mysql;
update user SET PASSWORD=PASSWORD("password") WHERE USER='root';
flush privileges;
exit
sudo systemctl start mariadb
MySQL Tuning Primer可用于优化MariaDB服务器。理想情况下,MariaDB实例应该在运行Tuner之前至少运行24小时。实例运行的时间越长,MySQL Tuner给出的建议就越好。
sudo yum install bc
wget http://www.day32.com/MySQL/tuning-primer.sh
chmod u+x tuning-primer.sh
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 删除。