can I connect bash to an existing mongod process? - bash

I run two mongo databases on my dev machine. The first runs on the standard port and (apparently) runs at startup. The second I start up as part of my "kickoff the dev env" script. How can I get the output from the first database in bash? I'm talking about the output that looks like:
Wed Oct 30 16:28:27.035 [initandlisten] recover : no journal files present, no recovery needed
Wed Oct 30 16:28:27.215 [initandlisten] waiting for connections on port XX987
Wed Oct 30 16:28:27.216 [websvr] admin web console waiting for connections on port XX987
Wed Oct 30 16:28:34.054 [initandlisten] connection accepted from 127.0.0.1:53736 #1 (1 connection now open)
Wed Oct 30 16:28:34.060 [conn1] end connection 127.0.0.1:53736 (0 connections now open)
Wed Oct 30 16:28:34.061 [initandlisten] connection accepted from 127.0.0.1:53737 #2 (1 connection now open)
Is this called a listening socket? How can I connect to this output from bash?

It looks like you are showing the output of the mongodb log file. In order to separate the log messages of the different mongo processes you'll need to configure separate log file locations for both mongo processes. Then you can just:
tail -f /path/to/mongo_dev/logfile
You can change the log file location of a mongodb process by setting
logpath=/path/to/anyhwere.log
in it's config file. Then restart the mongodb process.

Related

E NETWORK [thread1] listen(): listen() failed An attempt was made to access a socket in a way forbidden by its access permissions

I am new to MongoDB. However, after installing it, in my command prompt, I typed md \data\db where it created a folder as said in my C drive. Now after it, I typed
"mongod"
to start the mongodb server first.
It showed the following in cmd.
2017-10-24T23:08:28.525+0530 I CONTROL [initandlisten] MongoDB
starting : pid=3 608 port=27017 dbpath=C:\data\db\ 64-bit
host=INSPIRION 2017-10-24T23:08:28.527+0530 I CONTROL [initandlisten]
targetMinOS: Windows 7/W indows Server 2008 R2
2017-10-24T23:08:28.527+0530 I CONTROL [initandlisten] db version
v3.4.9 2017-10-24T23:08:28.527+0530 I CONTROL [initandlisten] git
version: 876ebee8c7d d0e2d992f36a848ff4dc50ee6603e
2017-10-24T23:08:28.528+0530 I CONTROL [initandlisten] OpenSSL
version: OpenSSL
1.0.1u-fips 22 Sep 2016 2017-10-24T23:08:28.528+0530 I CONTROL [initandlisten] allocator: tcmalloc 2017-10-24T23:08:28.529+0530 I
CONTROL [initandlisten] modules: none 2017-10-24T23:08:28.529+0530 I
CONTROL [initandlisten] build environment:
2017-10-24T23:08:28.529+0530 I CONTROL [initandlisten] distmod:
2008plus-ss l 2017-10-24T23:08:28.530+0530 I CONTROL [initandlisten]
distarch: x86_64 2017-10-24T23:08:28.530+0530 I CONTROL
[initandlisten] target_arch: x86_64 2017-10-24T23:08:28.531+0530 I
CONTROL [initandlisten] options: { net: { bindIp : "127.0.0.1" } }
2017-10-24T23:08:28.532+0530 W - [initandlisten] Detected
unclean shutdow n - C:\data\db\mongod.lock is not empty.
2017-10-24T23:08:28.533+0530 I - [initandlisten] Detected data
files in C :\data\db\ created by the 'wiredTiger' storage engine, so
setting the active sto rage engine to 'wiredTiger'.
2017-10-24T23:08:28.535+0530 W STORAGE [initandlisten] Recovering
data from the last clean checkpoint. 2017-10-24T23:08:28.535+0530 I
STORAGE [initandlisten] wiredtiger_open config:
create,cache_size=1487M,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),checkpoint=(wait=60,lo
g_size=2GB),statistics_log=(wait=0), 2017-10-24T23:08:30.559+0530 I
CONTROL [initandlisten] 2017-10-24T23:08:30.559+0530 I CONTROL
[initandlisten] ** WARNING: Access contr ol is not enabled for the
database. 2017-10-24T23:08:30.560+0530 I CONTROL [initandlisten] **
Read and wri te access to data and configuration is unrestricted.
2017-10-24T23:08:30.560+0530 I CONTROL [initandlisten]
2017-10-24T23:08:30.805+0530 I FTDC [initandlisten] Initializing
full-time d iagnostic data capture with directory
'C:/data/db/diagnostic.data' 2017-10-24T23:08:31.087+0530 I FTDC
[ftdc] Unclean full-time diagnostic data capture shutdown detected,
found interim file, some metrics may have been lost. OK
2017-10-24T23:08:31.249+0530 E NETWORK [thread1] listen(): listen()
failed An attempt was made to access a socket in a way forbidden by
its access permissions.
And I am wondering about the last line here :
2017-10-24T23:08:31.249+0530 E NETWORK [thread1] listen(): listen()
failed An attempt was made to access a socket in a way forbidden by
its access permissions.
I could not get a correct solution for this error. Can anyone help me in resolving this because only after this, I will be able to start the shell program which shows the following currently.
C:\Users\DELL>mongo MongoDB shell version v3.4.9 connecting to:
mongodb://127.0.0.1:27017 2017-10-24T23:21:39.937+0530 W NETWORK
[thread1] Failed to connect to 127.0.0.1 :27017 after 5000ms
milliseconds, giving up. 2017-10-24T23:21:39.938+0530 E QUERY
[thread1] Error: couldn't connect to ser ver 127.0.0.1:27017,
connection attempt failed : connect#src/mongo/shell/mongo.js:237:13
#(connect):1:6 exception: connect failed
For additional info, am using windows
Thanks in advance.

