Reputation: 11
I have a strange situation - four days of fruitless search and experiments. I installed mongoDB 3.2.8 on my Ubuntu 16.04 with this tutorial. So I have the following result:
maksim@maksim:~$ sudo systemctl status mongodb
[sudo] пароль для maksim:
● mongodb.service - High-performance, schema-free document-oriented database
Loaded: loaded (/etc/systemd/system/mongodb.service; disabled; vendor preset:
Active: active (running) since Чт 2016-08-04 18:03:00 YEKT; 4h 31min ago
Main PID: 11147 (mongod) CGroup: /system.slice/mongodb.service
└─11147 /usr/bin/mongod --quiet --config /etc/mongod.conf
авг 04 18:03:00 maksim systemd[1]: Started High-performance, schema-free documen lines 1-8/8 (END)
So I guess mongoDB works
When I type mongo
, I have this result:
maksim@maksim:~$ mongo
MongoDB shell version: 3.2.8
connecting to: test
Server has startup warnings:
2016-08-04T18:03:01.968+0500 I CONTROL [initandlisten]
2016-08-04T18:03:01.968+0500 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2016-08-04T18:03:01.968+0500 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2016-08-04T18:03:01.968+0500 I CONTROL [initandlisten]
2016-08-04T18:03:01.968+0500 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2016-08-04T18:03:01.968+0500 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2016-08-04T18:03:01.968+0500 I CONTROL [initandlisten]
>
When I type mongod
, I have that:
maksim@maksim:~$ mongod
2016-08-04T22:51:16.857+0500 I CONTROL [initandlisten] MongoDB starting : pid=16018 port=27017 dbpath=/data/db 64-bit host=maksim
2016-08-04T22:51:16.857+0500 I CONTROL [initandlisten] db version v3.2.8
2016-08-04T22:51:16.857+0500 I CONTROL [initandlisten] git version: ed70e33130c977bda0024c125b56d159573dbaf0
2016-08-04T22:51:16.857+0500 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.2g-fips 1 Mar 2016
2016-08-04T22:51:16.857+0500 I CONTROL [initandlisten] allocator: tcmalloc
2016-08-04T22:51:16.857+0500 I CONTROL [initandlisten] modules: none
2016-08-04T22:51:16.857+0500 I CONTROL [initandlisten] build environment:
2016-08-04T22:51:16.857+0500 I CONTROL [initandlisten] distmod: ubuntu1404
2016-08-04T22:51:16.857+0500 I CONTROL [initandlisten] distarch: x86_64
2016-08-04T22:51:16.857+0500 I CONTROL [initandlisten] target_arch: x86_64
2016-08-04T22:51:16.857+0500 I CONTROL [initandlisten] options: {}
2016-08-04T22:51:16.886+0500 E NETWORK [initandlisten] listen(): bind() failed errno:98 Address already in use for socket: 0.0.0.0:27017
2016-08-04T22:51:16.886+0500 E NETWORK [initandlisten] addr already in use
2016-08-04T22:51:16.886+0500 E STORAGE [initandlisten] Failed to set up sockets during startup.
2016-08-04T22:51:16.886+0500 I CONTROL [initandlisten] dbexit: rc: 48
I noticed the error (4th string from bottom). And the middle string (Ubuntu 14.04)
embarrass me also, because I have Ubuntu 16.04.
In some tutorials, or forums (forgot where, may be here too - huge amount of information I had read last days) suggest to type the following:
sudo service mongod start
But the result is the following:
Failed to start mongod.service: Unit mongod.service not found.
Having all this circumstates my simple express workpiece based just on the mongoDB works correctly. But when I installed mongoose (4.5.7) I begun to have a problem: mongoose does not want to connect to the mongoDb with my config.json file:
maksim@maksim:~/jspract/osnmongoose/firstmongooseapp$ nodejs ./bin/www
{ username: 'Tester3',
hashedPassword: '230a508d5a8f3ddac8f5c52ec3439f25b9c730bd',
salt: '0.7840297885704786',
_id: 57a33d6bfa062be32b2d10a5,
created: Thu Aug 04 2016 18:04:43 GMT+0500 (YEKT) }
3undefined
11undefined
undefined
info: [bin/www] Express server listening on port 3000
Mongoose default connection disconnected
Mongoose default connection error: MongoError: getaddrinfo ENOTFOUND undefined undefined:27017
3undefined, 11undefined, undefined - are my internal checking things - forget it. The two last strings are EventListeners. I don't think that there is the error in config file, because I take the port 3000 setting from it too and export the port settings with 'nconf' correctly.
{
"port": 3000,
"mongoose": {
"uri": "mongodb://127.0.0.1:27017/chat",
"options": {
"server": {
"socketOptions": {
"keepAlive": 1
}
}
}
}
}
Impotant note: when I put the mongoose settings into my app.js directly, mongoose connects with mongoDB correctly.
Hope, I didn't tire you too much. I want to understand what causes an error when calling mongod:
errno:98 Address already in use for socket: 0.0.0.0:27017
Failed to start mongod.service: Unit mongod.service not found.
Could be the failure of mongoose connection to mongoDB been caused the mongod error or just better to find the error in my programming code? I am convinced that the Mongo-team would have long been aware of the problem, if the problem really existed. I can not understand what was going on. Tell me please.
I'll repeat again:
Upvotes: 0
Views: 1383
Reputation: 4991
You have already a mongod process. mongod is the "Mongo Daemon" you don't have to use it. This is why you have :
2016-08-04T22:51:16.886+0500 E NETWORK [initandlisten] listen(): bind() failed errno:98 Address already in use for socket: 0.0.0.0:27017
If you want to access to mongo then type this command :
mongo
You can't restart mongod because you set mongodb.service inside /etc/systemd/system/mongodb.service; that is why mongod.service not found. If you want restart mongo use this.
sudo service mongodb start
You have warnings when you type mongo command but it works. After your warnings you have this symbol >
so you can see your mongo database.
So type the mongo command and try this :
show dbs;
If you see the locale database it means that it works.
If you want to fix your warnings, you should install linux kernel. But before installing the kernel you can try this https://docs.mongodb.com/manual/tutorial/transparent-huge-pages/
They said to create the following file at /etc/init.d/disable-transparent-hugepages:
#!/bin/sh
### BEGIN INIT INFO
# Provides: disable-transparent-hugepages
# Required-Start: $local_fs
# Required-Stop:
# X-Start-Before: mongod mongodb-mms-automation-agent
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Disable Linux transparent huge pages
# Description: Disable Linux transparent huge pages, to improve
# database performance.
### END INIT INFO
case $1 in
start)
if [ -d /sys/kernel/mm/transparent_hugepage ]; then
thp_path=/sys/kernel/mm/transparent_hugepage
elif [ -d /sys/kernel/mm/redhat_transparent_hugepage ]; then
thp_path=/sys/kernel/mm/redhat_transparent_hugepage
else
return 0
fi
echo 'never' > ${thp_path}/enabled
echo 'never' > ${thp_path}/defrag
unset thp_path
;;
esac
Save your file and type this command :
sudo chmod 755 /etc/init.d/disable-transparent-hugepages
And :
sudo update-rc.d disable-transparent-hugepages defaults
And restart your server.
Mongoose side you should connect your NodeJS app like this :
server.js
var mongoose = require('mongoose');
mongoose.Promise = global.Promise;
mongoose.connect(mongodb://localhost/yourdatabase); // For Nginx
mongoose.connect(mongodb://localhost:27017/yourdatabase); // Basic
Hope it helps.
Upvotes: 3
Reputation: 11
My app:
.
├── app.js
├── bin
├── config
├── createDb.js
├── libs
├── models
├── node_modules
├── package.json
├── public
├── routes
└── views
Excuse me if it not convenient for reading - I learned the 'tree' now only.
My /config/config.json
:
{
"port": 3000,
"mongoose": {
"uri": "mongodb://127.0.0.1:27017/chat",
"options": {
"server": {
"socketOptions": {
"keepAlive": 1
}
}
}
}
}
And /config/index.js
:
var nconf = require('nconf');
var path = require('path');
nconf.argv();
nconf.env();
nconf.file({ file: path.join(__dirname, 'config.json') });
module.exports = nconf;
There are no more files in /config
directory.
Further the /libs/mongoose.js
:
var mongoose = require('mongoose');
var configure = require('../config');
// ('nconf');
mongoose.connect(configure.get("mongoose: uri"), configure.get("mongoose: options"));
module.exports = mongoose;
Further the /models/user.js
:
var crypto = require("crypto");
var mongoose = require('../libs/mongoose');
var Schema = mongoose.Schema;
var schema = new Schema({
username: {
type: String,
unique: true,
required: true
},
hashedPassword: {
type: String,
required: true
},
salt: {
type: String,
required: true
},
created: {
type: Date,
default: Date.now
}
});
schema.methods.encryptPassword = function(password){
return crypto.createHmac('sha1', this.salt).update(password).digest('hex');
};
schema.virtual('password')
.set(function(password){
this._plainPassword = password;
this.salt = Math.random() + '';
this.hashedPassword = this.encryptPassword(password);
})
.get(function(){return this._plainPassword});
schema.methods.checkPassword = function(password){
return this.encryptPassword(password) ===this.hashedPassword;
};
var User = mongoose.model('User', schema);
module.exports = User;
Further I export the USER into thecreateDb.js
. So, my createDb.js
:
var mongoose = require('mongoose');
var User = require('./models/user');
var user = new User({
username: "Tester3",
password: "secret"
});
console.log(user);
mongoose.connection.on('connecting', function() {
console.log("TRY to Connecting to database")
});
mongoose.connection.on('connected', function() {
console.log("Connected to database")
});
mongoose.connection.on('error',function (err) {
console.log('Mongoose default connection error: ' + err);
});
mongoose.connection.on('disconnected', function () {
console.log('Mongoose default connection disconnected');
});
var process = { env: { ENV: '' } };
if (process.env.ENV === 'staging') {
console.log('1' + process.env.MONGODB_URI);
} else if (process.env.ENV === 'production') {
console.log('2');
} else {
console.log('3' + process.env.MONGODB_URI);
} if (process.env.ENV === 'staging' || 'production') {
console.log('11' + process.env.MONGODB_URI);
} else {
console.log('12');
}
var mongoUri = process.env.MONGOLAB_URI;
console.log(mongoUri);
And the ./bin/www/
at the end:
#!/usr/bin/nodejs
//NODE_PATH=..
var debug = require('debug')('my-application');
var app = require('../app');
var MongoClient = require('../createDb');
var config = require('../config');
var log = require('../libs/log')(module);
app.set('port', process.env.PORT || config.get('port'));
var server = app.listen(app.get('port'), function() {
debug('Express server listening on port ' + server.address().port);
log.info('Express server listening on port ' + server.address().port);
});
So, after all, the result looks like this, unfortunately:
{ username: 'Tester3',
hashedPassword: 'f481d1150a2e44f10534d95384092c1852ff7e4d',
salt: '0.07912106881849468',
_id: 57a4dbaeb5aae5401c403fd2,
created: Fri Aug 05 2016 23:32:14 GMT+0500 (YEKT) }
3undefined
11undefined
undefined
info: [bin/www] Express server listening on port 3000
Mongoose default connection disconnected
Mongoose default connection error: MongoError: getaddrinfo ENOTFOUND undefined undefined:27017
The /etc/mongod.conf
just in case(I deleted all commented things for shortness):
storage:
dbPath: /var/lib/mongodb
journal:
enabled: true
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log
net:
port: 27017
bindIp: 127.0.0.1
I am sure that without the help of a specialist I did not make out in my question.
Upvotes: 0