Windows10安装参考: https://blog.csdn.net/gaokcl/article/details/81982905
php + mongodb增删改查(原生): https://blog.csdn.net/gaokcl/article/details/85274958 # 增删改查
wamp安装php的MongoDB扩展:https://blog.csdn.net/qq_33210743/article/details/81533980
1,CentOS安装,Mongodb4.0.1
不要make编译安装,下载好,配置好就行了,配置安装如下
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-4.0.1.tgz
tar -zxvf mongodb-linux-x86_64-4.0.1.tgz
解压后可以删除: rm -rf mongodb-linux-x86_64-4.0.1.tgz
mkdir -p /usr/local/mongodb/
-----------------------注意安装包解压的位置------------------------
mv /root/mongodb-linux-x86_64-4.0.1/* /usr/local/mongodb/
或者
mv /usr/local/mongodb-linux-x86_64-4.0.1/* /usr/local/mongodb/
-----------------------------------------------------------------
移动后可以删除文件夹: rm -rf mongodb-linux-x86_64-4.0.1
mkdir -p /usr/local/mongodb/data/db/ # mongodb的db要自己建
mkdir -p /usr/local/mongodb/logs/
cd /usr/local/mongodb/logs/
touch mongodb.log # mongodb的log要自己建
2,配置:
dbpath=/usr/local/mongodb/data/db
logpath=/usr/local/mongodb/logs/mongodb.log
port=27017
logappend=true
fork=true
bind_ip=0.0.0.0
#auth=true
1,mongodb.conf(这个文件是没有的,自己创建需要自己创建并且放在/usr/local/mongodb/bin/目录下)
cd /usr/local/mongodb/bin/
vim mongodb.conf #输入以下配置
dbpath=/usr/local/mongodb/data/db #数据文件存放目录
logpath=/usr/local/mongodb/logs/mongodb.log #日志文件存放目录
port=27017 #端口,默认27017,可以自定义
logappend=true #开启日志追加添加日志
fork=true #以守护程序的方式启用,即在后台运行
bind_ip=0.0.0.0 #默认是127.0.0.1,开启远程访问
#auth=true(这项暂时不动,因为涉及到auth认证,调试好所有的mongodb的问题后在来弄权限)
注意:如果不加后台启动参数fork,这个页面不能关闭,使用命令连接数据库时要另打开一个窗口
3,添加mongodb环境:
vi /etc/profile
在文件最后一行添加:
export MONGODB_HOME=/usr/local/mongodb
export PATH=$PATH:$MONGODB_HOME/bin
source /etc/profile(立即生效)
4.启动mongodb:
cd /usr/local/mongodb/bin/
mongod --config mongodb.conf # 或者输入:
5.开机自动启动mongodb:
编辑/etc/rc.d/rc.local,
在文件后面加上如下这行:
/usr/local/mongodb/bin/mongod --config /usr/local/mongodb/bin/mongodb.conf
6.添加端口27017:
cd /etc/sysconfig/ (切换目录)
注意:如果没有iptables这个文件,因为centos7自带firewall
输入命令: yum install iptables-services # 更新或者安装iptables
然后: systemctl enable iptables # 启动iptables
最后: systemctl start iptables # 打开iptables
vim iptables # 添置:
-A INPUT -m state --state NEW -m tcp -p tcp --dport 27017 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited # 默认有的,上一句位置不要改
7.重启iptable:
systemctl restart iptables.service (重新启动)
systemctl enable iptables.service (启动生效)
8,在 cd /usr/local/mongodb/bin/ mongod 会出现:
Error: couldn't connect to server 127.0.0.1:27017
1、若数据库出现如上不能连接的原因,可能是data目录下的mongod.lock文件问题,可以用如下命令修复
[root@localhost mongodb]# ./bin/mongod --repair
或者直接删除mongod.lock
rm -rf /usr/local/mongodb/data/db/mongod.lock
2、如果第一步解决不了,那么就是路径设置有问题,通过重新对路径的设置解决此问题
[root@localhost mongodb]# ./bin/mongod --dbpath=/usr/local/mongodb/data/db/
9,出现 MongoDB starting : pid=2079 port=27017 dbpath=/root/mongodb/data/db/ 表示成功
10,使用,测试mongodb
使用MongoDB:
cd /usr/local/mongodb/bin
mongod
mongo # 进入命令行
9后 表示设置成功,此时不要关闭这个界面,需要 xshell 重新打开一个窗口来启动 mongo
cd /usr/local/mongodb
./bin/mongo
注意:mongodb数据库无法创建和添加数据的问题(这里不建议使用admin,local,config三个数据库),直接输入
> use test
> db.test.insert({id:1})
> show dbs, # 是不是发现多了一个test???问题解决
******************************************************************************************************************************************************
11,常用的快捷键:(配置好后,方便使用)
1、若数据库出现如上不能连接的原因,可能是data目录下的mongod.lock文件问题,可以用如下命令修复
[root@localhost mongodb]# ./bin/mongod --repair
或者直接删除mongod.lock
rm -rf /usr/local/mongodb/data/db/mongod.lock
2,启动MongoDB:
cd /usr/local/mongodb/data/db/
./bin/mongod # 建立连接
./bin/mongo # 使用MongoDB ( Xsell新开窗口 ),输入sql语句相关
3,也可以通过mongoVUE这个可视化工具来连接,或者 Navicat 12 for MongoDB
4,停止mongodb
# cd /usr/local/mongodb/bin
# ./mongod -shutdown -dbpath=/usr/local/mongodb/data
5,连接mongodb,可以直接使用命令mongo进入mongodb命令行
# cd /usr/local/mongodb/bin
# ./mongo
***************************************************************************************************************************************************
12,参考:
# centos7安装MongoDB4.0
https://blog.csdn.net/MiaodXindng/article/details/81774273
# 可能出现错误
https://blog.csdn.net/wangli61289/article/details/44459467
****************************************************************************************************************************************************
13,特别注意:( mogodb 启动 与 关闭 )
¥,mogodb 启动
1,缺省的启动方式
./mongod
缺省的启动方式,数据存储目录里是/data/db,监听端口是27017
2,带基本参数的启动方式
./mongod --port 51017 --dbpath /home/mongo/data --logpath /home/mongo/log/log.log --logappend --fork --journal 1000
基本参数:
-f 指定配置文件 (参考:http://www.mongodb.org/display/DOCS/File+Based+Configuration)
--port 指定端口,默认是27017
--dbpath 数据目录路径
--logpath 日志文件路径
--logappend 日志append而不是overwrite
--fork 以创建子进程的方式运行
--journal 日志提交间隔,默认100ms
--nojournal 关闭日志功能,2.0版本以上是默认开启的
¥,mongodb关闭:
> use admin
> db.shutdownServer()
> db.shutdownServer({force : true}) # 强制关闭Mongod,应对副本集中主从时间差超过10s时不允许关闭主库的情况
不要使用kill直接杀mongo进程的方式关闭数据节点,会造成数据损坏
**************************** 最终效果图 *****************************************************
xsell 链接 centos 7
[root@localhost ~]# cd /usr/local/mongodb/
[root@localhost mongodb]# ll
总用量 120
drwxr-xr-x. 2 root root 251 8月 23 14:39 bin
-rw-r--r--. 1 root root 34520 8月 4 01:43 GNU-AGPL-3.0
-rw-r--r--. 1 root root 2149 8月 4 01:43 LICENSE-Community.txt
drwxr-xr-x. 2 root root 25 8月 23 14:35 logs
-rw-r--r--. 1 root root 16726 8月 4 01:43 MPL-2
-rw-r--r--. 1 root root 2195 8月 4 01:43 README
-rw-r--r--. 1 root root 57190 8月 4 01:43 THIRD-PARTY-NOTICES
[root@localhost mongodb]# cd logs
[root@localhost logs]# ll
总用量 4
-rw-r--r--. 1 root root 1437 8月 23 14:40 mongodb.log
[root@localhost logs]#
[root@localhost logs]# cd
[root@localhost ~]#
[root@localhost ~]# cd /usr/local/mongodb/
[root@localhost mongodb]# ll
总用量 120
drwxr-xr-x. 2 root root 251 8月 23 14:39 bin
-rw-r--r--. 1 root root 34520 8月 4 01:43 GNU-AGPL-3.0
-rw-r--r--. 1 root root 2149 8月 4 01:43 LICENSE-Community.txt
drwxr-xr-x. 2 root root 25 8月 23 14:35 logs
-rw-r--r--. 1 root root 16726 8月 4 01:43 MPL-2
-rw-r--r--. 1 root root 2195 8月 4 01:43 README
-rw-r--r--. 1 root root 57190 8月 4 01:43 THIRD-PARTY-NOTICES
[root@localhost mongodb]# cd bin
[root@localhost bin]# ll
总用量 208816
-rwxr-xr-x. 1 root root 6906321 8月 4 01:43 bsondump
-rwxr-xr-x. 1 root root 5792 8月 4 01:59 install_compass
-rwxr-xr-x. 1 root root 38886336 8月 4 01:59 mongo
-rwxr-xr-x. 1 root root 60972352 8月 4 01:59 mongod
-rw-r--r--. 1 root root 151 8月 23 14:39 mongodb.conf
-rwxr-xr-x. 1 root root 10255092 8月 4 01:43 mongodump
-rwxr-xr-x. 1 root root 8245971 8月 4 01:43 mongoexport
-rwxr-xr-x. 1 root root 8123365 8月 4 01:43 mongofiles
-rwxr-xr-x. 1 root root 8404330 8月 4 01:43 mongoimport
-rwxr-xr-x. 1 root root 9322864 8月 4 01:43 mongoreplay
-rwxr-xr-x. 1 root root 11451628 8月 4 01:43 mongorestore
-rwxr-xr-x. 1 root root 34621336 8月 4 01:59 mongos
-rwxr-xr-x. 1 root root 8483293 8月 4 01:43 mongostat
-rwxr-xr-x. 1 root root 8116571 8月 4 01:43 mongotop
[root@localhost bin]# mongod
2018-08-23T16:23:30.288+0800 I CONTROL [initandlisten] MongoDB starting : pid=8416 port=27017 dbpath=/data/db 64-bit host=localhost.localdomain
2018-08-23T16:23:30.288+0800 I CONTROL [initandlisten] db version v4.0.1
2018-08-23T16:23:30.288+0800 I CONTROL [initandlisten] git version: 54f1582fc6eb01de4d4c42f26fc133e623f065fb
2018-08-23T16:23:30.288+0800 I CONTROL [initandlisten] allocator: tcmalloc
2018-08-23T16:23:30.288+0800 I CONTROL [initandlisten] modules: none
2018-08-23T16:23:30.288+0800 I CONTROL [initandlisten] build environment:
2018-08-23T16:23:30.288+0800 I CONTROL [initandlisten] distarch: x86_64
2018-08-23T16:23:30.288+0800 I CONTROL [initandlisten] target_arch: x86_64
2018-08-23T16:23:30.288+0800 I CONTROL [initandlisten] options: {}
2018-08-23T16:23:30.292+0800 I STORAGE [initandlisten] Detected data files in /data/db created by the 'wiredTiger' storage engine, so setting the active storage engine to 'wiredTiger'.
2018-08-23T16:23:30.292+0800 I STORAGE [initandlisten] wiredtiger_open config: create,cache_size=256M,session_max=20000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),statistics_log=(wait=0),verbose=(recovery_progress),
2018-08-23T16:23:31.275+0800 I STORAGE [initandlisten] WiredTiger message [1535012611:275666][8416:0x7f3078f489c0], txn-recover: Main recovery loop: starting at 5/16128
2018-08-23T16:23:31.377+0800 I STORAGE [initandlisten] WiredTiger message [1535012611:377240][8416:0x7f3078f489c0], txn-recover: Recovering log 5 through 6
2018-08-23T16:23:31.447+0800 I STORAGE [initandlisten] WiredTiger message [1535012611:447687][8416:0x7f3078f489c0], txn-recover: Recovering log 6 through 6
2018-08-23T16:23:31.506+0800 I STORAGE [initandlisten] WiredTiger message [1535012611:506397][8416:0x7f3078f489c0], txn-recover: Set global recovery timestamp: 0
2018-08-23T16:23:31.522+0800 I RECOVERY [initandlisten] WiredTiger recoveryTimestamp. Ts: Timestamp(0, 0)
2018-08-23T16:23:31.533+0800 I CONTROL [initandlisten]
2018-08-23T16:23:31.533+0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-08-23T16:23:31.533+0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2018-08-23T16:23:31.533+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2018-08-23T16:23:31.533+0800 I CONTROL [initandlisten]
2018-08-23T16:23:31.533+0800 I CONTROL [initandlisten] ** WARNING: This server is bound to localhost.
2018-08-23T16:23:31.533+0800 I CONTROL [initandlisten] ** Remote systems will be unable to connect to this server.
2018-08-23T16:23:31.533+0800 I CONTROL [initandlisten] ** Start the server with --bind_ip to specify which IP
2018-08-23T16:23:31.533+0800 I CONTROL [initandlisten] ** addresses it should serve responses from, or with --bind_ip_all to
2018-08-23T16:23:31.533+0800 I CONTROL [initandlisten] ** bind to all interfaces. If this behavior is desired, start the
2018-08-23T16:23:31.533+0800 I CONTROL [initandlisten] ** server with --bind_ip 127.0.0.1 to disable this warning.
2018-08-23T16:23:31.533+0800 I CONTROL [initandlisten]
2018-08-23T16:23:31.534+0800 I CONTROL [initandlisten]
2018-08-23T16:23:31.534+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2018-08-23T16:23:31.534+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2018-08-23T16:23:31.534+0800 I CONTROL [initandlisten]
2018-08-23T16:23:31.534+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2018-08-23T16:23:31.534+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2018-08-23T16:23:31.534+0800 I CONTROL [initandlisten]
2018-08-23T16:23:31.557+0800 I FTDC [initandlisten] Initializing full-time diagnostic data capture with directory '/data/db/diagnostic.data'
2018-08-23T16:23:31.558+0800 I NETWORK [initandlisten] waiting for connections on port 27017
2018-08-23T16:24:11.511+0800 I NETWORK [listener] connection accepted from 127.0.0.1:48394 #1 (1 connection now open)
2018-08-23T16:24:11.513+0800 I NETWORK [conn1] received client metadata from 127.0.0.1:48394 conn1: { application: { name: "MongoDB Shell" }, driver: { name: "MongoDB Internal Client", version: "4.0.1" }, os: { type: "Linux", name: "CentOS Linux release 7.5.1804 (Core) ", architecture: "x86_64", version: "Kernel 3.10.0-862.el7.x86_64" } }
xsell另一个窗口打开:
[root@localhost ~]# cd /usr/local/mongodb/bin/
[root@localhost bin]# ll
总用量 208816
-rwxr-xr-x. 1 root root 6906321 8月 4 01:43 bsondump
-rwxr-xr-x. 1 root root 5792 8月 4 01:59 install_compass
-rwxr-xr-x. 1 root root 38886336 8月 4 01:59 mongo
-rwxr-xr-x. 1 root root 60972352 8月 4 01:59 mongod
-rw-r--r--. 1 root root 151 8月 23 14:39 mongodb.conf
-rwxr-xr-x. 1 root root 10255092 8月 4 01:43 mongodump
-rwxr-xr-x. 1 root root 8245971 8月 4 01:43 mongoexport
-rwxr-xr-x. 1 root root 8123365 8月 4 01:43 mongofiles
-rwxr-xr-x. 1 root root 8404330 8月 4 01:43 mongoimport
-rwxr-xr-x. 1 root root 9322864 8月 4 01:43 mongoreplay
-rwxr-xr-x. 1 root root 11451628 8月 4 01:43 mongorestore
-rwxr-xr-x. 1 root root 34621336 8月 4 01:59 mongos
-rwxr-xr-x. 1 root root 8483293 8月 4 01:43 mongostat
-rwxr-xr-x. 1 root root 8116571 8月 4 01:43 mongotop
[root@localhost bin]# mongo
MongoDB shell version v4.0.1
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 4.0.1
Server has startup warnings:
2018-08-23T16:23:31.533+0800 I CONTROL [initandlisten]
2018-08-23T16:23:31.533+0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-08-23T16:23:31.533+0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2018-08-23T16:23:31.533+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2018-08-23T16:23:31.533+0800 I CONTROL [initandlisten]
2018-08-23T16:23:31.533+0800 I CONTROL [initandlisten] ** WARNING: This server is bound to localhost.
2018-08-23T16:23:31.533+0800 I CONTROL [initandlisten] ** Remote systems will be unable to connect to this server.
2018-08-23T16:23:31.533+0800 I CONTROL [initandlisten] ** Start the server with --bind_ip to specify which IP
2018-08-23T16:23:31.533+0800 I CONTROL [initandlisten] ** addresses it should serve responses from, or with --bind_ip_all to
2018-08-23T16:23:31.533+0800 I CONTROL [initandlisten] ** bind to all interfaces. If this behavior is desired, start the
2018-08-23T16:23:31.533+0800 I CONTROL [initandlisten] ** server with --bind_ip 127.0.0.1 to disable this warning.
2018-08-23T16:23:31.533+0800 I CONTROL [initandlisten]
2018-08-23T16:23:31.534+0800 I CONTROL [initandlisten]
2018-08-23T16:23:31.534+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2018-08-23T16:23:31.534+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2018-08-23T16:23:31.534+0800 I CONTROL [initandlisten]
2018-08-23T16:23:31.534+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2018-08-23T16:23:31.534+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2018-08-23T16:23:31.534+0800 I CONTROL [initandlisten]
> show dbs;
admin 0.000GB
config 0.000GB
local 0.000GB
test 0.000GB
>
三,PHP安装MongoDB扩展的问题
1,问题
运行/usr/local/php7/bin/pecl install mongodb 时出现:
Configuring for:
PHP Api Version: 20041225
Zend Module Api No: 20060613
Zend Extension Api No: 220060519
Cannot find autoconf. Please check your autoconf installation and the
$PHP_AUTOCONF environment variable. Then, rerun this script.
2,解决
cd /usr/src
wget http://ftp.gnu.org/gnu/m4/m4-1.4.9.tar.gz
tar -zvxf m4-1.4.9.tar.gz
cd m4-1.4.9/
./configure && make && make install
cd ../
wget http://ftp.gnu.org/gnu/autoconf/autoconf-2.62.tar.gz
tar -zvxf autoconf-2.62.tar.gz
cd autoconf-2.62/
./configure && make && make install
更简单的办法
yum install m4
yum install autoconf