Python Forum
pysql connection to cloud server database times out
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
pysql connection to cloud server database times out
#1
I have only ever used shared web hosting for my little webpages. That makes things easy for people like me who are not so computer-fit.

Now I have rented a cloud server, using Ubuntu 20.04 Server.

I haven't actually transferred my domain name to the new ip yet, this week I need it on the old hosting.

This bit of ssh tunneling allows me to open phpMyAdmin on the new cloud server from home on my laptop.

Quote:# do this on the local machine
ssh -L 9980:localhost:80 -p 22000 -i ~/.ssh/my_cloud_ed25519 pedro@123.456.789.123

Put this:

Quote:http://localhost:9980/phpmyadmin

in the browser on my laptop and I get my login for phpMyAdmin on the cloud server. I can open phpMyAdmin and manipulate tables.

Normally, the function below connects me to my database and gets the information I want, but I on the new cloud server, I just get a timeout error:

Quote:pymysql.err.OperationalError: (2003, "Can't connect to MySQL server on '123.456.789.123' (timed out)")

Is there some setting I need to adjust on the cloud server??
EDIT The port assigned to me by the cloud server company is 22000 Maybe I need to tell pymysql that??
EDIT: I tried setting pymysql to port=22000 but then I get:

Quote:pymysql.err.InternalError: Packet sequence number wrong - got 45 expected 0

Without port set, pymysql will use port=1443 and I get the timeout error.

def mysqlRemoteAttn(clas): 
        # To connect remote MySQL database 
        conn = pymysql.connect( 
            host='123.456.789.123', 
            user='my_sql_user_name',  
            password = 'my_sql_password', 
            db='allstudentsdb', 
            ) 
          
        cur = conn.cursor()
    
        # Select query 
        #cur.execute(f"SELECT studentnr, score FROM allstudentsAnswers{clas} WHERE weeknr = '{weeknr}'")
        #cursor.execute("SELECT spam FROM eggs WHERE lumberjack = ?", (lumberjack,))
        #sql = f"SELECT studentnr, attendance FROM allstudents{clas}"
        # should be SELECT studentnr, has_been_inc got that wrong in Week 2
        #sql = f"SELECT studentnr, has_been_inc FROM allstudents{clas}"
        sql = f"SELECT studentnr, attn_this_week FROM allstudents{clas}"
        cur.execute(sql)
        output = cur.fetchall() 
          
        #for i in output: 
            #print(i) 
          
        # To close the connection 
        conn.close()
        return output

    # get the attendance from the webpage MySQL
    results = mysqlRemoteAttn(clas)
    # put the results in a dictionary
    studentsAttn = {}
Reply
#2
(Oct-10-2021, 02:36 AM)Pedroski55 Wrote: EDIT The port assigned to me by the cloud server company is 22000 Maybe I need to tell pymysql that??

Given your SSH command line above, that looks like the port for the SSH server. What port is the database server running on? If not the default, then you'll need to specify it. Is there some firewall on the server (or in front of it) that's not allowing the connection, perhaps?
Reply
#3
The default port for the mysql server is 1443, according to the pymysql docs.

I opened 1443 in ufw on the server, but I just get a timeout.

Today I will ask the company register the domain name with the ip for the cloud server. Maybe that is the problem.

You gave me the idea to check the port for mysqld on the cloud server.

I checked with netstat -tlnp:

Quote:tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN 2770/mysqld

So I allowed port 3306 in ufw, but I am now getting connection refused.
Reply
#4
1443 is the default port for SQL Server (the Microsoft product).
3306 is the default port for MySQL.

Can you point to the pymysql documentation that references 1443 for mysql server?
ndc85430 likes this post
Reply
#5
Didn't know that! I just saw somewhere that the default port was 1443. Can't find that again right now!

Anyway, I used this query in mysql on the server to check the port:

Quote:mysql> show variables where variable_name in ('hostname','port');
+---------------+------------+
| Variable_name | Value |
+---------------+------------+
| hostname | ebs-105422 |
| port | 3306 |
+---------------+------------+
2 rows in set (0.01 sec)

mysql>

So at least 1 port is 3306

But I am getting:

Quote:pymysql.err.OperationalError: (2003, "Can't connect to MySQL server on '123.456.789.123' ([Errno 111] Connection refused)")

