Platform: LINUX.
I am a beginner of MongoDB and pymongo. After installing pymongo, here is a simple test I tried on ipython:
import pymongo
client = pymongo.MongoClient();
# Also tried to specify the local host and port number
db = client['myDB']
collections = db['temptables']
collections.insert({'a':'1'})
At this point, it chokes. And in the end, spits out a "Error 111: connection refused" error. So, I tried invoking MongoDB straight from the terminal and I still got the error below [look at the far end]. So, I searched a bit and tried:
removing the lock ( sudo rm /var/lib/mongodb/mongod.lock ). Turns out there was no lock in the first place.
sudo mongod --repair
I even saw a suggestion to comment out the host and port number from the config file. Tried that too, didn't work.
None of the above worked.
This is the error I see when I try to invoke mongodb from command line.
017-08-17T15:25:30.265-0700 W NETWORK [thread1] Failed to connect to 127.0.0.1:27017, in(checking socket for error after poll), reason: Connection refused
2017-08-17T15:25:30.265-0700 E QUERY [thread1] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed :
connect#src/mongo/shell/mongo.js:237:13
#(connect):1:6
exception: connect failed
Help, please.
Your mongo server isn't running.
You can confirm this by executing sudo ps -ef | grep mongod
If you have mongo installed and in your path, you can execute:
cd && mkdir -p ~/temp_mongo_db && mongod --dbpath=./temp_mongo_db
This will launch mongo and place all database files in your home directory under 'temp_mongo_db'.
Finally, in a new terminal window, execute sudo ps -ef | grep mongod again. You'll now see mongod running.
If you want to run mongo in production, you should configure it to be managed by SystemD or some other init system.
Related
I am trying to run a Python script to insert some data into an Oracle table, from a docker image.
I was given following connection string :
jdbc:oracle:thin:#(DESCRIPTION=(CONNECT_TIMEOUT=3)(RETRY_COUNT=2)(FAILOVER=ON)(LOAD_BALANCE=NO) (ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=host_1)(PORT=1521))) (ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=host_2)(PORT=1521))) (CONNECT_DATA=(SERVICE_NAME=service_name)))
I'm trying to connect using cx_Oracle package :
try:
# establish a new connection
with cx_Oracle.connect(self.oracle_user,
self.oracle_pwd,
self.oracle_dsn) as connection:
logger.info('ElasticsearchFinder.oracle_write : connexion established with DB')
with oracle_dsn being the connexion string (minus the jdbc:oracle:thin:# part)
I also tried things like
cx_Oracle.connect(self.oracle_user+'/'+self.oracle_pwd+'#'+self.oracle_dsn)
as seen in some examples, but I always get the following timeout error :
cx_Oracle.DatabaseError: ORA-12170: TNS:Connect timeout occurred
telnet host 1521 works fine, I also tried changing the CONNECT_TIMEOUT value.
I also tried
dsn_tns = cx_Oracle.makedsn(self.oracle_host, self.oracle_port, service_name = self.oracle_service_name)
cx_Oracle.connect(self.oracle_user,self.oracle_pwd,dsn_tns)
as suggested here but then I get
cx_Oracle.DatabaseError: ORA-12514: TNS:listener does not currently know of service requested in connect descriptor
I am running my script from a Docker image build with Dockerfile :
FROM oraclelinux:7.8
RUN yum -y install oracle-release-el7 && \
yum-config-manager --enable ol7_oracle_instantclient && \
yum -y install oracle-instantclient18.3-basic && \
rm -rf /var/cache/yum
COPY ./fetch_session_iptv.py /opt/
COPY ./conf/fetch_session_iptv.conf /opt/conf/
#COPY ./conf/certs/* /opt/conf/certs/
COPY ./logs /opt/logs
RUN yum install -y \
#https://yum.oracle.com/repo/OracleLinux/OL7/developer/x86_64/getPackage/oracle-instantclient18.3-basic-18.3.0.0.0-2.x86_64.rpm \
https://yum.oracle.com/repo/OracleLinux/OL7/developer/x86_64/getPackage/python-cx_Oracle-7.3-1.el7.x86_64.rpm \
https://yum.oracle.com/repo/OracleLinux/OL7/developer/x86_64/getPackage/python36-pytz-2016.10-2.0.1.el7.noarch.rpm
COPY ./python_requirements/elasticsearch-7.8.0-py2.py3-none-any.whl .
COPY ./python_requirements/certifi-2020.4.5.2-py2.py3-none-any.whl .
COPY ./python_requirements/urllib3-1.25.9-py2.py3-none-any.whl .
RUN pip3 install --user \
certifi-2020.4.5.2-py2.py3-none-any.whl \
urllib3-1.25.9-py2.py3-none-any.whl \
elasticsearch-7.8.0-py2.py3-none-any.whl \
cx_Oracle
RUN sh -c "echo /usr/lib/oracle/18.3/client64/lib > /etc/ld.so.conf.d/oracle-instantclient.conf"
RUN ldconfig
RUN export ORACLE_HOME=/usr/lib/oracle/18.3/client64/
RUN yum -y install telnet
#CMD ["/bin/bash"]
CMD [ "python3", "/opt/fetch_session_iptv.py" ]
I can't understand what's going wrong?
[UPDATE] I've set
ORACLE_HOME=/usr/lib/oracle/18.3/client64/bin
TNS_ADMIN=$ORACLE_HOME/admin
LD_LIBRARY_PATH=/usr/lib/oracle/18.3/client64/lib
PATH=$PATH:$ORACLE_HOME
I've added a tnsnames.ora file in TNS_ADMIN directory, with
CNX=(DESCRIPTION=(CONNECT_TIMEOUT=3)(RETRY_COUNT=2)(FAILOVER=ON)(LOAD_BALANCE=NO) (ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=host_1)(PORT=1521))) (ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=host_2)(PORT=1521))) (CONNECT_DATA=(SERVICE_NAME=service_name)))
Now, when I try sqlplus MY_USER#CNX I get the same :
ORA-12170: TNS:Connect timeout occured
but at least it seems to accept the connexion string and user. Prior to setting all the environment variables correctly, I only got different TNS error messages about TNS: listener does not currently know of service requested in connect descriptor or the net service name being incorrect.
[UPDATE 2]
I've checked with the person who gave me all connexion info: user, password and connection string are correct. They're running Oracle Database 12c (12.1.0.2.0), which according to this page is compatible with the 18c client I'm using.
I don't know what else could be a possible reason for this timeout error?
Start by using the equivalent connection string in cx_Oracle:
self.oracle_dsn = "(DESCRIPTION=(CONNECT_TIMEOUT=3)(RETRY_COUNT=2)(FAILOVER=ON)(LOAD_BALANCE=NO) (ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=host_1)(PORT=1521))) (ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=host_2)(PORT=1521))) (CONNECT_DATA=(SERVICE_NAME=service_name)))"
try:
# establish a new connection
with cx_Oracle.connect(self.oracle_user,
self.oracle_pwd,
self.oracle_dsn) as connection:
logger.info('ElasticsearchFinder.oracle_write : connexion established with DB')
The cx_Oracle manual on connecting and connect strings is here.
Personally I'd use 19c Instant Client, which will connect to the same DB versions as 18c - and doesn't need to have ldconfig run. See Docker for Oracle Database Applications in Node.js and Python.
Update your question with information and I can update this answer likewise.
I got this error message sublime issue(My OS: Ubuntu 16.04) "socket.error: [Errno 98] Address already in use" If I run flask in sublime text or PyCharm. But if I run flask on my Ubuntu terminal,it is running. I understood that port used another service. Then i was trying to solve this issue from google/stackoverflow.
# ps ax | grep 5000 // or # ps ax | grep name_of_service
# kill 3750 // or # killall name_of_service
But nothing changed. Only i found this problem when i was trying to run on sublime or pycharm IDE.
Simple way is to use fuser.
fuser <yourport>/tcp #this will fetch the process/service
Replace <yourport> with the port you want to use
#to kill the process using <yourport> add `-k` argument
fuser <yourport>/tcp -k
In your case
fuser 5000/tcp -k
Now you can run flask with that port.
Pycharm allows you to edit the run configuration, so enter the configuration and check the box (top-right corner) saying: "singleton instance". In this way, every time you restart the server, the previous connection on port 5000 is closed and opened again.
I am trying to connect to GitLab production (installed with omnibus package) postgresql database with psycopg2.
My configuration is like below:
onn = psycopg2.connect(database="gitlabhq_production", user="gitlab-psql", host="/var/opt/gitlab/postgresql", port="5432")
It gives the following error:
FATAL: Peer authentication failed for user "gitlab-psql"
I can connect to the postgresql server on command line with:
sudo -u gitlab-psql -i bash /opt/gitlab/embedded/bin/psql --port 5432 -h /var/opt/gitlab/postgresql -d gitlabhq_production
Does anyone know what will be the correct parameters to pass into?
Peer authentication works by checking the user the process is running as. In your command line example you switch to gitlab-psql using sudo.
There are two ways to fix this:
Assign a password to the gitlab-psql postgres user (not the system user!) and use that to connect via python. Setting the password is just another query you need to run as a superuser like so:
sudo -u postgres psql -c "ALTER USER gitlab-psql WITH PASSWORD 'ReplaceThisWithYourLongAndSecurePassword';"
Run your python script as gitlab-psql like so:
sudo -u gitlab-psql python /path/to/your/script.py
I just wanted to install a PostgreSQL Database. After 3 hours of trying I do not know what else to do. My last try included installing PostgreSQL via Homebrew -> Works perfectly fine.
But typing this:
which psql
I got this: /usr/local/bin/psql
From my view this sort of Path is wrong, a I saw a different one in most tutorials. But I have no idea what to do.
But I went on trying:
createuser -U postgres yrkIO -P
And the terminal asked me for a password only to give me this:
createuser: could not connect to database postgres: FATAL: role "postgres" does not exist
What can I do, I just want to run a PostgreSQL on my Python Flask App?
Have you tried it without forcing a password?
createuser -s -r postgres
This worked for me.
Also, remember to start a server
Start:
pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
Stop server:
pg_ctl -D /usr/local/var/postgres stop -s -m fast
I have a mongodb 2.2.2 setup on ubuntu 12.04 machine and I need to modify binding_ip list while database is running, without mongo restart. Is there a way to do so?
Is it possible to do from pymongo?
p.s. I've actually tried
mongod --config /etc/mongodb.conf --bind_ip 127.0.0.1 31.**
with bind_ip list supplied but it says
Wed Dec 19 17:02:05 [initandlisten] exception in initAndListen: 10309 Unable to create/open lock file: /var/lib/mongodb/mongod.lock errno:13 Permission denied Is a mongod instance already running?, terminating
and I'm not sure if it is not just restarting database.
Apparently you can do with iptables(8) rules. Then try (with 192.0.2.1 being the IP address you want to receive connections on):
iptables -A INPUT -p tcp -d '!' 192.0.2.1 -p 27017 --m state --state NEW -j REJECT
If you already have iptables rules then you may need a different command. Check the output of iptables -L INPUT.