Python MYSQL will not connect - mysql

I have been helping troubleshoot a process on an old UBUNTU 16 server. We cannot migrate a site to a new server until we understand how it works as we need to recreate and troubleshoot.
Basically its a python3 script that connects to a MYSQL database and reads some data, and updates data under a certain condition. It is basically linked to ZOHO hence the need for this programming language.
The error has been diagonsed as a mysql connection error. Error 111 connection refused. The database is localhost
I have 100% verified the access for the username, password and database visibiity and that port 3306 is open. I can login using a MYSQL command on the CLU and show the database and its tables.
The connector is 100% pulling in the correct variables from the config.py as I have added a print(HOST, DBASE, USER, PASSWORD) just before the connector and verified they are correct.
I have also tried 127.0.0.1 instead of localhost. I have also (dont shoot me) put in the root username and password to remove any permissions issues
I am wondering if its possible to expand on the mysql error in python so I can get a very clear message as to WHAT part is causing an issue. Is there any command in MYSQL that could have been issued to specifically block python.
I have disabled UFW just in case, made no difference, and the general logs for MYSQL dont appear to contain anything useful
Exact error:
mysql.connector.errors.InterfaceError: 2003: Can't connect to MySQL server on '127.0.0.1:3306' (111 Connection refused)
Note: The server is Ubuntu 16, running MYSQL 5.7.26-0ubuntu0.16.04.1 with localhost access only. Python is 2.7.12
As noted this is an old server, that I want to scrap ASAP but until I can prove that the zoho connector is fixable, we cant proceed as its a blocker.

As you can see in theexample below.
the port is separate from the host
import mysql.connector
cnx = mysql.connector.connect(
host='localhost',
database='import_test',
user='testuser',
password='password***',
port=3306
)

Turns out to be the most basic of all issues. On the old server we cloned from, everyting was open, zero lockdown including MYSQL listening to the world. On the new server, mysql had skip-networking was enabled.
Whereas the web instances were happily connecting directly to the socket, mysql was not activly listening on port 3306. I had changed my.cnf to enable networking but a different issue was stopping those changes taking place.
Once I had mysql working on port 3306 and I could see it listening, python now connected ok so some of the answers were correct. MYSQL was 'not working;' in a global sense.. HOWEVER this is a step backwards in terms of security. I did not want MYSQL open to the world
So I modified the code to use the unix socket of /var/run/mysqld/mysqld.sock, renenabled skip-networking and voila.. all is now fine with python, port 3306 is shutdown again and I have my first trial by fire of python complete
My config file defines DATABASE as local host, USER and PASSWORD but now has SOCKET as /var/run/mysqld/mysqld.sock and my code is
import mysql.connector
from models import ExportRecord
from config import HOST, USER, PASSWORD, DATABASE, SOCKET
def connect():
return mysql.connector.connect(host=HOST, user=USER, passwd=PASSWORD, database=DATABASE, unix_socket=SOCKET)

Related

Can't connect to digital ocean LEMP stack MySql via Workbench (or any gui) but CAN connect via terminal mysql cmd

