mongodb 如何确保 Mongo 二进制文件在我的 PATH 中 - 在 Mac 上的 shell 'rc (~/.bashrc) 中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28267773/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 20:16:33  来源:igfitidea点击:

How do I ensure that Mongo binaries are in my PATH - in my shell' rc (~/.bashrc) on a Mac

bashmongodbmacospathbinaries

提问by shareyourpeace

I am learning mongoDb.
I did the following: I understand this.

我正在学习 mongoDb。
我做了以下事情:我明白这一点。

mkdir -p mongodb
cp -R -n mongodb-osx-x86_64-2.6.7/ mongodb

These next ** instructions are not crystal clear to me.

这些接下来的 ** 说明对我来说不是很清楚。

** Ensure the location of the binaries is in the PATH variable. The MongoDB binaries are in the bin/ directory of the archive. To ensure that the binaries are in your PATH, you can modify your PATH.

** 确保二进制文件的位置在 PATH 变量中。MongoDB 二进制文件位于存档的 bin/ 目录中。为确保二进制文件在您的 PATH 中,您可以修改您的 PATH。

For example, you can add the following line to your shell's rc file (e.g. ~/.bashrc):

例如,您可以将以下行添加到您的 shell 的 rc 文件(例如 ~/.bashrc):

export PATH=<mongodb-install-directory>/bin:$PATH

Replace with the path to the extracted MongoDB archive.

替换为提取的 MongoDB 存档的路径。

End of the instructions.

说明结束。

From terminal on my Mac

从我的 Mac 上的终端

$which bash
/bin/bash

The bin directory has a bunch of commands: bash, chmod, ln, pid, mkdir.

bin 目录有一堆命令:bash、chmod、ln、pid、mkdir。

I cannot find the directory where I should include the PATH code above.

我找不到我应该在上面包含 PATH 代码的目录。

Thanks

谢谢

Abhay's instructions worked ! Sorry that the following is long but I believe it is necessary because you were so clear last time and I want you to see my file structure.
If email is easier let me know but this will more likely than not help others :) I really want to understand my file structure so if you do not mind a bit of further clarification. Thanks again for your expertise.

Abhay 的指示奏效了!抱歉,以下内容很长,但我认为这是必要的,因为您上次非常清楚,我希望您能看到我的文件结构。
如果电子邮件更容易让我知道,但这更有可能帮助其他人:) 我真的很想了解我的文件结构,所以如果你不介意进一步澄清一下。再次感谢您的专业知识。

1. But why are all of the binaries (the executables) listed individually in this bin folder ? Are they the 'only' files that should 'live' in this folder ? Perhaps when I was installing bower grunt etc. I should installed those (bower grunt etc) into a different location. I am trying to keep my file structures logical and clean. Here is what I have.

1. 但是为什么所有的二进制文件(可执行文件)都单独列在这个 bin 文件夹中?它们是应该“存在”在此文件夹中的“唯一”文件吗?也许当我安装 bower grunt 等时,我应该将它们(bower grunt 等)安装到不同的位置。我试图保持我的文件结构合乎逻辑和干净。这是我所拥有的。

MacBookPro:usr regina$ cd ..
MacBookPro:/ regina$ ls
Applications        bin         net
CoreSyncInstall.log cores           private
Library         data            sbin
Network         dev         tmp
System          etc         usr
Users           home            var
Volumes         mach_kernel
MacBookPro:/ regina$ cd usr/local/bin
MacBookPro:bin regina$ ls
bower       grunt       mongod      mongorestore    npm
browserify  gulp        mongodump   mongos      statsd
bsondump    http-server mongoexport mongosniff  yo
cake        karma       mongofiles  mongostat
coffee      lessc       mongoimport mongotop
cordova     mkdirp      mongooplog  node
express     mongo       mongoperf   nodemon
MacBookPro:bin regina$ 

2. Also, I wanted to look at my .bashrc file. I do not have one. I am on osx 10.8.5 I want to 'view' my $PATH variable. I believe I found it somewhere months ago. I was using nano to open a bunch of files because I was curious. Was it listed as an environmental variable ?

2. 另外,我想看看我的 .bashrc 文件。我一个都没有。我在 osx 10.8.5 上我想“查看”我的 $PATH 变量。我相信我几个月前在某个地方找到了它。我正在使用 nano 打开一堆文件,因为我很好奇。它是否被列为环境变量?

