2019-07-16 09:57:00 +08:00
|
|
|
|
# MariaDB 安装配置与使用
|
|
|
|
|
|
|
|
|
|
MariaDB 是与 MySQL 兼容的开源实现,当 Linux 服务器上需要使用 MySQL 时可以用 MariaDB 替代。
|
|
|
|
|
|
2021-08-03 14:48:19 +08:00
|
|
|
|
## 1.安装
|
2019-07-16 09:57:00 +08:00
|
|
|
|
|
|
|
|
|
MariaDB 的安装命令如下:
|
|
|
|
|
|
2020-05-20 16:18:39 +08:00
|
|
|
|
```bash
|
2021-08-06 17:07:16 +08:00
|
|
|
|
# Manjaro
|
|
|
|
|
sudo pacman -S mariadb mariadb-clients mariadb-libs
|
|
|
|
|
# Ubuntu
|
|
|
|
|
sudo apt install mariadb-server mariadb-client libmariadb-dev libmariadbclient-dev
|
2019-07-16 09:57:00 +08:00
|
|
|
|
```
|
|
|
|
|
|
2021-08-03 14:48:19 +08:00
|
|
|
|
## 2.初始配置
|
2019-07-16 09:57:00 +08:00
|
|
|
|
|
|
|
|
|
安装完 MariaDB 后需要进行初始配置,首先需要执行以下命令:
|
|
|
|
|
|
2020-05-20 16:18:39 +08:00
|
|
|
|
```bash
|
2021-08-06 17:07:16 +08:00
|
|
|
|
# Manjaro
|
|
|
|
|
sudo mariadb-install-db --user=mysql --basedir=/usr --datadir=/var/lib/mysql
|
|
|
|
|
# Ubuntu ?
|
|
|
|
|
sudo mysql_install_db --user=mysql --basedir=/usr --datadir=/var/lib/mysql
|
2019-07-16 09:57:00 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
之后启动 MariaDB 的系统服务:
|
|
|
|
|
|
2020-05-20 16:18:39 +08:00
|
|
|
|
```bash
|
2021-08-06 17:07:16 +08:00
|
|
|
|
sudo systemctl start mariadb
|
2019-07-16 09:57:00 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
最后初始化数据库:
|
|
|
|
|
|
2020-05-20 16:18:39 +08:00
|
|
|
|
```bash
|
2021-09-09 21:51:31 +08:00
|
|
|
|
sudo mysql_secure_installation
|
2019-07-16 09:57:00 +08:00
|
|
|
|
|
2021-08-03 14:48:19 +08:00
|
|
|
|
Enter current password for root (enter for none):[password]
|
|
|
|
|
Switch to unix_socket authentication [Y/n] n
|
|
|
|
|
Change the root password? [Y/n] Y
|
|
|
|
|
Remove anonymous users? [Y/n] Y
|
|
|
|
|
Disallow root login remotely? [Y/n] n
|
|
|
|
|
Remove test database and access to it? [Y/n] Y
|
|
|
|
|
Reload privilege tables now? [Y/n] Y
|
|
|
|
|
... Success!
|
2019-07-16 09:57:00 +08:00
|
|
|
|
```
|
|
|
|
|
|
2021-08-03 14:48:19 +08:00
|
|
|
|
## 3.基本维护
|
|
|
|
|
|
|
|
|
|
### 3.1.初始配置+常见问题解决
|
2019-07-16 09:57:00 +08:00
|
|
|
|
|
2020-05-20 16:18:39 +08:00
|
|
|
|
```bash
|
2019-07-16 09:57:00 +08:00
|
|
|
|
# 进入 MariaDB 交互模式
|
|
|
|
|
mariadb -u root -p
|
2021-08-03 14:48:19 +08:00
|
|
|
|
# 本地直接登陆
|
|
|
|
|
mariadb -h localhost -u root -p
|
|
|
|
|
|
|
|
|
|
# 更改密码(解决普通用户登陆报错:Access denied for user 'root'@'localhost'),请将'passwd'替换为实际的密码
|
2021-08-06 17:07:16 +08:00
|
|
|
|
UPDATE mysql.user set authentication_string=PASSWORD('passwd') where user='root';
|
2019-07-16 09:57:00 +08:00
|
|
|
|
# 如果没这一行可能也会报一个错误,因此需要运行这一行
|
2021-08-06 17:07:16 +08:00
|
|
|
|
UPDATE mysql.user set plugin="mysql_native_password";
|
2021-08-03 14:48:19 +08:00
|
|
|
|
|
|
|
|
|
# 强制写入,操作完退出前先进行以下该操作
|
|
|
|
|
FLUSH privileges;
|
2019-07-16 09:57:00 +08:00
|
|
|
|
# 退出
|
2022-03-11 12:27:04 +08:00
|
|
|
|
QUIT;
|
2019-07-16 09:57:00 +08:00
|
|
|
|
# 重启 MariaDB 服务使新的配置生效
|
|
|
|
|
systemctl restart mariadb
|
|
|
|
|
```
|
2021-08-03 14:48:19 +08:00
|
|
|
|
|
|
|
|
|
### 3.2.配置信息查询
|
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
# 查看端口号
|
|
|
|
|
SHOW global variables like 'port';
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### 3.3.数据库操作
|
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
# 显示所有数据库
|
2021-08-06 17:07:16 +08:00
|
|
|
|
SHOW DATABASES;
|
2021-08-03 14:48:19 +08:00
|
|
|
|
|
|
|
|
|
# 删除数据库
|
2021-08-06 17:07:16 +08:00
|
|
|
|
DROP DATABASE 'database name';
|
2021-08-03 14:48:19 +08:00
|
|
|
|
|
|
|
|
|
# 选择操作的数据库
|
2021-08-06 17:07:16 +08:00
|
|
|
|
USE mysql;
|
2021-08-03 14:48:19 +08:00
|
|
|
|
|
|
|
|
|
# 创建数据库
|
|
|
|
|
CREATE DATABASE 'database name';
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### 3.4.用户操作
|
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
# 查询用户
|
|
|
|
|
SELECT user from mysql.user;
|
|
|
|
|
|
|
|
|
|
# 删除用户
|
2022-03-11 12:27:04 +08:00
|
|
|
|
DROP user ‘username’@‘host’;
|
2021-08-03 14:48:19 +08:00
|
|
|
|
|
|
|
|
|
# 创建用户
|
|
|
|
|
# username - 你将创建的用户名
|
|
|
|
|
# host - 指定该用户在哪个主机上可以登陆,如果是本地用户可用 localhost, 如果想让该用户可以从任意远程主机登陆,可以使用通配符 %
|
|
|
|
|
# password - 该用户的登陆密码,密码可以为空,如果为空则该用户可以不需要密码登陆服务器
|
|
|
|
|
CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';
|
|
|
|
|
# 示例
|
|
|
|
|
CREATE USER 'm23100'@'localhost' IDENTIFIED BY '123456';
|
|
|
|
|
CREATE USER 'm23100'@'192.168.1.101' IDENDIFIED BY '123456';
|
|
|
|
|
CREATE USER 'm23100'@'%' IDENTIFIED BY '123456';
|
|
|
|
|
CREATE USER 'm23100'@'%' IDENTIFIED BY '';
|
|
|
|
|
CREATE USER 'm23100'@'%';
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### 3.5.用户权限管理
|
|
|
|
|
|
|
|
|
|
主要是管理用户对数据库的操作权限。
|
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
# 授权用户使用数据库
|
|
|
|
|
# privileges - 用户的操作权限,如SELECT , INSERT , UPDATE 等.如果要授予所的权限则使用ALL
|
|
|
|
|
# databasename - 数据库名
|
|
|
|
|
# tablename-表名
|
|
|
|
|
# 如果要授予该用户对所有数据库和表的相应操作权限则可用 * 表示, 如 *.*
|
|
|
|
|
GRANT privileges ON databasename.tablename TO 'username'@'host'
|
|
|
|
|
# 示例
|
2023-05-06 08:18:28 +08:00
|
|
|
|
GRANT ALL ON test.* TO 'username'@'localhost' IDENTIFIED BY 'password';
|
2021-08-03 14:48:19 +08:00
|
|
|
|
|
|
|
|
|
# 撤销用户权限
|
|
|
|
|
REVOKE privilege ON databasename.tablename FROM 'username'@'host';
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## 4.异常处理
|
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
# 停止服务
|
|
|
|
|
sudo systemctl stop mariadb
|
|
|
|
|
# 进入安全模式,并设置为后台进程
|
|
|
|
|
sudo mysqld_safe --skip-grant-tables &
|
|
|
|
|
# 登陆mysql
|
2021-08-06 17:07:16 +08:00
|
|
|
|
mariadb -u root
|
2021-08-03 14:48:19 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
# 查询用户
|
|
|
|
|
SELECT Host,User,plugin from mysql.user where User='root';
|
|
|
|
|
# 重置加密模式
|
2021-08-06 17:07:16 +08:00
|
|
|
|
UPDATE mysql.user set plugin='mysql_native_password';
|
2021-08-03 14:48:19 +08:00
|
|
|
|
# 重置密码
|
2021-08-06 17:07:16 +08:00
|
|
|
|
UPDATE mysql.user set password=PASSWORD("newpassword") where User='root';
|
2021-08-03 14:48:19 +08:00
|
|
|
|
# 刷新权限信息
|
2021-08-06 17:07:16 +08:00
|
|
|
|
FLUSH privileges;
|
2023-05-06 08:18:28 +08:00
|
|
|
|
exit;
|
2021-08-03 14:48:19 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
# 杀掉进程
|
2021-08-06 17:07:16 +08:00
|
|
|
|
sudo kill -9 $(pgrep mysql)
|
2021-08-03 14:48:19 +08:00
|
|
|
|
# 重新启动服务
|
2021-08-06 17:07:16 +08:00
|
|
|
|
sudo systemctl start mariadb
|
|
|
|
|
# 或
|
2021-08-03 14:48:19 +08:00
|
|
|
|
service mariadb start
|
|
|
|
|
# 登陆 mysql
|
|
|
|
|
mariadb -u root -p
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## 5.彻底卸载
|
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
apt purge mysql-* mariadb-server mariadb-client libmariadb-dev libmariadbclient-dev
|
|
|
|
|
```
|