I want to create a temporary MySQL database for unit testing in Python. I decided to go with docker as the tests will be run on different machines.
As a first thing I run the container:
client = docker.from_env()
container = client.containers.run("ubuntu/mysql", detach=True, ports={3306:3307},environment={
'MYSQL_USER':'mysqluser',
'MYSQL_PASSWORD':'password',
'MYSQL_DATABASE':'mydb',
})
print('Container Started : {}'.format(container.status))
And then I try to connect using SQLAlchemy:
engine = sa.create_engine(f'mysql+pymysql://mysqluser:password#localhost:3307/mydb')
engine.connect()
which returns:
sqlalchemy.exc.OperationalError: (pymysql.err.OperationalError) (2013, 'Lost connection to MySQL server during query')
EDIT
After putting sleep(10) after container.run, the response is different:
sqlalchemy.exc.OperationalError: (pymysql.err.OperationalError) (2003, "Can't connect to MySQL server on 'localhost' ([Errno 111] Connection refused)")
Do you know what I'm doing wrong?
What I want to do is to connect to MySQL database on port 3307 (as one server already runs on the host). Then I want to create and populate a table.
EDIT2
Switching ports to {3307:3306} returns error:
docker.errors.APIError: 500 Server Error for http+docker://localhost/v1.41/containers/67a04b384bf3d39407881d4dfc73961932a3ce72fe8dc82c3cd6aedd57da2821/start: Internal Server Error ("driver failed programming external connectivity on endpoint hopeful_leakey (97840eb42d2c09b5e9754b634481d8e048c57ba2de8ffc10b34261de2f76600b): Error starting userland proxy: listen tcp4 0.0.0.0:3306: bind: address already in use")
Related
I run the code from https://github.com/aiordache/demos/tree/master/dockercon2020-demo which is from some official docker blog.
I get
mysql.connector.errors.InterfaceError
mysql.connector.errors.InterfaceError: 2003: Can't connect to MySQL server on 'db:3306' (111 Connection refused)
I even tried some solutions like
self.cursor.execute('GRANT ALL PRIVILEGES ON *.* TO 'root'#'%')
or putting in the port number:
self.connection = mysql.connector.connect(
user=user,
password=pf.read(),
host=host,
database=database,
port='3306'
auth_plugin='mysql_native_password'
)
I dunno whats wrong.
I just removed secrets and put in the password in clear text.
My current connection configuration is as follow, this is for redshift db
con=('postgresql://username:password#hostname:port/databasename')
server = SSHTunnelForwarder(
('ssh host', 22),
ssh_username="-",
ssh_password="-",
remote_bind_address=('db host', port)
)
server.start()
local_port = str(server.local_bind_port)
engine = sa.create_engine(con)
######## Reaches here then times out when reading the table
df_read = pd.read_sql_table('tablename',engine)
However, the Redshift database also has an SSH, which might be affecting the connection? It creates the engine but when reading the SQL in pd.read_sql_query I reach this error.
(psycopg2.OperationalError) could not connect to server: Connection timed out (0x0000274C/10060)
Is the server running on host "xxx" (xxx) and accepting
TCP/IP connections on port xxx?
(Background on this error at: http://sqlalche.me/e/e3q8)
You cannot SSH into a Redshift cluster but you can use SSL to secure the connection.
Amazon Redshift security overview
Configuring security options for connections
I am trying to send some commands to a remote Postgres server using SQLAlchemy but each time I receive an error.
Please note that I can connect to the remote Postgres using SSH username and password to login to the server. For that I have used my local terminal, PuTTY and WinSCP so the problem appears to be in the Python code I have written
# create postgres engine to connect to the database
engine = create_engine('postgres://server_username:server_password#server_name:port/database')
with engine.connect() as conn:
ex = conn.execute("SELECT version();")
conn.close() # not needed but keep just in case
print(ex)
Running the code above yields the following error:
sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) SSL SYSCALL error: Connection reset by peer (0x00002746/10054)
expected authentication request from server, but received S
I have also tried adding the SSL verification parameter as follows
create_engine('postgres://server_username:server_password#server_name:port/database?sslmode=verify-full')
which returned the error
sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) root certificate file "C:\Users\aris.pavlides\AppData\Roaming/postgresql/root.crt" does not exist
Either provide the file or change sslmode to disable server certificate verification.
at which point I had nothing to lose so I disabled certificate verification altogether
create_engine('postgres://server_username:server_password#server_name:port/database?sslmode=disable')
which returned the initial error message.
Do you have any ideas on how I can modify the code to make it work?
I am trying to connect to a MySQL db on my ubuntu VPS with python and I am using this code
import pymysql
conn= pymysql.connect(host='remotehost',port='3306',
user='user',password='password',db='db')
a = conn.cursor()
sql = 'SELECT * from `users`;'
a.execute(sql)
countrow = a.execute(sql)
print("Number of rows :",countrow)
data = a.fetchone()
print(data)
When I run the code I get the following error
pymysql.err.OperationalError: (2003, "Can't connect to MySQL server on 'remotehost' ([WinError 10061] No connection could be made because the target machine actively refused it)")
I have checked my MySQL db and the 'user' that I am connecting to the server with has the "Any host" hostname set. I can't seem to fix this issue.
Fixed by commenting out
bind-address = 127.0.0.1 in /etc/mysql/mysql.conf.d/mysqld.cnf
I'm tring to understand what I should do in order to connect to a remote MySQL server using SSH.
from sshtunnel import SSHTunnelForwarder
with SSHTunnelForwarder(
('192.168.0.60', 22),
ssh_password="MySSHPASSWORD",
ssh_username="MUSSHUserName",
remote_bind_address=('192.168.0.30', 3306)) as server:
conn = MySQLdb.connect(user='UserNameOnSQLSERVER', passwd='PasswordOnSQLSERVER', host='192.168.0.60', db='myDB',
port=server.local_bind_port)
I've been able to connect to this server with the above configuration throw MySQL Workbench and Run queries and everything worked great
The problem is that when I'm trying to connect this DB by the above code I keep getting error:
_mysql_exceptions.OperationalError: (2003, "Can't connect to MySQL server on '192.168.0.60' (10060)")
I also tried to change the ip in MySQLdb.connect to be
conn = MySQLdb.connect(user='kiboUser', passwd='Lb24#db30#kL68', host='192.168.0.60', db='dbKiboDashboard',
port=server.local_bind_port)
and I got the following error:
_mysql_exceptions.OperationalError: (2003, "Can't connect to MySQL server on '192.168.0.30' (10060)")
Can please someone help me understand what im doing wrong
Thanks
remote_bind_address is the one you are tunneling into. Make sure to verify the server's TCP port that is listening.
To verify this, you could try the command
netstat
or
netstat -tln
This should work:
with SSHTunnelForwarder(
('ServerIP', sshport), #The server's IP and SSH port
ssh_password="MySSHPASSWORD",
ssh_username="MUSSHUserName",
remote_bind_address=('127.0.0.1', 3306)) as server: #The server's TCP IP and port, you're tunneling
conn = MySQLdb.connect(user='UserNameOnSQLSERVER', passwd='PasswordOnSQLSERVER', host='127.0.0.1',
port=server.local_bind_port) #Your TCP IP and port (your end of the tunnel)