MacBookPro:/ regina$ ls
Applications        bin         net
CoreSyncInstall.log cores           private
Library         data            sbin
Network         dev         tmp
System          etc         usr
Users           home            var
Volumes         mach_kernel
MacBookPro:/ regina$ cd home
MacBookPro:home regina$ ls -la
total 2
dr-xr-xr-x   2 root  wheel     1 Feb  3 08:29 .
drwxr-xr-x  33 root  wheel  1190 Jan 25 17:21 ..
MacBookPro:home regina$ 
  1. I start the mongo shell like this. I am not showing all of the lines output in terminal.
    Look at my file structure. I originally followed a cp command but it did not explain 'where' to copy the archived (desktop) mongodb-osx-x84.... folder so look where I copied it to. Now I have these files in 2 locations - yes ? And I have a mongodb folder inside of a higher level mongodb folder. Why ? How did I manage this ? It seems illogical. I have been indented 4 spaces but the below will not format so I just added line breaks. sorry.

    MacBookPro:home regina$ cd ..

    MacBookPro:/ regina$ ls Applications bin net CoreSyncInstall.log cores private Library data sbin Network dev tmp System etc usr Users home var Volumes mach_kernel

    MacBookPro:/ regina$ cd data

    MacBookPro:data regina$ ls

    db mongodb

    MacBookPro:data regina$ cd mongodb

    MacBookPro:mongodb regina$ ls

    mongodb MacBookPro:mongodb regina$ cd mongodb MacBookPro:mongodb regina$ ls GNU-AGPL-3.0 THIRD-PARTY-NOTICES README bin

    MacBookPro:mongodb regina$ cd bin

    MacBookPro:bin regina$ ls

    bsondump mongodump mongoimport mongorestore mongostat mongo mongoexport mongooplog mongos mongotop mongod mongofiles mongoperf mongosniff

  2. This is what happens when I try and type 'mongo' as another forum suggested.

    MacBookPro:bin regina$ mongo MongoDB shell version: 2.6.7 connecting to: test 2015-02-03T08:36:54.157-0500 warning: Failed to connect to 127.0.0.1:27017, reason: errno:61 Connection refused

  1. 我像这样启动 mongo shell。我没有在终端中显示所有的行输出。
    看看我的文件结构。我最初遵循 cp 命令,但它没有解释复制存档(桌面)mongodb-osx-x84 .... 文件夹的“位置”,所以看看我将它复制到哪里。现在我在 2 个位置有这些文件 - 是吗?我在更高级别的 mongodb 文件夹中有一个 mongodb 文件夹。为什么 ?我是怎么做到的?似乎不合逻辑。我已经缩进了 4 个空格,但下面不会格式化,所以我只是添加了换行符。对不起。

    MacBookPro:home regina$ cd ..

    MacBookPro:/regina$ ls Applications bin net CoreSyncInstall.log cores private Library data sbin Network dev tmp System etc usr Users home var Volumes mach_kernel

    MacBookPro:/regina$ cd 数据

    MacBookPro:data regina$ls

    数据库mongodb

    MacBookPro:data regina$ cd mongodb

    MacBookPro:mongodb regina$ls

    mongodb MacBookPro:mongodb regina$ cd mongodb MacBookPro:mongodb regina$ ls GNU-AGPL-3.0 第三方通知自述文件

    MacBookPro:mongodb regina$ cd bin

    MacBookPro:bin regina$ ls

    bsondump mongodump mongoimport mongorestore mongostat mongo mongoexport mongooplog mongos mongotop mongod mongofiles mongoperf mongosniff

  2. 当我按照另一个论坛的建议尝试输入“mongo”时,就会发生这种情况。

    MacBookPro:bin regina$ mongo MongoDB shell 版本:2.6.7 连接到:测试 2015-02-03T08:36:54.157-0500 警告:无法连接到 127.0.0.1:27017,原因:errno:61 连接被拒绝

So I typed this which still failed.

所以我输入了这个仍然失败。

MacBookPro:bin regina$ ./mongo
MongoDB shell version: 2.6.7
connecting to: test
2015-02-03T08:37:03.599-0500 warning: Failed to connect to    127.0.0.1:27017, reason: errno:61 Connection refused

Then I tried this which connected - opened port 27017 - and is listening. Why did I need the daemon command ?

然后我尝试了这个连接 - 打开了端口 27017 - 并且正在监听。为什么我需要守护进程命令?

