You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ubuntu 16.04启动MongoDB Shell遇.mongorc.js权限错误求解决

Fixing .mongorc.js Permission Error When Running mongo on Ubuntu 16.04

Got it, let's break down this permission issue. The problem happens because the .mongorc.js file—MongoDB's user-specific startup config file—has incorrect ownership or permissions, so your regular user can't access it. Using sudo mongo works because it runs as the root user, which has full access to the file. Here's how to fix it step by step:

Step 1: Locate the .mongorc.js file

First, check if the file exists in your regular user's home directory:

ls -la ~/.mongorc.js

If it shows up, note the owner and permissions (look at the columns starting with -rw-r--r-- and the username/group name at the end).

If it doesn't exist here, it was probably created in the root user's home directory when you ran sudo mongo earlier. Check that with:

sudo ls -la /root/.mongorc.js

Step 2: Fix ownership and permissions

Case 1: .mongorc.js exists in your home directory

Change the file's owner to your regular user and set safe, proper permissions:

sudo chown $USER:$USER ~/.mongorc.js
chmod 644 ~/.mongorc.js

The 644 permission means you (the owner) can read/write the file, while others can only read it—this is a secure default for config files.

Case 2: .mongorc.js exists only in root's home directory

You have two simple options here:

  • Option A: Move it to your home directory (if you need the config)
    sudo cp /root/.mongorc.js ~/
    sudo chown $USER:$USER ~/.mongorc.js
    chmod 644 ~/.mongorc.js
    
  • Option B: Delete the root version (if you don't need the config)
    sudo rm /root/.mongorc.js
    
    MongoDB will just skip loading the config file when you run mongo as your regular user, which won't cause any functionality issues.

Step 3: Check your home directory permissions (if needed)

In rare cases, your home directory itself might have overly restrictive permissions that block MongoDB from accessing files inside it. Verify your home directory's permissions with:

ls -ld ~

You should see output like drwxr-xr-x (permissions 755). If not, fix it with:

chmod 755 ~

After these steps, running mongo without sudo should launch the shell without any permission errors related to .mongorc.js.

内容的提问来源于stack exchange,提问作者Mouaici_Med

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 07:46:24