CentOS
#!/bin/sh cat << 'EOF' >> /etc/yum.repos.d/mongodb-org-5.0.repo [mongodb-org-5.0] name=MongoDB Repository baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/5.0/x86_64/ gpgcheck=1 enabled=1 gpgkey=https://www.mongodb.org/static/pgp/server-5.0.asc EOF dnf install -y mongodb-org-server dnf install -y mongodb-org-shell dnf install -y mongodb-org-tools cp /etc/mongod.conf{,.original} cat << 'EOF' >> /etc/security/limits.d/20-nofile.conf mongod soft nofile 65000 mongod hard nofile 65000 EOF systemctl is-enabled mongod systemctl start mongod
MacOS 安装 MongoDB
brew install mongodb
启动
brew services start mongodb
二进制tar包安装
Install MongoDB
wget http://fastdl.mongodb.org/linux/mongodb-linux-x86_64-1.5.5.tgz debian:/srv# tar zxf mongodb-linux-x86_64-1.5.5.tgz debian:/srv# ln -s mongodb-linux-x86_64-1.5.5 mongodb
Create a data directory
By default MongoDB will store data in /data/db, but it won't automatically create that directory. To create it, do:
$ sudo mkdir -p /data/db/ $ sudo chown `id -u` /data/db
You can also tell MongoDB to use a different data directory, with the --dbpath option.
Run and connect to the server
First, start the MongoDB server in one terminal:
$ ./mongodb/bin/mongod
In a separate terminal, start the shell, which will connect to localhost by default:
$ ./mongodb/bin/mongo > db.foo.save( { a : 1 } ) > db.foo.find()
Congratulations, you've just saved and retrieved your first document with MongoDB!
例 10.1. MongoDB Test
debian:/srv/mongodb/bin# ./mongo MongoDB shell version: 1.5.5 connecting to: test [initandlisten] Thu Jul 22 16:42:07 connection accepted from 127.0.0.1:42876 #1 > db.foo.save({a:1}) Thu Jul 22 16:42:23 allocating new datafile /data/db/test.ns, filling with zeroes... Thu Jul 22 16:42:23 done allocating datafile /data/db/tes