mirror of https://github.com/jkjoy/sunpeiwen.git
58 lines
1.3 KiB
Markdown
58 lines
1.3 KiB
Markdown
---
|
||
title: Mariadb创建数据库、用户及授权
|
||
tags: Mariadb
|
||
categories: 分享
|
||
abbrlink: dfdf0a76
|
||
date: 2023-10-06 11:01:17
|
||
cover:
|
||
---
|
||
|
||
在测试`SForum`程序中使用到的部分关于数据库的操作
|
||
## 安装Mariadb服务器
|
||
```
|
||
apt install mariadb-server-10.6
|
||
```
|
||
## 初始化数据库
|
||
```
|
||
mysql_secure_installation
|
||
```
|
||
## 连接数据库
|
||
```
|
||
mysql -u root -p
|
||
```
|
||
|
||
## 创建数据库
|
||
```
|
||
CREATE DATABASE bbs;
|
||
```
|
||
|
||
## 创建一个本地访问数据库的账户
|
||
### 创建用户
|
||
```
|
||
CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';
|
||
```
|
||
### 参数:
|
||
`username`:数据库访问的账户;`localhost`:数据库的访问方式,外网访问的时候用%替换localhost即可;`password`:数据库访问密码,此处输入密码明文;
|
||
|
||
## 给测试账户分配权限
|
||
|
||
### 分配所有权限
|
||
|
||
给用户分配所有的权限,并且通过localhost访问
|
||
```
|
||
GRANT ALL ON bbs.* to username@'localhost' IDENTIFIED BY 'password';
|
||
FLUSH privileges;
|
||
```
|
||
### 分配部分权限
|
||
|
||
#### 给账户分配部分的权限,并且通过外网访问
|
||
```
|
||
GRANT insert,delete,select,update ON test.* to username@'%' IDENTIFIED BY 'password';
|
||
FLUSH privileges;
|
||
```
|
||
|
||
#### 或则采用下面的代码,除了操作权限授权外,还赋予授权的权限。
|
||
```
|
||
GRANT ALL ON test.* to username@'%' IDENTIFIED BY 'passowrd' WITH GRANT OPTION;
|
||
FLUSH privileges;
|
||
``` |