Can't connect to MariaDB server remotely using TCP - python

I know this kind of a question has been asked for a thousand times, but I still need to, because I assume that I'm doing everything right here and internet hasn't helped my out.
So I'm trying to remotely connect to my MySQL server (hosted by MariaDB 10.3.29 on Raspbian 10, Raspberry Pi 4). I've been trying to connect by using address 192.168.1.4 on my phone, another computer on another network, by using mysql server command line on a Windows PC and even on iOS using a QueryDB app.ยด
The exact error message I'm getting is:
ERROR 2003 (HY000): Can't connect to MySQL server on '192.168.1.4:3306' (10060)
Config file:
/etc/mysql/mariadb.conf.d/50-server.cnf
has this to be able to connect from any IP:
bind-address = 0.0.0.0
as I think it should.
Also my ufw has mysql allowed, and port 3306 that I'm using, is allowed from anywhere.
I've done this:
GRANT ALL ON *.* TO 'user'#'%' IDENTIFIED BY 'my_pass';
FLUSH PRIVILEGES;
And to be sure I've checked that with
SELECT User, Host FROM mysql.user;
All seems to be OK. I still can't connect remotely. Of course I can connect from localhost, like another computer on my LAN.
I've also opened port 3306 on my ASUS router settings like this:
ASUS router port forwarding
Is that like it should be?
I really can't figure out anything causing this problem. According to information I've gathered, everything should be working. Thanks!

Ok, so after searching for three days I got this.
The problem wasn't with the MySQL but with the Apache. I needed to forward Apache's port 80 to connect to my Public IP address, not local of course.
So it works now!

Related

Postgresql cannot connect to server

The project is a django web app, packed on docker containers. For some reason, I cannot run the server and launch it.
when I run "manage.py runserver", this error occurs:
File "C:\Users\andre\AppData\Local\Programs\Python\Python310\lib\site-packages\psycopg2\__init__.py", line 122, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
django.db.utils.OperationalError: connection to server at "158.160.17.21", port 5432 failed: Connection refused (0x0000274D/10061)
Is the server running on that host and accepting TCP/IP connections?
What do you think can be done with it? Thank you.
The first line of your error tells me that you're using a locally installed copy of psycopg2, a Python client library for PostgreSQL, to connect to the database. This is happening behind the scenes when Django needs to make that connection, but it's interesting that it's installed locally, rather than in a docker container, given you said your Django webapp is running in Docker.
The third line of your error is very simple: it tells you that
the programme is expecting Postgres to be served on a machine with the IP address 158.160.17.21 on port 5432 (the default port for Postgres)
it isn't there
Now, this IP address is not a default and doesn't refer to your own machine, so it must be something you've provided. An IP lookup suggests it's in Venezuela (does that sound right to you?). Perhaps you are expecting Postgres to be served on a third party machine; if so, you'll need to check that you have the right IP address and that Postgres is being served there.
Otherwise, you'll need to reconfigure Django to seek Postgres elsewhere.
Open Task Manager > Services tab > Right-Click on Mongo > Start.
Now go re-run the server again.

How to connect to my PostgreSQL database from RDP?

I have a discord.py bot and I host it on Google Cloud RDP (windows). I was working and testing PostgreSQL database on my local computer and it was working like charm now when I tried to use the same code and same connection method on my RDP, I got an error.
This is how I connect to database on my local pc:
How do I connect it with RDP now? Do I need to make any changes to the database like whitelisting the IP and if so How do I do it?
Thanks
If the database is running on your local computer inside your firewall/router, then it cannot be reached from the Internet. If you control your router, you can try forwarding port 5432 on your public IP to port 5432 on your Windows computer. However, it might be better if you just moved the Postgres instance to your cloud instance.

Configuring Remote MYSQL with a Dynamic IP

I am connecting my python software to a remote msql server.
i have had to add an access host on cPanel just for my computer but the problem is the access host, which is my IP, is dynamic.
How can i connect to the remote server without having to change the access host everytime?
thanks guys, networking is my weakness.
Your best option is probably to find a [dynamic DNS] provider. The idea is to have a client running on your machine which updates a DNS entry on a remote server. Then you can use the hostname provided instead of your IP address in cPanel.
There is a bash script for linux which can be run to check continuously whether the IP has changed and update the providers record if necessary. It is for cPanel and requires editing the code to insert the credentials, which is trivial.
Download from github