curl do not work if it is executed from a batch file

I am stuck with this issue:
I am trying to run curl using a batch file which is automatically called when password hook is running. The content of this batch file is:
curl "http://10.x.y.z/passwd.php?user=%1&pass=%2"
User and password are automatically read as you can see below in error log.
But I am still receiving a curl(27) out of memory ERROR as you can see below:
[Wed Sep 09 12:09:42 2015]
"C:\passwd.bat" testuser Pass#1234
c:\>curl "http://10.x.y.z/passwd.php?user=testuser&pass=Pass#1234"
curl: (27) Out of memory
[Wed Sep 09 12:09:43 2015]
If I run the batch file from command prompt with passwd.bat testuser Pass#1234, everything is OK.
Curl and .bat file are running on a Windows Server 2012 R2 machine.
Do you have any ideas about how could I solve this issue?

Where can I find a list of correspondences between mongodb Windows and mongodb Linux?

For example (I wonder if all these should be separate questions):
What would be the equivalent of mongod.exe in Linux?
What's the equivalent of mongodb.conf in Windows?
How do we make mongodb to launch when computer start in Windows?
For example, in mongodb windows, I have this file called mongod.exe running it run the mongod "service" or something and then it shows status of what's going on at mongodb.
For example this is what mongod.exe shows:
Fri Aug 31 16:08:13 [conn6232] end connection 127.0.0.1:19782
Fri Aug 31 16:08:13 [initandlisten] connection accepted from 127.0.0.1:19783 #
33
Fri Aug 31 16:08:13 [conn6233] end connection 127.0.0.1:19783
Fri Aug 31 16:08:13 [initandlisten] connection accepted from 127.0.0.1:19784 #
34
Fri Aug 31 16:08:13 [conn6234] end connection 127.0.0.1:19784
Fri Aug 31 16:08:13 [initandlisten] connection accepted from 127.0.0.1:19785 #
35
Fri Aug 31 16:08:13 [conn6235] end connection 127.0.0.1:19785
Fri Aug 31 16:08:13 [initandlisten] connection accepted from 127.0.0.1:19786 #
36
Fri Aug 31 16:08:13 [conn6236] end connection 127.0.0.1:19786
Fri Aug 31 16:08:13 [initandlisten] connection accepted from 127.0.0.1:19787 #
37
Fri Aug 31 16:08:13 [initandlisten] connection accepted from 127.0.0.1:19788 #
38
Fri Aug 31 16:08:13 [conn6238] end connection 127.0.0.1:19788
What would be the equivalent of mongod.exe in linux?
Also in Linux we have this thing called mongodb.conf where we set up, for example, where the data directory is. In windows, what's the equivalent of mongodb.conf in windows? In windows, if I want to change data directory I simply copy and move the whole mongod directory. I am not even sure whether that's the right way to do it.
Then in Linux, once I install mongodb it's automatically run everytime I restarted the server. In windows I have to run mongod.exe first. Hmm... How can I arrange that mongodb run automatically as a service in Windows?
What would be the equivalent of mongod.exe in Linux?
Answer: mongod (Linux does not use file extensions to denote executables)
What's the equivalent of mongodb.conf in Windows?
They are identical - this is just a text file on both systems, you can use the same one on both (after adjusting for file path differences etc.)
How do we make mongodb to launch when computer start in Windows?
You install it as a service, as outlined here:
http://www.mongodb.org/display/DOCS/Windows+Service