I have tried suggestions in what seems like every post about this topic and none of them have worked for me, so I'm posting my own in case anyone spots anything that I missed.
I'm migrating a wordpress site into a LEMP (ubuntu, apache) stack instance on Digital Ocean, and I need temporary access to the database via MySQL Workbench or something similar on my local machine. I'm planning to turn it off once the migration is done. However I am having a very weird issue I can't figure out the cause for.
If I attempt to sign into the sql instance using
mysql -u user -h my.ip.address -p
I can get access from my local machine just fine.
I can also SSH into the server itself via terminal just fine, I have it set up in my ssh config file using the same ip address and the same user and ssh key i'm attempting to use for my workbench connection.
However doing the same via an SSH connection on workbench I constantly get the error:
Authentication failed, access denied.
Failed to connect to MySql at my.ip.address:3306 through ssh tunnel at user#127.0.0.1 with user mysqluser
Things I've done to attempt to rectify this:
set bind-address in mysql config to 0.0.0.0 on the server and restarted server's mysql
set bind-address in mysql config to my.ip.address on the server and restarted
allowed tcp connection
allowed connection via ufw firewall to 3306 and 33060 ports
created a mysql user with all privileges granted for 'localhost', '%', and '0.0.0.0' hosts
tried sshing in as root user
tried using the root user for the mysql user
I am probably missing some things on that list as I feel like I've tried a million things already!
I even have a different server on a LAMP stack (not LEMP) that I set up a few days ago and was able to gain access to the mysql db on workbench by following the same steps, and even attempted backtracing and copying what I did on the LAMP server, but no go.
I checked to see the netstats of the mysql instance and match the port and the ip and that didn't work, I made sure the port of the SSH was correct, nothing is working.
Does anyone have any tips? I've been using all of the "allow remote mysql connection on digital ocean" articles and questions i can find but nothing is working.
Here is my Workbench connection config at the moment but i have tried what feels like every iteration of it i could think of (re users, passwords, IP addresses, and ports)
SSH Hostname: my.ip.address
SSH Username: user
SSH Password: user-password (also tried empty, since i have a key)
SSH Key File: same key file i use to ssh on terminal
Mysql Hostname: 127.0.0.1 (also tried my.ip.address)
MySql Server Port: 3306 (also tried 33060)
Username: MySqlUser
Password: MySqlUser-Password (works when connecting to mysql via terminal)

Unable to connect to MySQL Server with both the MySQL Command Line Client and mysql connector for python

I recently setup MySQL Server on my computer. But the command line client asks for the password and simply closes without showing anything.
The mysql connector for python can't connect to it either.
import mysql.connector
mydb = mysql.connector.connect(username='username',passwd='password')
Just gives
InterfaceError: 2003: Can't connect to MySQL server on '127.0.0.1:3306' (10061 No connection could be made because the target machine actively refused it)
I saw somewhere that setting the host to localhost might help.
import mysql.connector
mydb = mysql.connector.connect(username='username',passwd='password',host='localhost')
But even then:
InterfaceError: 2003: Can't connect to MySQL server on 'localhost:3306' (10061 No connection could be made because the target machine actively refused it)
The target machine actively refused it?
This already happened once and I reconfigured MySQL Server from the MySQL Community Installer which temporarily got it to work on both the command line client and with the python connector. Is there any way to permanently fix this?
Here are the versions of software I'm using:
MySQL Server 8.0.21
MySQL Server Installer 1.34.4.0
Python 3.8.2
JupyterLab 2.1.2
EDIT: Should've mentioned I'm running Windows 10.
#replace username as user and it should work
import mysql.connector
mydb = mysql.connector.connect(user='username',passwd='password',host='localhost')
If the MySQL80 Command line Client it is closing after you enter the pwd means that you're entering a wrong password. Typically the password that it's asking is the root password.
For such a case check this page:https://dev.mysql.com/doc/refman/8.0/en/mysql-installer-workflow.html#mysql-installer-workflow-server and eventually you may have to reset your root pwd and your user accounts.
With the help of #Erick, I got MySQL working for me. So MySQL is usually installed as a service on windows and is set to manually start every session. This was why reconfiguring worked but only that once. What you need to do is go to services, and look for MySQL80. If it isn't already running (which was what happened to me), double-click it to start, and set 'Start type' to automatic so that the MySQL Server starts automatically during system start-up. For me, doing this made both the client and python able to connect to the server :).

Cannot connect to MySQL server on [remote host] (where remote host is through ngrok)

