173 lines
4.2 KiB
Markdown
173 lines
4.2 KiB
Markdown
# MariaDB 安装配置与使用
|
||
|
||
MariaDB 是与 MySQL 兼容的开源实现,当 Linux 服务器上需要使用 MySQL 时可以用 MariaDB 替代。
|
||
|
||
## 1.安装
|
||
|
||
MariaDB 的安装命令如下:
|
||
|
||
```bash
|
||
# Manjaro
|
||
sudo pacman -S mariadb mariadb-clients mariadb-libs
|
||
# Ubuntu
|
||
sudo apt install mariadb-server mariadb-client libmariadb-dev libmariadbclient-dev
|
||
```
|
||
|
||
## 2.初始配置
|
||
|
||
安装完 MariaDB 后需要进行初始配置,首先需要执行以下命令:
|
||
|
||
```bash
|
||
# 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
|
||
```
|
||
|
||
之后启动 MariaDB 的系统服务:
|
||
|
||
```bash
|
||
sudo systemctl start mariadb
|
||
```
|
||
|
||
最后初始化数据库:
|
||
|
||
```bash
|
||
mysql_secure_installation
|
||
|
||
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!
|
||
```
|
||
|
||
## 3.基本维护
|
||
|
||
### 3.1.初始配置+常见问题解决
|
||
|
||
```bash
|
||
# 进入 MariaDB 交互模式
|
||
mariadb -u root -p
|
||
# 本地直接登陆
|
||
mariadb -h localhost -u root -p
|
||
|
||
# 更改密码(解决普通用户登陆报错:Access denied for user 'root'@'localhost'),请将'passwd'替换为实际的密码
|
||
UPDATE mysql.user set authentication_string=PASSWORD('passwd') where user='root';
|
||
# 如果没这一行可能也会报一个错误,因此需要运行这一行
|
||
UPDATE mysql.user set plugin="mysql_native_password";
|
||
|
||
# 强制写入,操作完退出前先进行以下该操作
|
||
FLUSH privileges;
|
||
# 退出
|
||
quit;
|
||
# 重启 MariaDB 服务使新的配置生效
|
||
systemctl restart mariadb
|
||
```
|
||
|
||
### 3.2.配置信息查询
|
||
|
||
```bash
|
||
# 查看端口号
|
||
SHOW global variables like 'port';
|
||
```
|
||
|
||
### 3.3.数据库操作
|
||
|
||
```bash
|
||
# 显示所有数据库
|
||
SHOW DATABASES;
|
||
|
||
# 删除数据库
|
||
DROP DATABASE 'database name';
|
||
|
||
# 选择操作的数据库
|
||
USE mysql;
|
||
|
||
# 创建数据库
|
||
CREATE DATABASE 'database name';
|
||
```
|
||
|
||
### 3.4.用户操作
|
||
|
||
```bash
|
||
# 查询用户
|
||
SELECT user from mysql.user;
|
||
|
||
# 删除用户
|
||
drop user ‘username’@‘host’;
|
||
|
||
# 创建用户
|
||
# 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'
|
||
# 示例
|
||
GRANT ALL ON test.* TO username @ 'localhost' IDENTIFIED BY 'password';
|
||
|
||
# 撤销用户权限
|
||
REVOKE privilege ON databasename.tablename FROM 'username'@'host';
|
||
```
|
||
|
||
## 4.异常处理
|
||
|
||
```bash
|
||
# 停止服务
|
||
sudo systemctl stop mariadb
|
||
# 进入安全模式,并设置为后台进程
|
||
sudo mysqld_safe --skip-grant-tables &
|
||
# 登陆mysql
|
||
mariadb -u root
|
||
```
|
||
|
||
```bash
|
||
# 查询用户
|
||
SELECT Host,User,plugin from mysql.user where User='root';
|
||
# 重置加密模式
|
||
UPDATE mysql.user set plugin='mysql_native_password';
|
||
# 重置密码
|
||
UPDATE mysql.user set password=PASSWORD("newpassword") where User='root';
|
||
# 刷新权限信息
|
||
FLUSH privileges;
|
||
exit
|
||
```
|
||
|
||
```bash
|
||
# 杀掉进程
|
||
sudo kill -9 $(pgrep mysql)
|
||
# 重新启动服务
|
||
sudo systemctl start mariadb
|
||
# 或
|
||
service mariadb start
|
||
# 登陆 mysql
|
||
mariadb -u root -p
|
||
```
|
||
|
||
## 5.彻底卸载
|
||
|
||
```bash
|
||
apt purge mysql-* mariadb-server mariadb-client libmariadb-dev libmariadbclient-dev
|
||
```
|