
树莓派部署MariaDB
MariaDB简介
MariaDB 是一个开源的关系型数据库管理系统,是 MySQL 的一个分支,由 MySQL 的创始人 Michael Widenius(Monty)主导开发,以替代 Oracle 公司的 MySQL 数据库。MariaDB 完全兼容 MySQL,包括 API 和命令行工具,使得从 MySQL 迁移到 MariaDB 非常容易。
MariaDB的安装
更新系统
在安装前先更新系统
sudo apt update
sudo apt upgrade
安装MariaDB
输入以下内容后回车,安装MariaDB
sudo apt install mariadb-server
等待一会就安装完成了
MariaDB的配置
输入以下指令初始化MariaDB
sudo mysql_secure_installation
速通版
Enter current password for root (enter for none):回车
Switch to unix_socket authentication [Y/n]:y
Change the root password? [Y/n]:y
New password: 输入root账号的密码
Re-enter new password: 再次输入,确认密码
Remove anonymous users? [Y/n]: y
Disallow root login remotely? [Y/n]: n
Remove test database and access to it? [Y/n]:n
Reload privilege tables now? [Y/n]:y
Thanks for using MariaDB!感谢您使用 MariaDB!
详细版
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
注意:建议对所有生产环境中的 MariaDB 服务器运行此脚本的全部内容!请仔细阅读每一步!
In order to log into MariaDB to secure it, we'll need the current password for the root user. If you've just installed MariaDB, and haven't set the root password yet, you should just press enter here.
为了登录 MariaDB 以进行安全设置,我们需要当前root 用户的密码。如果您刚刚安装了 MariaDB,并且尚未设置 root 密码,您应该直接按回车键。
Enter current password for root (enter for none):
请输入 root 的当前密码(按回车键表示无):
OK, successfully used password, moving on...
密码使用成功,继续进行下一步...
Setting the root password or using the unix_socket ensures that nobody can log into the MariaDB root user without the proper authorisation.
设置 root 密码或使用 unix_socket 可以确保没有适当授权的人无法登录 MariaDB root 用户。
You already have your root account protected, so you can safely answer 'n'.
您的 root 账户已经受到保护,因此您可以安全地回答 'n'。
Switch to unix_socket authentication [Y/n] y
是否切换到 unix_socket 身份验证 [Y/n] y
Enabled successfully!
已成功启用!
Reloading privilege tables..
重新加载权限表..
... Success!
... 成功!
You already have your root account protected, so you can safely answer 'n'.
您的 root 账户已经受到保护,因此您可以安全地回答 'n'。
Change the root password? [Y/n] y
是否更改 root 密码?[Y/n] y
New password:
新密码:
Re-enter new password:
重新输入新密码:
Password updated successfully!
密码更新成功!
Reloading privilege tables..
重新加载权限表..
... Success!
... 成功!
By default, a MariaDB installation has an anonymous user, allowing anyone to log into MariaDB without having to have a user account created for them. This is intended only for testing, and to make the installation go a bit smoother. You should remove them before moving into a production environment.
默认情况下,MariaDB 安装有一个匿名用户,允许任何人无需创建用户账户即可登录 MariaDB。这仅用于测试,使安装过程更加顺利。您应在正式使用之前删除它们。
Remove anonymous users? [Y/n] y
是否删除匿名用户?[Y/n] y
... Success!
... 成功!
Normally, root should only be allowed to connect from 'localhost'. This
通常,root 应仅允许从 'localhost' 连接。
ensures that someone cannot guess at the root password from the network.
这可以确保有人无法从网络上猜测 root 密码。
Disallow root login remotely? [Y/n] n
是否禁止 root 远程登录?[Y/n] n
... skipping.
... 跳过。
By default, MariaDB comes with a database named 'test' that anyone can access. This is also intended only for testing, and should be removed before moving into a production environment.
默认情况下,MariaDB 配备了一个名为 'test' 的数据库,任何人都可以访问。这也仅用于测试,应在正式使用之前删除。
Remove test database and access to it? [Y/n] n
是否删除 test 数据库及其访问权限?[Y/n] n
... skipping.
... 跳过。
Reloading the privilege tables will ensure that all changes made so far
重新加载权限表将确保到目前为止所做的所有更改
will take effect immediately.
立即生效。
Reload privilege tables now? [Y/n] y
是否现在重新加载权限表?[Y/n] y
... Success!
... 成功!
Cleaning up...
清理中...
All done! If you've completed all of the above steps, your MariaDB
全部完成!如果您已完成上述所有步骤,您的 MariaDB
installation should now be secure.
安装现在应该是安全的。
Thanks for using MariaDB!
感谢您使用 MariaDB!
MariaDB的管理
注意:MariaDB的命令最后需要加上;
来表示一个SQL语句已经结束
开户
先登录到MariaDB,输入后再输入初始化时设置的密码即可登录
mysql -u root -p
MariaDB默认只有root一个用户,想要新建用户就要用到以下的命令
CREATE USER 'newuser'@'%' IDENTIFIED BY 'newpassword';
解释一下什么意思,CREATE USER
是命令,newuser 是用户名,@后的%
是可以从XXX主机连接,%表示可以从任意主机连接,newpassword
是新用户的密码。
比如我要创建一个叫"halo"的用户,登录密码是"123456",只能从192.168.2.255这个主机连接(如果没有配置DHCP的话建议不要指定主机),那我输入的应该是以下的命令
CREATE USER 'halo'@'192.168.2.255' IDENTIFIED BY '123456';
像这样户就开好了
新建数据库
使用CREATE USER
语句来新建数据库
CREATE DATABASE newdatabase CHARACTER SET utf8mb4;
这里newdatabase
是数据库名,utf8mb4
是字符集,适用于存储包括emoji在内的Unicode字符
比如我这里新建一个halouser
数据库
这样新建数据库的工作就完成了
数据库授权
数据库新建之后默认只有root账号可以访问,如果要其他用户(比如我们上面创建的halo用户)有权访问,就需要进行授权
使用GRANT
语句为新用户授予权限
GRANT ALL PRIVILEGES ON newdatabase.* TO 'newuser'@'%';
这条命令授予用户newuser
对newdatabase
数据库的所有权限。
老样子我们再举个例子,给halo
用户授权halouser
数据库就像这样
GRANT ALL PRIVILEGES ON halouser.* TO 'halo'@'192.168.2.255';
完成授权后需要手动刷新权限使权限更改生效
FLUSH PRIVILEGES;
其他常用命令
查看所有用户
这条命令会列出所有用户的用户名和允许连接的主机
SELECT user, host FROM mysql.user;
查看所有数据库
SHOW DATABASES;
这条命令会列出所有数据库的名称
删除数据库
DROP DATABASE database_name;
这条命令会删除叫做database_name
的数据库
销户
DROP USER 'username'@'host';
这条命令会删除允许从host
连接的username
用户
总结
如果你能顺利的完成上述动作,那么恭喜你,你已经成功部署并学会了MariaDB和它的管理方法:)