So, there must be some other server setting I am not aware of.

Any clues??

Like I said, this works like a charm on my old shared hosting website.
Reply
#6
Seems like a mysql question or a network question, not a python thing. Connection error is at the network layer. From the same machine, can you do nc 123.456.789.123 3306 < /dev/null?

Some servers default to only allowing connections from localhost, not from other machines until changed, but I have no idea if that's the case here.
ndc85430 likes this post
Reply
#7
This is what I get from nc

Quote:pedro@pedro-HP:~$ nc -v -w 2 123.456.789.123 3306
nc: connect to 123.456.789.123 port 3306 (tcp) failed: Connection refused
pedro@pedro-HP:~$

There must be something I need to set on the server, but what??
Reply
#8
https://superuser.com/questions/702907/m...-localhost
Reply
#9
Connecting to the cloud server via ssh, I ran:

sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf

I changed the line bind-address to 0.0.0.0 (I also tried *)

Quote:pedro@ebs-105422:~$ nc -v -w 2 123.456.789.123 3306
Connection to 123.456.789.123 3306 port [tcp/mysql] succeeded!
pedro@ebs-105422:~$

But I still get :

pymysql.err.OperationalError: (1130, "183.206.16.30' is not allowed to connect to this MySQL server")
Reply
#10
Like bowlofred said, this is not really a Python problem, but without this, pymysql will not work from a remote machine.

Just in case anyone else has this problem, a solution:

This way is probably not so secure, with 3306 open, but a) I only have homework b) you still need the user name and password. There is a more secure way using mysql rsa key encryption I read.

ssh to your cloud server.

First, open port 3306 on the cloud server.

Quote:sudo ufw allow 3306
sudo ufw enable

Check if you like:

Quote:sudo ufw status

When you create a user on mysql as root, it looks something like this, also when you do it via ssh on the server:

Quote:CREATE USER 'peter'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON some_db.* TO 'peter'@'localhost';

If you want remote access, you need to change the user data (or make a new, remote user):

Quote:RENAME USER 'peter'@'localhost' TO 'peter'@'%';
GRANT ALL ON somedb.* TO 'peter'@'%';
FLUSH PRIVILEGES;

% here apparently represents any ip

Then on the also via ssh on the server (Ubuntu uses nano mostly, there are other editors):

Quote:sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf

find the line that has

Quote:bind-address = 127.0.0.1

change this to

Quote:bind-address = 0.0.0.0

ctrl X to quit nano, nano asks if you want to save, press y, then enter to save in the same place you opened /etc/mysql/mysql.conf.d/mysqld.cnf

Then (on the server):

Quote:sudo systemctl restart mysql

After that, my Python data collector using pymysql worked fine from the Idle shell.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Database connection problems [login, PyQt5, PySql] gradlon93 5 620 Dec-15-2023, 05:08 PM
Last Post: deanhystad
  Networking Issues - Python GUI client and server connection always freezes Veritas_Vos_Liberabit24 0 679 Mar-21-2023, 03:18 AM
Last Post: Veritas_Vos_Liberabit24
  Mysql error message: Lost connection to MySQL server during query tomtom 6 15,682 Feb-09-2022, 09:55 AM
Last Post: ibreeden
  Serial connection connection issue Joni_Engr 15 7,833 Aug-30-2021, 04:46 PM
Last Post: deanhystad
  How to take the tar backup files form remote server to local server sivareddy 0 1,871 Jul-14-2021, 01:32 PM
Last Post: sivareddy
  ws server exit after getting 1 connection korenron 3 5,381 Feb-04-2021, 07:49 PM
Last Post: nilamo
  run a health check script on cloud server through paramiko amritjsr 4 3,175 Jul-21-2020, 02:30 AM
Last Post: amritjsr
  Connection timed out error when connecting to SQL server kenwatts275 2 3,275 Jun-02-2020, 07:35 PM
Last Post: bowlofred
  Connection DATABASE to Python MenThoLLt 3 2,376 Jan-17-2020, 10:35 PM
Last Post: DT2000
  Unable to login to remote SQL Server database sipriusPT 1 14,931 Dec-20-2019, 10:16 AM
Last Post: sipriusPT

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020