Used this link to try to connect to a remote hive. Below is the code used. The error msg received is also given below
How to Access Hive via Python?
Code
from pyhive import hive
conn = hive.Connection(host="10.111.22.11", port=10000, username="user1" ,database="default")
Error msg
Could not connect to any of [('10.111.22.11', 10000)]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/opt/anaconda3/lib/python3.6/site-packages/pyhive/hive.py", line 131, in __init__
self._transport.open()
File "/opt/anaconda3/lib/python3.6/site-packages/thrift_sasl/__init__.py", line 61, in open
self._trans.open()
File "/opt/anaconda3/lib/python3.6/site-packages/thrift/transport/TSocket.py",line 113, in open
raise TTransportException(TTransportException.NOT_OPEN, msg)
thrift.transport.TTransport.TTransportException: Could not connect to any of [('10.111.22.11', 10000)]
What are the other requirements for successful connection? I am able to connect to the server directly (using putty) and run hive. But when tried from another server X i get this error. Also i can ping the hive server from server X.
Could the port number be the problem? How do i check the correct port number?
As discussed in the below answer i tried to start hiveserver2 . But the command doesnt seem to work. Any help is really appreciated.
Also the port i see in the log when i execute a query from hive shell is 8088. wonder if this should be the port instead of 10000(both did not work anyway)
Could not make it work using pyhive. Had to use paramiko insted
below is the sample code
import os
import paramiko
import time
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.load_host_keys(os.path.expanduser(os.path.join("~", ".ssh", "known_hosts")))
ssh.connect('1.1.1.1', username='uid', password='pwd')
sshin, sshout, ssherr= ssh.exec_command('hive -e "create table test(key varchar(10),keyval varchar(200))"')
HiveServer2 process must be started in your remote Hive host. 10000 is the default port number.
Use this command to start HiveServer2.
$HIVE_HOME/bin/hiveserver2
For PyHive to work, Hive Server 2.0 should be running using transport mode as “binary”. If it's running with transport as Http change it to "binary".
Please try below code to access remote hive table using pyhive:
from pyhive import hive
import pandas as pd
#Create Hive connection
conn = hive.Connection(host="10.111.22.11", port=10000, username="user1")
# Read Hive table and Create pandas dataframe
df = pd.read_sql("SELECT * FROM db_Name.table_Name limit 10", conn)
print(df.head())
Related
How do I port forward to an rds instance from a bastion host to a rds sql server instance?
Typically, I port forward from a bastion host like so: ssh -L 20004:targeted-mssql-rds.aws.com:1433 knn0926#my_bastion_host.com and that will allow me to call localhost:20004 for my db connection. However, that doesn't seem to work or to be the correct process for connecting to a db through port forwarding out of a python script. Every time I try to connect I get the following error: pyodbc.OperationalError: ('HYT00, [HYT00] [Mircosoft][ODBC Driver 17 for SQL Server]Login timeout expired(0) (SqlDriverConnect)). I'm also on a mac so I'm not sure if that makes a difference compared to windows.
I think my code is pretty straightforward for connecting:
import pyodbc
import pandas as pd
import io
import os
#listing drivers
for driver in pyodbc.drivers():
print(driver)
#Make db connection
sql_connect = pyodbc.connect("Driver = {ODBC Driver for SQL Server}; SERVER=localhost,20004; database=simpsons;uid=my_user;PWD=my_pwd, autocommit=True")
cursor.execute('''select * from spp.transactions where acx_num > 1000''')
Am I supposed to be connecting to my bastion and mssql instance in the python code itself? I'm confused as to why its failing and how to properly connect it to my code.
I'm new to using postgres as well as ssh and am having some trouble understanding what I need to do to get remote clients accessing a postgres server. Right now I've got one computer with a server running that I can access using psycopg2 but now I want to query the server using another computer. I've looked around and found examples using sshtunneler, but I feel like I'm missing some puzzle pieces.
import psycopg2
from sshtunnel import SSHTunnelForwarder
import time
with SSHTunnelForwarder(
('192.168.1.121', 22),
ssh_password="????",
ssh_username="????",
remote_bind_address=('127.0.0.1', 5432)) as server:
conn = psycopg2.connect(database="networkdb",port=server.local_bind_port)
curs = conn.cursor()
sql = "select * from Cars"
curs.execute(sql)
rows = curs.fetchall()
print(rows)
My first confusion is I'm not sure what username/password should be. I downloaded putty and put the remote address info in the tunnel section using this tutorial but I have no idea if that's doing anything. When I try to start the server I get the error
2017-03-03 10:03:28,742| ERROR | Could not connect to gateway 192.168.1.121:22 : 10060
Any sort of help/explanation of what I need to do would be appreciated.
If I can do it without ssh then that would be better. Currently running this:
psycopg2.connect(dbname='networkinfodb', user='postgres', host='168.192.1.121', password='postgres', port=5432)
outputs...
OperationalError Traceback (most recent call last)
in ()
----> 1 psycopg2.connect(dbname='networkinfodb', user='postgres', host='168.192.1.121', password='postgres', port=5432)
OperationalError: could not connect to server: Connection timed out (0x0000274C/10060)
Is the server running on host "168.192.1.121" and accepting
TCP/IP connections on port 5432?
and I'm not sure where to go to figure out what the issue is.
So I didn't use ssh tunneling. That was only a backup as I was having trouble connecting to the database using psycopg2. I found that the firewall was blocking the port from being accessed externally so I was able to change that and now I can access the database from clients.
I am having some issues connecting to my hosted mysql database using python. My code is as follows:
import MySQLdb
db = MySQLdb.connect(host="xxxxxx.db.1and1.com", user="xxxxxxx",passwd="xxxxxxx", db="xxxxxxx")
I get the following error:
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
db = MySQLdb.connect(host="db518597727.db.1and1.com", user="dbo518597727", passwd="OilyOily123", db="db518597727")
File "C:\Python27\lib\site-packages\MySQLdb\__init__.py", line 81, in Connect
return Connection(*args, **kwargs)
File "C:\Python27\lib\site-packages\MySQLdb\connections.py", line 193, in __init__
super(Connection, self).__init__(*args, **kwargs2)
OperationalError: (2003, "Can't connect to MySQL server on 'xxxxxxx.db.1and1.com' (10060)")
My sql database is hosted by 1and1. I am sure I have the right credentials, as when I read from this database on a php website, using the same credentials (I have triple checked) it works fine.
I have tried this both on a Raspberry Pi (where I intend to use it) and on my Windows 8.1 PC, so I am pretty sure that it is not the computer that is the problem.
Any help would be much appreciated.Many thanks.
In MySQL credentials are per client, so we distinguish a couple of client types:
- localhost means the same machine as MySQL server connecting via Unix Socket
- 127.0.0.1 means the same machine as MySQL server connecting via TCP/IP
- A.B.C.D where letters are replaced by numbers in range 0-255 which means IP address of client machine connecting via TCP/IP
- * is a wildcard which means that any client can connect with given credentials via TCP/IP
Every entry in MySQL users table consists of client specification (described above), username, password and some other columns (not relevant here).
So the problem you are facing is that PHP script on 1and1 server can connect to the database hosted on 1and1 since the hosting company sets up their database server to accept connections from their own servers with valid credentials.
But the same credentials are considered invalid for connections coming from client machines unknown to 1and1.
What you can do is to ask 1and1 to give your specific IP access rights to your database.
What you can't to is to overcome this problem on your own.
I am trying to access an SQL database that reside on my web server from a Raspberry Pi - using python.
I tried using the following code:
#!/usr/bin/env python
import MySQLdb as mdb
conn = mdb.connect(host="abc.co.uk.mysql"
user="USER NAME"
passwd="PASSWORD"
db="NAME OF DATABASE")
cur = conn.cursor()
cur.execute("SELECT * FROM TABLE_NAME")
for row in cur.fetchall():
print row[0]
I receive the following error when executing!
Traceback (most recent call last):
File "getDatafromSQL", line 7, in <module>
db='alisoliman_co_u')
File "/usr/lib/python2.7/dist-packages/MySQLdb/__init__.py", line 81, in Connect
return Connection(*args, **kwargs)
File "/usr/lib/python2.7/dist-packages/MySQLdb/connections.py", line 187, in __init__
super(Connection, self).__init__(*args, **kwargs2)
_mysql_exceptions.OperationalError: (2003, "Can't connect to MySQL server on 'alisoliman.co.uk.mysql' (111)")
Any idea why could that be? Is there any ways that I can connect to a MySQL database on a server?
I'm sure that you made the right indentation for the print statement under the for loop (the last line of your code).
I don't know which distro are you running on your Raspberry PI, but I've tried your code on my RPI (Arch distro), doing a query on a simple table on the mysql server running in the RPI, and it works well. I've did some changes but I don't believe that these changes are relevant for the right flow of the query.
#!/usr/bin/env python
import MySQLdb as mdb
conn_1=mdb.Connection(db='NAME OF DATABASE', host='localhost', user='USER NAME', passwd='PASSWORD')
cur = conn_1.cursor()
cur.execute("SELECT * FROM rkn_env")
for row in cur.fetchall():
print row[0]
FYI, you can capture the traffic between RPI and the host where the MySQL server is running using tcpdump. This tool has saved me many times...
Lastly, did you check if your MySQL daemon is running and listening (e.g. using netstat -anp) ?
I'm attempting to use mysql after only having worked with sqlite in the past.
I've installed XAMPP on Linux (ubuntu) and have mysql up and running fine (seems like that with phpMyadmin at least). However, I'm having trouble getting the MySQLdb (the python lib) working {installed this using apt}.
to be exact:
>>> import MySQLdb
>>> db = MySQLdb.connect(host="localhost",db="opfine")
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "/usr/lib/pymodules/python2.6/MySQLdb/__init__.py", line 81, in Connect
return Connection(*args, **kwargs)
File "/usr/lib/pymodules/python2.6/MySQLdb/connections.py", line 170, in __init_
...
super(Connection, self).__init__(*args, **kwargs2)
OperationalError: (2002, "Can't connect to local MySQL server through
socket '/var
/run/mysqld/mysqld.sock' (2)")
I'm guessing
Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock
means its expecting some sort of local installation (i.e. not within XAMPP), but I can't figure out how to go about modding this to get it to work with the XAMMP flavor of mysql.
Help is much appreciated!
For the record (and thanks to a pointer from Igancio), I found that the below works (terrible I didn't think of this before):
db=MySQLdb.connect(
user="root"
,passwd=""
,db="my_db"
,unix_socket="/opt/lampp/var/mysql/mysql.sock")
It means that you didn't start the MySQL server, or it's configured to not use a domain socket.
Have the same issue using and look for your SQL configuration file my.cnf.
# The following options will be passed to all MySQL clients
[client]
#password = your_password
port = 3306
socket = /Applications/XAMPP/xamppfiles/var/mysql/mysql.sock
and use socket as parameter:
mysql://read:read#localhost/phonehome?unix_socket=/Applications/XAMPP/xamppfiles/var/mysql/mysql.sock
In my case:
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://read:read#localhost/phonehome?unix_socket=/Applications/XAMPP/xamppfiles/var/mysql/mysql.sock'