一、背景
Maria Db是流行的跨平台MySQL数据库管理系统的分支,被认为是MySQL 的完全替代品。Maria Db是由Sun在Sun Micro systems合并期间被Oracle收购后,于2009年由MySQL的一位原始开发人员创建的。今天,Maria Db由Maria Db Foundation和社区贡献者维护和开发,
Maria Db将MySQL替换为Cent OS 7存储库中的默认数据库系统。虽然将MySQL安装到Cent OS 7并不困难,但是如果您只需要一个数据库,建议使用Maria Db进行官方支持,并且与其他存储库软件不兼容的可能性很小。
二、开始之前
$ 表示系统的一般权限,不用使用root 超级管理员权限配置。
要检查您的主机名:
$ hostname -f
$ hostname
第一个命令应显示您的短主机名,第二个命令应显示您的完全限定域名(FQDN)。
更新您的系统:
$ sudo yum update
安装并启动MariaDB
$ 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 -uroot -p
查询数据库列表
$ show databses;
安装到此结束,余下是介绍如何使用配置
保护MariaDB服务器
- 运行
mysql_secure_installation
脚本以解决默认MariaDB安装中的几个安全问题:
sudo mysql_secure_installation
您可以选择更改MariaDB root密码,删除匿名用户帐户,禁用localhost之外的root登录,以及删除测试数据库。建议您对这些选项回答yes
。
使用MariaDB
与MariaDB交互的标准工具是mariadb
客户端,它随mariadb-server
包一起安装。MariaDB客户端通过终端使用。
Root登录
- 以root用户身份登录MariaDB:
mysql -u root -p
- 出现提示时,输入当
mysql_secure_installation
脚本运行时分配的root密码。
然后,您将看到欢迎标题和MariaDB提示,如下所示:
MariaDB [(none)]>
- 要为MariaDB提示生成命令列表,请输入
\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)]>
创建一个新的MariaDB用户和数据库
- 在下面的示例中,
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';
- 然后退出MariaDB:
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;
- 然后退出MariaDB:
exit
重置MariaDB Root密码
如果您忘记了root MariaDB密码,则可以重置密码。
- 停止当前的MariaDB服务器实例,然后使用不要求输入密码的选项重新启动它:
sudo systemctl stop mariadb sudo mysqld_safe --skip-grant-tables &
- 使用MariaDB root帐户重新连接到MariaDB服务器:
mysql -u root
- 使用以下命令重置root的密码。用强密码替换
password
:
use mysql; update user SET PASSWORD=PASSWORD("password") WHERE USER='root'; flush privileges; exit
- 然后重启MariaDB:
sudo systemctl start mariadb
优化MariaDB
MySQL Tuning Primer可用于优化MariaDB服务器。理想情况下,MariaDB实例应该在运行Tuner之前至少运行24小时。实例运行的时间越长,MySQL Tuner给出的建议就越好。
- 该脚本需要安装bc语言:
sudo yum install bc
- 将MySQL Tuner下载到您的主目录并使其可执行:
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服务器的一个很好的起点。
原文地址:https://www.cnblogs.com/sopcce/p/10593564.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。