奋斗
努力

腾讯云轻量应用服务器如何安装数据库?

云计算

在腾讯云轻量应用服务器上安装数据库的步骤如下,根据不同的数据库类型(如 MySQL、PostgreSQL、MongoDB 等)选择对应方法:


一、安装 MySQL

1. 更新系统并安装 MySQL

# 更新软件包列表
sudo apt update -y
sudo apt upgrade -y

# 安装 MySQL Server(Ubuntu/Debian)
sudo apt install mysql-server -y

# 安装 MySQL Server(CentOS)
sudo yum install mysql-server -y
sudo systemctl start mysqld
sudo systemctl enable mysqld

2. 安全配置(可选)

sudo mysql_secure_installation

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

3. 配置远程访问(如需)

# 登录 MySQL
sudo mysql -u root -p

# 创建用户并授权远程访问(替换 'your_password')
CREATE USER 'username'@'%' IDENTIFIED BY 'your_password';
GRANT ALL PRIVILEGES ON *.* TO 'username'@'%' WITH GRANT OPTION;
FLUSH PRIVILEGES;
EXIT;

# 修改 MySQL 配置文件允许远程连接
sudo sed -i 's/127.0.0.1/0.0.0.0/g' /etc/mysql/mysql.conf.d/mysqld.cnf
sudo systemctl restart mysql

二、安装 PostgreSQL

1. 安装 PostgreSQL

# Ubuntu/Debian
sudo apt install postgresql postgresql-contrib -y

# CentOS
sudo yum install postgresql-server postgresql-contrib -y
sudo postgresql-setup initdb
sudo systemctl start postgresql
sudo systemctl enable postgresql

2. 配置远程访问

# 修改配置文件
sudo nano /etc/postgresql/*/main/postgresql.conf
# 将 `listen_addresses` 改为 '*'
sudo nano /etc/postgresql/*/main/pg_hba.conf
# 添加行:`host all all 0.0.0.0/0 md5`

# 重启服务
sudo systemctl restart postgresql

# 登录 PostgreSQL 创建用户
sudo -u postgres psql
CREATE USER username WITH PASSWORD 'your_password';
ALTER ROLE username SET client_encoding TO 'utf8';
GRANT ALL PRIVILEGES ON DATABASE your_db TO username;
q

三、安装 MongoDB

1. 安装 MongoDB

# Ubuntu/Debian(官方源)
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 9DA31620334BD75D9DCB49F368818C72E52529D4
echo "deb [ arch=amd64 ] https://repo.mongodb.org/apt/ubuntu $(lsb_release -cs)/mongodb-org/4.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list
sudo apt update
sudo apt install mongodb-org -y
sudo systemctl start mongod
sudo systemctl enable mongod

# CentOS
sudo nano /etc/yum.repos.d/mongodb-org.repo
# 添加官方源后安装
sudo yum install mongodb-org -y
sudo systemctl start mongod

2. 配置远程访问

# 修改配置文件
sudo nano /etc/mongod.conf
# 将 `bindIp: 127.0.0.1` 改为 `0.0.0.0`
sudo systemctl restart mongod

# 进入 MongoDB Shell 创建用户
mongo
use admin
db.createUser({user: "admin", pwd: "password", roles: ["root"]})

四、其他注意事项

  1. 防火墙设置

    • 在腾讯云控制台开放对应端口(如 MySQL 3306、PostgreSQL 5432、MongoDB 27017)。
    • 服务器本地防火墙(如 ufwfirewalld)也需放行端口。
  2. 安全建议

    • 使用强密码并限制访问 IP。
    • 考虑通过内网连接(如服务器与数据库在同一 VPC)。
  3. 使用腾讯云数据库(可选)
    如果希望免运维,可直接购买腾讯云的 TencentDB for MySQL 等托管服务。


五、验证安装

  • MySQL: mysql -u username -p
  • PostgreSQL: psql -U username -d your_db
  • MongoDB: mongo --username admin --password password

按需选择数据库类型并完成安装后,即可部署应用。

未经允许不得转载:云服务器 » 腾讯云轻量应用服务器如何安装数据库?