Change log time format of OpenSSH

OpenSSH log in my system are spitting log in following format.
Aug 21 14:34:44 systemHostName sshd[1952]: Server listening on 0.0.0.0 port 22.
I need to get the year also, in the log time. something like following
2012 Aug 21 14:34:44 systemHostName sshd[1952]: Server listening on 0.0.0.0 port 22.
I tried to look into sshd_config and other web resources but could not find anything useful.
Anybody know, how to change openSSH log time format?
It is syslog, not sshd time format, depends on your syslog implementation, though I haven't seen anyone using the year there.

MongoDB running but can't connect using shell

CentOS 5.x Linux with MongoDB 2.0.1 (tried main and legacy-static)
MongoDB is running:
root 31664 1.5 1.4 81848 11148 ? Sl 18:40 0:00 ./mongod -f mongo.conf -vvvvv --fork
Using a simple shell connect to get to the server fails:
[root#xxxx bin]# ./mongo
MongoDB shell version: 2.0.1
connecting to: test
Mon Oct 31 18:41:32 Error: couldn't connect to server 127.0.0.1 shell/mongo.js:84
exception: connect failed
The web interface on port 28017 loads fine, as does using the MongoDB shell from a remote Linux host. Can also telnet to localhost:27017, which means no ports are blocked. There is no SELinux running on this machine as well. I have also tried explicitly specifying localhost:2017/db to no avail.
$ ./mongo remote-ip:27017
MongoDB shell version: 2.0.1
connecting to: remote-ip:27017/test
> show dbs
local 0.03125GB
>
Logs are completely mum on the subject:
.....
Mon Oct 31 18:40:34 [initandlisten] fd limit hard:1024 soft:1024 max conn: 819
Mon Oct 31 18:40:34 [initandlisten] waiting for connections on port 27017
Mon Oct 31 18:40:34 BackgroundJob starting: snapshot
Mon Oct 31 18:40:34 BackgroundJob starting: ClientCursorMonitor
Mon Oct 31 18:40:34 BackgroundJob starting: PeriodicTask::Runner
Mon Oct 31 18:40:34 [websvr] fd limit hard:1024 soft:1024 max conn: 819
Mon Oct 31 18:40:34 [websvr] admin web console waiting for connections on port 28017
Stracing the mongo shell client shows only one problematic call:
[pid 31708] connect(4, {sa_family=AF_INET, sin_port=htons(27017), sin_addr=inet_addr("127.0.0.1")}, 16) = -1 EBADF (Bad file descriptor)
Filesystem is clean, no ulimit restrictions (running as root for testing). I can see from the strace that the mongo client is trying to connect via TCP (AF_INET), but since it is local and MongoDB creates a file socket, is there a way to tell the client to connect through that instead? Or better yet, why would the client be throwing a EBADF?
EDIT: My basic Mongo conf:
dbpath=/root/mongodb-linux-i686-2.0.1/data
logpath=/root/mongodb-linux-i686-2.0.1/logs/mongo.log
slowms=15
rest=1
I think there is some default config what is missing in this version of mongoDb client. Try to run:
mongo 127.0.0.1:27017
It's strange, but then I've experienced the issue went away :) (so the simple command 'mongo' w/o any params started to work again for me)
[Ubuntu Linux 11.10 x64 / MongoDB 2.0.1]
You may want to check your config to see if the bind_ip is set
bind_ip: 127.0.0.1
If it is then this permits only local logins. Comment this out and restart mongo, this may help.
I had a similar problem, well actually the same (mongo process is running but can't connect to it).
What I did was went to my database path and removed mongod.lock, and then gave it another try (restarted mongo). After that it worked.
Hope it works for you too.
mongodb repair on ubuntu
I had same problem. In my case MongoDB server wasn't running.
Try to open this in your web browser:
http://localhost:28017
If you can't, this means that you have to start MongoDB server.
Run mongod in another terminal tab.
Then in your main tab run mongo which is is the shell that connects to your MongoDB server.
Delete /var/lib/mongodb/mongod.lock, then issue sudo service mongodb start, then mongo.
Open the file /etc/mongod.conf and add the ip of the machine from where you are connecting, to bind_ip
bind_ip = 127.0.0.1,your Remote Machine Ip Address Here
Ex:-
bind_ip = 127.0.0.1,192.168.1.5
Restart mongodb service:
sudo service mongod restart
Make sure mongodb port is opened in the firewall.
You can also comment the line, if you are not worried about security.
I had this problem as well. Is your MongoDB journaling?
I noticed the following "preallocate" entries in the log file.
Once I saw the last line "waiting for connections on port", I could connect.
Notice that this "faster" mode took 12 minutes to intialize.
William
Tue Apr 17 16:48:01 [initandlisten] MongoDB starting : pid=2248 port=27017 dbpath=E:\MongoData 64-bit host=ME
Tue Apr 17 16:48:01 [initandlisten] db version v2.0.0-rc0, pdfile version 4.5
Tue Apr 17 16:48:01 [initandlisten] git version: 8d4bf50111352cee5a4f1abf25b63442d6c45dc4
Tue Apr 17 16:48:01 [initandlisten] build info: windows (6, 1, 7601, 2, 'Service Pack 1') BOOST_LIB_VERSION=1_42
Tue Apr 17 16:48:01 [initandlisten] options: { bind_ip: "ip", dbpath: "E:\MongoData", directoryperdb: true, journal: true, logpath: "E:\MongoData\mongo.log", quiet: true, rest: true, service: true }
Tue Apr 17 16:48:01 [initandlisten] journal dir=E:/MongoData/journal
Tue Apr 17 16:48:01 [initandlisten] recover : no journal files present, no recovery needed
Tue Apr 17 16:48:02 [initandlisten] preallocateIsFaster=true 9.68
Tue Apr 17 16:48:04 [initandlisten] preallocateIsFaster=true 8.44
Tue Apr 17 16:48:06 [initandlisten] preallocateIsFaster=true 9.68
Tue Apr 17 16:48:06 [initandlisten] preallocateIsFaster check took 4.921 secs
Tue Apr 17 16:48:06 [initandlisten] preallocating a journal file E:/MongoData/journal/prealloc.0
Tue Apr 17 16:52:37 [initandlisten] preallocating a journal file E:/MongoData/journal/prealloc.1
Tue Apr 17 16:56:54 [initandlisten] preallocating a journal file E:/MongoData/journal/prealloc.2
Tue Apr 17 17:01:42 [initandlisten] waiting for connections on port 27017
Tue Apr 17 17:01:42 [websvr] admin web console waiting for connections on port 28017
By default, mongodb is configured to listen only to localhost. Excerpt from mongodb default config file :
# Listen to local interface only. Comment out to listen on all interfaces.
bind_ip=127.0.0.1
One needs to comment the bind_ip to listen from external entities.
You wont be able to add shards unless you start listening on non-local interfaces.
HTH,
Abhay Dandekar
This is actually not an error... What happens here is that Mongo relies on a daemon in order to run the local database server, so in order to "fire up" the mongo server in your shell, you have to start the mongo service first.
For Fedora Linux (wich is the Distro I use) You have to run these commands:
1 sudo service mongod start
2 mongo
And there you have it! the server is going to run. Now, If you want Mongo service
to Start when the system boots then you have to run:
sudo chkconfig --levels 235 mongod on
And that's all! If you do that, now in the shell you just have to type mongo in order
to start the server but that's pretty much it, the problem is you have to start the SERVICE first and then the SERVER :)
P.S. The commands I posted might work on other linux distros as well, not just in fedora... In case not maybe you have to tweak some words depending on the distro you're using ;)
Facing the same issue with the error described by Garrett above.
1. MongoDB Server with journaling enabled is running as seen using ps command
2. Mongo client or Mongoose driver are unable to connect to the database.
Solution :
1. Deleting the Mongo.lock file seems to bring life back to normal on the CentOS server.
2. We are fairly new in running MongoDB in production and have been seeing the same issue cropping up a couple of times a week.
3. We've setup a cron schedule to regularly cleanup the lock file and intimate the admin that an incident has occurred.
Searching for a bug fix to this issue or any other more permanent way to resolve it.
If your bind_ip is set to anything other than 127.0.0.1 then you'll need to add the ip explicitly even from the local machine. So simply use the same method that you're using on the remote box on the local box. At least that's what did it for me.
Not so much an answer but more of an FYI:I've just hit this and found this question as a result of searching. Here is the details of my experience:
Shell error
markdsievers#ip-xx-xx-xx-xx:~$ mongo
MongoDB shell version: 2.0.1
connecting to: test
Wed Dec 21 03:36:13 Socket recv() errno:104 Connection reset by peer 127.0.0.1:27017
Wed Dec 21 03:36:13 SocketException: remote: 127.0.0.1:27017 error: 9001 socket exception [1] server [127.0.0.1:27017]
Wed Dec 21 03:36:13 DBClientCursor::init call() failed
Wed Dec 21 03:36:13 Error: Error during mongo startup. :: caused by :: DBClientBase::findN: transport error: 127.0.0.1 query: { whatsmyuri: 1 } shell/mongo.js:84
exception: connect failed
Mongo logs reveal
Wed Dec 21 03:35:04 [initandlisten] connection accepted from 127.0.0.1:50273 #6612
Wed Dec 21 03:35:04 [initandlisten] connection refused because too many open connections: 819
This perhaps indicates the other answer (JaKi) was experiencing the same thing, where some connections were purged and access made possible again for the shell (other clients)
I don't see this having an accepted answer yet, so I'll just add my 2 cents.
I had the exact same issue just now. After a while I realized I've locked localhost out in my iptables rules. So, check your firewall.
On Ubuntu:
Wed Jan 27 10:21:32 Error: couldn't connect to server 127.0.0.1 shell/mongo.js:84
exception: connect failed
Solution
look for if mongodb is running by following command:
ps -ef | grep mongo
If mongo is not running you get:
vimal 1806 1698 0 10:11 pts/0 00:00:00 grep --color=auto mongo
You are seeing that the mongo daemon is not there.
Then start it through configuration file(with root priev):
root#vimal:/data# mongod --config /etc/mongodb.conf &
[1] 2131
root#vimal:/data# all output going to: /var/log/mongodb/mongodb.log
you can see the other details:
root#vimal:~# more /etc/mongodb.conf
Open a new terminal to see the result of mongod --config /etc/mongodb.conf &
then type mongo. It should be running or grep
root#vimal:/data# ps -ef | grep mongo
root 3153 1 2 11:39 ? 00:00:23 mongod --config /etc/mongodb.conf
root 3772 3489 0 11:55 pts/1 00:00:00 grep --color=auto mongo
NOW
root#vimal:/data# mongo
MongoDB shell version: 2.0.4
connecting to: test
you get the mongoDB shell
This is not the end of story. I will post the repair method so that it starts automatically every time, most development machine shutdowns every day and the VM must have mongo started automatically at next boot.
After starting the mongod
$mongod --dbpath <db name>
to get shell
$mongo --shell
If your mongoDB server(remote server)'s version is greater then 4.0.3, then you will face this issue. Hence you should replace your current mongo-client shell with below mongo :
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 9DA31620334BD75D9DCB49F368818C72E52529D4
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/4.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.0.list
sudo apt-get update
sudo apt-get install -y mongodb-org
Then you mongo client will be able to connect your remove mongodb
If you are getting the following message
start: Rejected send message, 1 matched rules; type="method_call", sender=":1.84" (uid=1000 pid=3215 comm="start mongodb ") interface="com.ubuntu.Upstart0_6.Job" member="Start" error name="(unset)" requested_reply="0" destination="com.ubuntu.Upstart" (uid=0 pid=1 comm="/sbin/init")
shriprasad#shriprasad-HP-430-Notebook-PC:/var/lib/mongodb$ mongo
You must be trying to start the mongodb service as user other than root. You must be root user. Thus log in as root and then run following command as follows:
sudo bash
followed by
service mongodb start

Resources