MacBookPro:bin regina$ ./mongod
./mongod --help for help and startup options
2015-02-03T08:37:08.712-0500 [initandlisten] MongoDB starting :    pid=487 port=27017 dbpath=/data/db 64-bit host=macbookpro.home
more lines of code
2015-02-03T08:37:09.018-0500 [initandlisten] waiting for connections on port 27017

回答by Abhay PS

When you download MongoDB for Mac, you get an archive file. First thing you need to is uncompress the archive. To do that you just double click on the archive. The uncompressed folder will have the binaries in the binfolder, like mongodb-osx-x86_64-2.6.7/bin. What you need to do next is to make sure these binaries are accessible. So, for this you can do either of the two things -

当您下载 Mac 版 MongoDB 时,您将获得一个存档文件。您需要做的第一件事是解压缩存档。为此,您只需双击存档即可。未压缩的文件夹将在文件夹中包含二进制bin文件,如mongodb-osx-x86_64-2.6.7/bin. 您接下来需要做的是确保这些二进制文件是可访问的。所以,为此你可以做两件事中的任何一件 -

  1. Copy the binaries to /usr/local/binfolder. As /usr/local/binis always in PATH, so you don't have to add it to the PATH.

    This is pretty simple. Run cp mongodb-osx-x86_64-2.6.7/bin/* /usr/local/bin/. That's it. You are all done.

  2. Add the binary folder's path to your $PATHvariable.

    Open the .bashrcfile, which is located in your home folder, and put export PATH=$HOME/mongodb/bin:$PATHline in the end. I am assuming that mongodb uncompressed in downloads.

  1. 将二进制/usr/local/bin文件复制到文件夹。与/usr/local/binPATH 中一样,因此您不必将其添加到 PATH 中。

    这很简单。运行cp mongodb-osx-x86_64-2.6.7/bin/* /usr/local/bin/。就是这样。你都完成了。

  2. 将二进制文件夹的路径添加到您的$PATH变量中。

    打开.bashrc位于您的主文件夹中的文件,并将export PATH=$HOME/mongodb/bin:$PATHline 放在最后。我假设 mongodb 在下载中未压缩。

If going by the second method, make sure you don't accidently delete the mongodb folder that is added in the PATH.

如果采用第二种方法,请确保您没有意外删除添加到 PATH 中的 mongodb 文件夹。

Once you have finished doing this, close the terminal, open a new terminal window and run:

完成此操作后,关闭终端,打开一个新的终端窗口并运行:

mongo --version

If you get the mongodb info with version, then good.

如果您获得带有版本的 mongodb 信息,那就太好了。

If you still get something like command mongo not found, then make sure you followed above instructions right. Also make sure you are not using any other shell, like zsh. If you are, then you have to add the exportstatement in the respective shell file. For zsh the file name is .zshrc.

如果你仍然得到类似的东西command mongo not found,那么请确保你正确地遵循了上面的说明。还要确保您没有使用任何其他 shell,例如 zsh。如果是,则必须export在相应的 shell 文件中添加该语句。对于 zsh,文件名是.zshrc.



Okay! So as you have updated the initial question with some more questions. So here goes the explanation point by point:

好的!因此,您已经用更多问题更新了最初的问题。所以这里逐点解释:

  1. The files you see in /usr/local/binfolder are binaries. In simple language, they are executable programs. These executable binaries live there though you can run them from anywhere in the command line.

  2. The .bashrcfile is found in user's home directory. On Mac it's /Users/username. In your case, it should be /Users/reginabecause your name is Regina.

  3. mongois the client application. It is used to interact with the mongodb database. And, mongodis the database deamon that stores the data. So first you have to run mongod, and then only you can connect to it using the client mongoand start querying.

  1. 您在/usr/local/bin文件夹中看到的文件是二进制文件。用简单的语言来说,它们是可执行程序。这些可执行二进制文件存在于那里,但您可以从命令行的任何位置运行它们。

  2. .bashrc文件位于用户的主目录中。在 Mac 上是/Users/username. 就您而言,应该是/Users/regina因为您的名字是 Regina。

  3. mongo是客户端应用程序。它用于与 mongodb 数据库进行交互。并且,mongod是存储数据的数据库守护进程。因此,首先您必须运行mongod,然后只有您才能使用客户端连接到它mongo并开始查询。