Article 5QJT5 Access mysql on the cloud server using Python

Access mysql on the cloud server using Python

by
Pedroski
from LinuxQuestions.org on (#5QJT5)
I have a little Python script to collect students' homework and online classwork scores from the table allstudentsAnswers20BE.

Works great on my old shared web-hosting webpage.

On the new Ubuntu 20.04 cloud server, I am getting connection refused.

Quote:
pymysql.err.OperationalError: (2003, "Can't connect to MySQL server on '123.456.789.123' ([Errno 111] Connection refused)")
netstat says mysqld is listening on 33060 and 3306, so I allowed both in the firewall ufw

Quote:
pedro@ebs-105422:~$ sudo netstat -tlnp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.53:53 0.0.0.0:* LISTEN 657/systemd-resolve
tcp 0 0 127.0.0.1:33060 0.0.0.0:* LISTEN 2770/mysqld
tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN 2770/mysqld
tcp 0 0 0.0.0.0:22000 0.0.0.0:* LISTEN 21908/sshd: /usr/sb
tcp6 0 0 :::22000 :::* LISTEN 21908/sshd: /usr/sb
tcp6 0 0 :::80 :::* LISTEN 27719/apache2
There must be something else that needs setting.

Any tips what that might be please, I really need this to work.

Below is from mysql on the server, confirming port 3306

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>


Here is the Python, works fantastic on my old webpage. I run it in bash on my laptop:

Code:def mysqlRemoteAttn(clas):
# To connect remote MySQL database
conn = pymysql.connect(
host='123.456.789.123',
port=3306,
user='my_mysql_user',
password = 'my_mysql_pw',
db='allstudentsdb',
)

cur = conn.cursor()

# Select query
sql = f"SELECT studentnr, attn_this_week FROM allstudents{clas}"
cur.execute(sql)
output = cur.fetchall()

# To close the connection
conn.close()
return output

# get the attendance from the webpage MySQL
results = mysqlRemoteAttn(clas)latest?d=yIl2AUoC8zA latest?i=F4BPYRWGZ_4:AaWfM85mE78:F7zBnMy latest?i=F4BPYRWGZ_4:AaWfM85mE78:V_sGLiP latest?d=qj6IDK7rITs latest?i=F4BPYRWGZ_4:AaWfM85mE78:gIN9vFwF4BPYRWGZ_4
External Content
Source RSS or Atom Feed
Feed Location https://feeds.feedburner.com/linuxquestions/latest
Feed Title LinuxQuestions.org
Feed Link https://www.linuxquestions.org/questions/
Reply 0 comments