Lost connection to MySQL server at 'reading initial communication packet [duplicate]

This question already has answers here:
Lost connection to MySQL server at 'reading initial communication packet', system error: 0
(40 answers)
Closed 9 years ago.
I want to establish a connection between my local machine and MySQL database server via Python.
Can someone tell me how to "bind-address with localhost"?
Can someone tell me how to "bind-address with localhost"?
This is a MySQL configuration directive.
Edit the global my.ini file, in the [mysqld] section:
[mysqld]
# -- various other settings
port = 3306
bind-address = 127.0.0.1
# -- other settings
Save this file, and then restart your server.
Edit
If you want to connect to your local Windows instance of MySQL, simply use 127.0.0.1 as the server's address.
If you want to connect to your remote server, the one running Linux then it is a bit complicated:
First, make sure MySQL is listening on the public IP of the Linux server. Change the line bind-address = and set it to the public IP of your server.
Make sure port 3306 isn't blocked by any firewall.
The user that you use to connect to the server needs to have rights to connect from a remote IP. By default, users are only given rights to connect from localhost - in other words they can only connect if the program is running on the same machine as the server itself.
To grant a user access from a remote IP, run this command from the mysql> shell when logged in with the MySQL root user:
GRANT ALL on somedb.* to someuser#8.8.8.8 identified by 'somepassword';
If you want to grant access to someuser from any remote IP:
GRANT ALL on somedb.* to someuser#% identified by 'somepassword';
After those steps, make sure to restart the MySQL server so it will read the changes in the configuration.

Weird MySQL Python mod_wsgi Can't connect to MySQL server on 'localhost' (49) problem

There have been similar questions on StackOverflow about this, but I haven't found quite the same situation. This is on a OS X Leopard machine using MySQL
Some starting information:
MySQL Server version 5.1.30
Apache/2.2.13 (Unix)
Python 2.5.1
mod_wsgi 3
mysqladmin also has skip-networking listed as OFF
I am able to connect to mysql from the python command line. But when I try to do it through mod_wsgi using code that is copy and pasted or via Django I receive the generic connection refusal
OperationalError: (2003, "Can't connect to MySQL server on 'localhost' (49)")
I've looked at the mysql manual and tried its troubleshooting tips such as
telnet localhost 3306
and I do get a connection.
I am not trying to connect as root, either.
Any ideas on what else I could check?
Thanks in advance!
I came across this error and it was due to an SELinux denial. /usr/bin/httpd didn't have permission to connect to port 3306. I corrected the issue with:
setsebool httpd_can_network_connect_db on
Seems to work great and should be more secure than just disabling SELinux. As Avinash Meetoo points out below, you can use:
setsebool -P httpd_can_network_connect_db
To make the selinux change persist across reboots.
I was getting the exact same error message in Django:
OperationalError: (2003, "Can't connect to MySQL server on 'localhost' (49)")
To fix it, I had to explicitly set the mysql port to 3306 in the django settings file.
Not too much out there on this. Just a random guess but try using:
DATABASE_HOST = 'localhost'
instead of 127.0.0.1
and/or try commenting out in your my.ini:
bind-address 127.0.0.1
worth a shot.
Bit odd that the telnet connection works. Maybe some more ways to trouble shot:
shell> perror 49
OS error code 49: Can't assign requested address
I would check the localhost interface first, check if it has IPv4 address. Far fetched maybe, but I had troubles ones when I didabled IPv6.
shell> ifconfig lo0
Maybe the name resolution doesn't work correctly from within Apache/mod_wsgi/etc..
import python
print socket.gethostbyname('localhost')
print socket.gethostbyaddr('127.0.0.1')
Maybe to get you going (something I contributed to Django) try the UNIX Socket in Django, it works setting the database host to the path (start with forward-slash):
DATABASE_HOST = '/tmp/mysql.sock'
Or where ever your socket file is.
Last, check the MySQL error log if there are any weird messages, like it failing to bind on the IP address or port.
Hope this helps a bit.

Categories