I am currently trying to work through a guide to build an automated phone system using twilio and integrate it into MySQL database (https://www.twilio.com/blog/connect-local-database-twilio-functions). The issue I am having is that I am unable to connect to the MySQL server after tunneling with ngrok.
Here is the command I am running:
mysql --protocol=tcp --host=0.tcp.ngrok.io --port 18477 --user=root -p
When I press run, the terminal sits in idle for several minutes and then outputs:
ERROR 2003 (HY000): Can't connect to MySQL server on '0.tcp.ngrok.io' (110)
Here is a screenshot of ngrok:
I have followed the steps in https://linuxize.com/post/mysql-remote-access/ to further my knowledge on setting up remote access to mysql. At the bottom of the article, it says that this error typically means 'the port 3306 is not open, or the MySQL server is not listening on the IP address." I then followed the links provided and checked both of those. The second image looked at the listening aspect.
Upon encountering this error, I tried to do some research to see if I can track down the issue. I have zero networking experience, so I do not understand anything beyond client and server basics and such. I came across the mysql documentation (https://dev.mysql.com/doc/refman/8.0/en/can-not-connect-to-server.html).
It says, "if the server was started with skip_networking system variable, it will not accept TCP/IP connections at all." Furthermore, it goes on to say, "If the server was started with bind_address system variable set to 127.0.0.1, it will listen for TCP/IP connections only locally on loopback interface and will not accept remote connections."
I have already made the modifications that I have seen across various documentation and articles. Here are some images to show what I have set-up:
The image below is a screenshot from MySQL Workbench. My bind address is open to all and is not restricted to local connections (bind_address in etc/mysql/mysql.conf.d is commented out). Furthermore, port is set to 3306. Lastly, skip-networking is not checked so TCP/IP can occur.
To confirm that MySQL is listening on port 3306, I ran this lsof command with iTCP:3306. I believe this tells me that mysql is listening on 3306. I am not sure if USER refers to the user mysqld is running on, but if it is, I am not sure what this means in the context of root and other users.
I wish this seemed trivial, but with little networking knowledge, I can easily overlook something simple. Any additional information can be provided.
When you connect to MySql via the hostname set up by ngrok, don't use port 3306. Instead, use the port allocated for you by ngrok. In your example it's 18447.

How can I connect a Jupyter Notebook to a remote MySQL DB using Peewee?

I am trying Peewee to connect and retrieve data from a MySQL remote database, but I get the following error:
InternalError: (1130, "Host 'x.x.x.x' is not allowed to connect to this MariaDB server")
Could you help me?
"retrieve data from a MySQL remote database"
"Host is not allowed to connect to this MariaDB server"
Seem to point on a simple problem:
You're not allowed to connect on the DB from "outside".
By default, MySql / MariaDB are only listening on the "inside" of the server, from MariaDb doc :
MariaDB packages bind MariaDB to 127.0.0.1 (the loopback IP address) by default as a security measure using the bind-address configuration directive.
This mean apart for an application that run on the same machine (accessing 127.0.0.1 or localhost), you'll not be able to connect.
Solutions:
SSH tunnelling
This is probably the safest way to allow a connexion on a remote DB.
SSH is a protocol that allow you to connect to a server. It's mainly used on unix server to manage them, but can do a lot more.
How to use it in your case?
if you can connect with SSH to your DB server, then running this simple command on your notebook the will do the trick:
ssh -L 3306:localhost:3306 user#x.x.x.x
Lets explain a bit: first, your run SSH, then, you tell him to enable a port forwarding from your 3306 port to the localhost:3306 port of the server you connect through user#IP.
With this command running, every query from your local machine:3306 will by send to your MariaDB:3306 server, allowing you to use it as if you where on the server.
Allowing a remote-access user
This one is way more dangerous than the previous one. You'll need to take your time and think about every outcome it mean.
As already said, you're not allowed to connect from outside, ssh let you be "inside", but if you know what you do, you can just remove the security.
The point is:
to make an account that'll be able to login from a remote IP,
allow MariaDB to listen on external requests,
and at least, secure other account to disable remote connection.
[I'm not putting the how-to now, if you really need it, I'll update this answer]

Mysql Workbench error "system error:61"

I have mysql installed several months ago. However I do remember using mysql workbench successfully at one point of time. Today I try launching it and get following error
Lost connection to MySQL server at 'reading initial communication packet', system error: 61
open /etc/mysql/my.cnf in a text editor and try changing:
bind-address = 127.0.0.1
to
#bind-address = 127.0.0.1
and then restart MySQL.
Try using the connection method: Local Socket/Pipe.
If you are trying to use the MySQL Workbench and connect through an SSH tunnel, you will get this error when your SSH connection does not complete successfully (e.g. improper host, password, key file, etc)
A good way to trouble shoot this is to separately test the the ssh connection from the machine you are trying to connect from and establish that you can do so succesfully.
I came across the same problem.I fix this below:
3306 port may be occupied by other process, so mysql change the port to 3307(or nearly other port 3308...).So when you connect,change the port to 3307 and try