一、安装mariabd数据库

使用以下命令安装mariadb数据库:
sudo apt-get install mariadb-server
之后会提示安装,如下图所示,键入Y即可安装成功
在这里插入图片描述

查看数据库状态
service mariadb status
出现如下图所示即代表数据库安装成功
在这里插入图片描述

二、配置数据库

安装完数据库需要对数据进行安全设置

// 配置数据库安全设置
sudo mysql_secure_installation
pi@raspberrypi:~ $ sudo mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

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.

Enter current password for root (enter for none): #初次运行直接回车,设置密码
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.

You already have your root account protected, so you can safely answer 'n'.

Switch to unix_socket authentication [Y/n] n		#root帐户已受保护,回答“n”
 ... skipping.

You already have your root account protected, so you can safely answer 'n'.

Change the root password? [Y/n] y		#是否设置root密码,输入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.

Remove anonymous users? [Y/n] y		#是否删除匿名用户
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] n		#是否禁止root用户远程登录
 ... 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.

Remove test database and access to it? [Y/n] y		#是否删除test数据库
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y		#是否重新加载权限表
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

在这里插入图片描述

添加账号/给账号授权

grant all privileges on *.* to root@'%' identified by 'admin' with grant option;

刷新权限,使刚才的命令生效

flush privileges;

解释一下,上面这段sql中 grant all privileges on *.*,其中*.*表示所有库的所有表,如需指定库表可对应修改,如访问test库的user表,则可修改成grant all privileges on test.user;这段sql中grant all privileges on *.* to root@'%',其中root@'%'表示root账户可以通过所有的IP地址来对数据库进行访问,如需指定root账户的IP访问,如指定10.18.18.12这个IP对数据库进行访问,则可修改成root@'10.18.18.12'

三、 关于MariaDB 数据库没有办法远程访问的问题

在 RaspberryPi(树莓派) 上安装 MariaDB 或者 MySQL 数据库的时候,设置的用户没有办法通过网络远程登录。首先确定的是,用户已经设置了访问权限。
提示的错误信息是:
ERROR 2002 (HY000): Can’t connect to MySQL server on ‘xx.xx.xx.xx’ (115)
其中地址 xx.xx.xx.xx 是 RaspberryPi(树莓派)的地址
这是一个有关 RaspberryPi(树莓派)的配置问题。
使用命令sudo vi /etc/mysql/mariadb.conf.d/50-server.cnf编辑。
找到 bind-address = 127.0.0.1 这一行,注释掉,如下图所示
在这里插入图片描述
注:如果不注释掉这一行,MariaDB 数据库只会绑定 127.0.0.1 的地址,这样只能在 localhost上登录

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