Debian安装MariaDB

1. 安装 MariaDB

sudo apt install -y mariadb-server mariadb-client

初始化安全配置:

sudo mysql_secure_installation

开始安装配置:

// 输入root的当前密码(如无则输入) 
Enter current password for root (enter for none): 
// Unix Socket 身份验证  建议Y切换
Switch to unix_socket authentication [Y/n] y

// 更新root密码   如果root密码为空,建议Y设置新的root密码
Change the root password? [Y/n] y
// 输入root的新密码
New password: 
// 再次输入root的新密码
Re-enter new password: 

// 移除匿名用户  建议删除Y
Remove anonymous users? [Y/n] y

// 是否禁止root远程登陆  如有需要可以开启Y
Disallow root login remotely? [Y/n] n

// 移除测试数据库并禁止访问   测试数据库如无需要,可以删除
Remove test database and access to it? [Y/n] y

// 重新加载权限表  如果有变更,建议Y
Reload privilege tables now? [Y/n] y

// 出现这个提示语表示MariaDB的安全配置已完成。
All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

进入数据库并创建用户和库:

sudo mysql -u root -p

在 MariaDB 里执行:

-- 创建数据库,并设置编码为utf8mb4,设置排序规则utf8mb4_unicode_ci 
CREATE DATABASE testdb DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
-- 创建一个数据库用户devuser,只允许从localhost登陆,设置密码为devpass111
CREATE USER 'devuser'@'localhost' IDENTIFIED BY 'devpass111';
-- 授权所有权限(增删改查、建表、索引等) 作用在testdb数据库下的所有表,并把这些授权授予给devuser
GRANT ALL PRIVILEGES ON testdb.* TO 'devuser'@'localhost';
-- 刷新权限
FLUSH PRIVILEGES;
-- 退出
EXIT;

2. 修改 MariaDB 配置文件,允许监听远程请求

编辑配置文件:

sudo nano /etc/mysql/mariadb.conf.d/50-server.cnf

找到:

bind-address = 127.0.0.1

改成:

bind-address = 0.0.0.0

保存退出,然后重启 MariaDB:

sudo systemctl restart mariadb

3. 开放防火墙端口(默认 3306)

如果你启用了 ufw

sudo ufw allow 3306

评论

等风等雨等你来