教你使用服务器如何搭建数据库

数据库是存储和管理数据的核心组件,无论是网站、应用还是企业系统,都离不开数据库的支持。本文将以 莱卡云服务器 为例,教你如何快速搭建常用数据库服务。


一、准备工作

  1. 服务器环境

    • 推荐操作系统:Ubuntu 20.04 / Debian 11 / CentOS 7+

    • 公网 IP,可通过 SSH 远程登录

  2. 服务器配置建议

    • CPU ≥ 1 核

    • 内存 ≥ 2GB

    • 确保安全组或防火墙允许数据库访问端口(如 MySQL 默认 3306)

  3. 数据库选择

    • 常用数据库:

      • MySQL / MariaDB:关系型数据库,适合网站和企业应用

      • PostgreSQL:高级关系型数据库,支持复杂查询和大数据量

      • Redis:高性能键值数据库,用于缓存和实时应用


二、搭建 MySQL 数据库

1. 安装 MySQL

以 Ubuntu 为例:

sudo apt update sudo apt install mysql-server -y

2. 初始化数据库

sudo mysql_secure_installation

按照提示设置 root 密码、删除匿名用户、禁止远程 root 登录等。

3. 启动与检查 MySQL

sudo systemctl start mysql sudo systemctl enable mysql sudo systemctl status mysql

4. 远程访问设置

编辑 /etc/mysql/mysql.conf.d/mysqld.cnf

bind-address = 0.0.0.0

然后重启 MySQL:

sudo systemctl restart mysql

创建远程用户:

CREATE USER 'dbuser'@'%' IDENTIFIED BY 'password'; GRANT ALL PRIVILEGES ON *.* TO 'dbuser'@'%' WITH GRANT OPTION; FLUSH PRIVILEGES;


三、搭建 PostgreSQL 数据库

1. 安装 PostgreSQL

sudo apt update sudo apt install postgresql postgresql-contrib -y

2. 启动与检查 PostgreSQL

sudo systemctl start postgresql sudo systemctl enable postgresql sudo systemctl status postgresql

3. 配置远程访问

编辑 /etc/postgresql/12/main/postgresql.conf

listen_addresses = '*'

编辑 /etc/postgresql/12/main/pg_hba.conf,增加:

host all all 0.0.0.0/0 md5

重启 PostgreSQL:

sudo systemctl restart postgresql


四、搭建 Redis 数据库

1. 安装 Redis

sudo apt update sudo apt install redis-server -y

2. 启动与检查 Redis

sudo systemctl start redis-server sudo systemctl enable redis-server sudo systemctl status redis-server

3. 配置远程访问

编辑 /etc/redis/redis.conf

bind 0.0.0.0 requirepass yourpassword

重启 Redis:

sudo systemctl restart redis-server


五、总结

通过以上步骤,你可以在 莱卡云服务器 上快速部署 MySQL、PostgreSQL 或 Redis 数据库,并实现远程访问和管理。根据业务需求,可以选择适合的数据库类型,配合服务器资源进行优化配置,保障数据库性能和安全性。

相关推荐
空灵之海2 小时前
Ubuntu Server 22.04.5系统安装教程
linux·运维·ubuntu
gamers3 小时前
rock linux 9 安装mysql 5.7.44
linux·mysql·adb
arbboter3 小时前
【自动化】深入浅出UIAutomationClient:C#桌面自动化实战指南
运维·c#·自动化·inspect·uiautomation·uia·桌面自动化
二进制_博客3 小时前
给CentOS的虚拟机扩容
linux·运维·centos
千钰v3 小时前
Tcpdump: The Basics Tcpdump 基础
linux·运维·网络·tcpdump·tryhackme
kk5793 小时前
【Ubuntu】sudo apt update出现E :仓库***没有Release文件
linux·运维·ubuntu
cellurw3 小时前
Linux下C语言实现HTTP+SQLite3电子元器件查询系统
linux·c语言·http
okra-4 小时前
如何进行WEB安全性测试
数据库
belldeep4 小时前
Win10 上 Debian 12 如何安装 Redis ?
运维·redis·debian