参考:http://www.itnpc.com/news/web/146889317992721.html
1、将安装文件放置单/home目录下
2、解压文件到当前目录
sudo tar -zxvf /home/lj/mongodb-linux-x86_64-ubuntu1404-3.2.4.tgz
3、将文件移动到/usr/local/目录下,次目录一般用来存用户自定义安装
sudo mv /home/lj/mongodb-linux-x86_64-ubuntu1404-3.2.4 /usr/local/mongodb/
4、新建数据库目录和日志目录
root@servename:/home# mkdir -p /usr/local/mongodb/data
root@servename:/home# cd /usr/local/mongodb
root@servename:/usr/local/mongodb# touch logs
5、使用语句vim /usr/local/mongodb/mongod.conf创建mongodb数据库配置文件,然后输入如下
dbpath=/usr/local/mongodb/data
logpath=/usr/local/mongodb/logs
logappend = true
#fork and run in background
fork = true
auth=true
#location of pidfile
pidfilepath=/usr/local/mongodb/mongod.pid
port=27017
#bind_ip = 121.22.33.19
6、启动mongodb
进入到mongodb安装目录的bin
root@servename:~# cd /usr/local/mongodb/bin
root@servename:~# ./mongod --dbpath=/usr/local/mongodb/data --fork --logpath=/usr/local/mongodb/logs
如上,表示成功
7、配置不用进入到/usr/local/mongodb/bin路径下使用mongo命令
root@servename:~# vim .bash_profile
PATH=$PATH:$HOME/bin:/usr/local/mongodb/bin
root@servename:~# source .bash_profile
这样就可以不用进入到/usr/local/mongodb/bin路径下使用mongo命令了,直接输入mongo命令即可。
8、怎样使用以service方式启动数据库
前面启动数据库的方式略显繁琐,所以想用service mongodb start方式启动数据库。
默认service mongodb status会报错(由于该服务没有加入到service中):
root@servename:~# service mongodb status
mongodb: unrecognized service
需要进行如下配置:
root@servename:~# vim /etc/init.d/mongod添加:
#!/bin/sh
. /lib/lsb/init-functions
CONFIG="/usr/local/mongodb/mongod.conf"
PROGRAM="/usr/local/mongodb/bin/mongod"
MONGOPID=`ps -ef | grep 'mongod' | grep -v grep | awk '{print $2}'`
test -x $PROGRAM || exit 0
case "$1" in
start)
ulimit -n 3000
echo "Starting MongoDB Server"
$PROGRAM -f $CONFIG &
;;
stop)
ulimit -n 3000
echo "Stopping MongoDB Server"
if [ ! -z "$MONGOPID" ]; then
kill -15 $MONGOPID
fi
;;
*)
log_success_msg "Usage:/etc/init.d/mongod {start|stop}"
exit 1
esac
exit 0
root@servename ~]# ls -l /etc/init.d/mongod
-rw-r--r--. 1 root root 529 Jun 25 17:33 /etc/init.d/mongod
root@servename:~# chmod 744 /etc/init.d/mongod
root@servename:~# ls -l /etc/init.d/mongod-rwxr--r--. 1 root root 529 Jun 25 17:33 /etc/init.d/mongod
测试效果:
root@servename:~# service mongod stop
Stopping MongoDB Server
Terminated
root@servename:~# service mongod start
Starting MongoDB Server
root@ser6-51 mongodb]# about to fork child process, waiting until server is ready for connections.
forked process: 3632
child process started successfully, parent exiting
9、用户名、密码 连接mongodb数据库
进入数据库mongo
root@servename:~# mongo
进入用户数据库
>use admin
创建用户
>db.createUser({user:"username",pwd:"pwd",roles:["root"]});
登陆数据库
>db.auth("username","pwd");
测试
>show dbs
10、设置开机自动启动
方法1:
root@servename:~# vim /etc/rc.local
添加:
/usr/local/mongodb/bin/mongod --dbpath=/usr/local/mongodb/data --fork --logpath=/usr/local/mongodb/logs
方法2:
设置开机启动或关闭
sudo sysv-rc-conf mongod on(off)
二、通过apt-get语句安装mongodb
参考:https://docs.mongodb.org/manual/tutorial/install-mongodb-on-ubuntu/
1、Import the public key used by the package management system.
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv EA312927
2、Create a list file for MongoDB.
echo "deb http://repo.mongodb.org/apt/ubuntu trusty/mongodb-org/3.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.2.list
3、Reload local package database.
sudo apt-get update
4、Install the MongoDB packages.
sudo apt-get install -y mongodb-org
5、在ubuntu中创建数据库的路径
sudo mkdir /data/db
6、修改mongodb的数据库路径
在ubuntu中输入sudo gedit /etc/mongod.conf 修改配置文件的dbPath这里以/data/db为列
7、重新配置mongodb的路径(如果没有这个语句,可能启动不了)
sudo chown -R mongodb:mongodb /data/db
8、mongodb启动、停止、重启操作
sudo service mongod start
sudo service mongod stop
sudo service mongod restart
9、卸载mongodb
sudo apt-get purge mongodb-org*
sudo rm -r /var/log/mongodb
sudo rm -r /var/lib/mongodb