使用 shell 脚本安装 MongoDB

使用 shell 脚本安装 MongoDB

我正在尝试使用 shell 脚本自动执行在 Ubuntu 20.04 上设置 MongoDB 的任务,如下所示:

mongodb_安装.sh*

MONGO_VERSION=6.0

sudo apt-get update
sudo apt-get install gnupg

# import MongoDB GPG public key
wget -qO - https://www.mongodb.org/static/pgp/server-${MONGO_VERSION}.asc | sudo apt-key add -
sudo cd /etc/apt/sources.list.d/
sudo touch mongodb-org-${MONGO_VERSION}.list
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/${MONGO_VERSION} multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-${MONGO_VERSION}.list
sudo apt-get update
sudo apt-get install -y mongodb-org

sudo chown -R mongodb:mongodb /var/lib/mongodb
sudo chown mongodb:mongodb /tmp/mongodb-27017.sock
sudo service mongod start
sudo systemctl status mongod

运行脚本后,我收到状态“活动”:

● mongod.service - MongoDB Database Server
     Loaded: loaded (/lib/systemd/system/mongod.service; disabled; vendor preset: enabled)
     Active: active (running) since Thu 2022-11-03 14:04:45 +07; 7ms ago
       Docs: https://docs.mongodb.org/manual
   Main PID: 74147 (mongod)
     Memory: 796.0K
     CGroup: /system.slice/mongod.service
             └─74147 /usr/bin/mongod --config /etc/mongod.conf

然而,当我在终端重新检查时,mongod 服务并没有启动:

$ systemctl status mongod
mongod.service - MongoDB Database Server
     Loaded: loaded (/lib/systemd/system/mongod.service; disabled; vendor preset: enabled)
     Active: failed (Result: exit-code) since Thu 2022-11-03 14:04:45 +07; 7s ago
       Docs: https://docs.mongodb.org/manual
    Process: 74147 ExecStart=/usr/bin/mongod --config /etc/mongod.conf (code=exited, status=14)
   Main PID: 74147 (code=exited, status=14)

我尝试过、等的不同组合sudo ./mongodb_install.shsudo . ./mongodb_install.sh但目前没有任何效果。

你能告诉我解决这个问题的方法吗?

答案1

使用apt-key addis强烈不推荐,在 Ubuntu 22.04 及更高版本上甚至不可能。相反,请安装密钥/etc/apt/keyrings并在文件中引用此密钥.list

因此你的脚本看起来应该是这样的:

MONGO_VERSION=6.0
sudo apt-get update
sudo apt-get install gnupg -y
# Changes here!
  sudo mkdir -p /etc/apt/keyrings
  curl -fsSL https://www.mongodb.org/static/pgp/server-${MONGO_VERSION}.asc | sudo gpg --dearmor -o /etc/apt/keyrings/mongodb-${MONGO_VERSION}.gpg
cd /etc/apt/sources.list.d/
sudo touch mongodb-org-${MONGO_VERSION}.list
# And here!
  echo "deb [arch=amd64,arm64 signed-by=/etc/apt/keyrings/mongodb-${MONGO_VERSION}.gpg] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/${MONGO_VERSION} multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-${MONGO_VERSION}.list
sudo apt-get update
sudo apt-get install -y mongodb-org
sudo systemctl start mongod

是的,使用 MongoDB 的人应该会告诉他们他们的指南已经过时了在这方面。

答案2

为了使其正常工作,我做了两处更改:

  1. 添加-y 标志安装 gnupg 时:sudo apt-get install gnupg -y
  2. 删除以下chown部分:
sudo chown -R mongodb:mongodb /var/lib/mongodb
sudo chown mongodb:mongodb /tmp/mongodb-27017.sock
sudo service mongod start
sudo systemctl status mongod

生成的在 Ubuntu 20.04 上安装 MongoDB 6.0 的 shell 脚本如下:

MONGO_VERSION=6.0
sudo apt-get update
sudo apt-get install gnupg -y
wget -qO - https://www.mongodb.org/static/pgp/server-${MONGO_VERSION}.asc | sudo apt-key add -
cd /etc/apt/sources.list.d/
sudo touch mongodb-org-${MONGO_VERSION}.list
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/${MONGO_VERSION} multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-${MONGO_VERSION}.list
sudo apt-get update
sudo apt-get install -y mongodb-org
sudo systemctl start mongod

相关内容