I need to create a mysql DB for my project. I have a ubuntu server and have followed this guide to install required modules:
https://www.javahelps.com/2018/10/install-mysql-with-phpmyadmin-on-ubuntu.html
After installing everything I can reach the phpmyadmin using the browser 192.168.3.146/phpmyadmin works without any issues:
phpmyadmin home screen
But the problem is when I try to reach the database through the python code, it does not work. My program hangs after mysql.connector.connect and will never print the next print statement.
print("trying MYSQL")
myConnection = mysql.connector.connect(host=192.168.3.146, user='test_user', passwd='test_user123', db='test', autocommit=True)
print("Connection ID:", myConnection)
print(myConnection)
I have previously sucesfully connected to the mysql this way to the mysql server which was created on Raspbian and did not have any issues. It seems that UBUNTU is not as straightforward.
Please can someone suggest me what could be the problem if I can easily reach the database through the web browser but python program fails
UPDATE
Trying out to comment out bind address in the configuration file but still no luck. The configuration file:
enter image description here
I have allowed firewall through port 3306 with the following command:
sudo ufw allow 3306
There is some progress. Now my Python program does not hang on the mysql connection but instead gives me error:
ERROR 1130 (HY000): Host '192.168.3.251' is not allowed to connect to this MySQL server
First of all, I am unsure where does this IP come from 192.168.3.251. The machine am using to connect to the database is configured with static IP and it is 192.168.4.200. I do not know why it says 192.168.3.251
Is the python script running from another machine? If so, maybe the problem is that mysql is listening only in localhost interface. You can change this by editing the file mysqld.cnf located in the /etc/mysql/mysql.conf.d/ folder and comment (with an #) the line that says:
bind-address = 127.0.0.1.
Once you've done this, restart the service this way:
$ sudo systemctl restart mysql.service
And, verify that everything is ok with:
$ systemctl status mysql.service
Related
I im trying to insert data read from a raspberry pi into a database hosted on another raspberry pi, i used mysql as database and my code is writtin in python on the "client" pi, this operation is all done on local network.
i did all the config in order to connect as "RaspberryPi" user that i created and granted all permissions on the specific database and table on ip: 192.168.0.20 which is the client pi, i created and granted that user from root user of mysql which i granted all permission just before in case it needed to.
my mysql server is at 192.168.0.14. when i run my python program it shows this error: Failed to insert record into HumiditySensor table 2003: Can't connect to MySQL server on '192.168.0.14:3306' (111 Connection refused)
The thing is that i used all the correct infos regarding host,database,user,password in my mysql.connector.connect()
I veryfied if the server was using the right port to communicate and it was port 3306 which is what i expected.
I saw online that the problem might be caused by tcp/ip skipping, i looked at my my.cnf file and all i have is:
[client-server]
!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mariadb.conf.d/
The rest is commented.
i couldnt see bind-address nor tcp/ip skipping so i dont believe it's because of an ip binding or wtv
I also looked if my mysql server was running by looking if the mysql.sock file was in /var/run/mysqld folder and it was...
i did this command to see if the grant permission worked on my RaspberrPi user by typing:
SELECT * from information_schema.user_privileges where grantee like "'RaspberryPi'%";
in mysql shell on host raspberry pi and it showed me in the "IS_GRANTABLE" section that everything was at "YES" instead of "NO" which means that this user has all permissions.
I've been trying to solve this for days i really wish someone can help me on this, thank you.
thanks for the answer! But i found it! if anybody encounters the same error i had, to solve that problem, you need to modify the /etc/mysql/mariadb.conf.d/50-server.cnf file since today when you install mysql, it install mariadb instead and i guess the config files are different. then once you get into 50-server.cnf you just need to comment "bind-address = 127.0.0.1" and it will now listen to other IPs requests.
I updated to the latest ubuntu distribution and now I can't access my mysql databases! I used to log in with root without a password, and now when I try to log in I don't have access because I don't know the password.
There are a lot of questions that are similar to this online. I followed a few of them and the furthest I get is this error:
(2002, "Can't connect to local MySQL server through socket
'/var/run/mysqld/mysqld.sock' (2)")
I tried to follow /var/run/mysqld/mysqld.sock and I don't have a mysqld folder.
So I imagine that's a problem. Other questions I've seen on here point to /etc/mysql/...mysql.cnf.
I have that file but the file doesn't contain anything about a socket.
I'm totally at a loss for how I get access to my databases that I've built! Please help! What should I do?
From terminal:
sudo service mysqld start
Error show that the mysql server is not running on your ubuntu machine.
I am not a network/web/internet programmer so please excuse my noobness in this area. I have gotten a website using a free hosting service. They include one MySQL database. Here are the details for the database:
port = 3306
host = "fdb4.biz.nf"
database = "1284899_6067"
user = "1284899_6067"
password = "somepass9351"
I am using MySQLdb module (installed on my CLIENT machine - not server) to connect to this database:
db = MySQLdb.connect(host=host, user=user, passwd=password, db=database,port=port)
But I get the following error:
OperationalError: (2003, "Can't connect to MySQL server on 'fdb4.biz.nf' (10060)
What I have already tried
tried two different databases from different hosts
tried changing the port
tried searching SO for similar answers but all others connect to 'local host'
What I think:
could this be caused by my firewall? I am using my school's internet. I don't think this could be it because I am on CLIENT so if anything it is the SERVER'S firewall.
Two questions
Can MySQLdb be used to connect to a db on a SERVER when it is imported on a CLIENT?
If yes, what am I doing wrong?
Thank you so much for any help, its greatly appreciated! Been stuck the whole day on this.
For security reasons, mysql only listens for connections from localhost. Error code 10060 is basically that: you are not allowed to connect remotely.
Solution: find a my.ini (or my.cnf in linux) and try to find a line:
bind-address = 127.0.0.1
this line says: allow only local connections. So, you should comment-out this line, or set your IP address.
Yes, MySQLdb can connect to remote hosts.
And your usage of the connect method is correct.
You should first check if you can connect to the remote mysql server from your mysql client.
In terminal you can type mysql -h hostname -u username -p databasename
This should prompt you for the password. Enter the password. Can you connect?
If you can't connect, then you have an access problem, and its not a python - mysqldb problem
Either the server is not reachable because it is behind a firewall, in that case your client machine's ip needs to be whitelisted. Check your firewall settings
Or, the mysql server running on the remote machine is configured to accept only local connections. I think this is the default, but I'm not sure. You should ssh into the server remote host where the database server is running, locate the my.cnf file on the server and check the settings. Depending on your mysql version, the configuration would look slightly different.
Or, the user that you're trying to connect as is not associated with the ip that you're trying to connect from. Mysql users have two parts, like this: 'username'#'host'. To enable a user to connect from all ips the user needs to look like this 'user'#'%'.
I hope I've given you enough to try to debug this issue.
I've run into a weird situation while trying to use PostgreSQL and Psycopg2. For some reason, every time I attempt to connect to the postgre database via python, I get the following error:
psycopg2.OperationalError: FATAL: no pg_hba.conf entry for host "127.0.0.1", user "steve", database "steve", SSL on
FATAL: no pg_hba.conf entry for host "127.0.0.1", user "steve", database "steve", SSL off
Naturally, I checked pg_hba.conf to see what the issue was, but everything appeared to be configured correctly as far as I can see:
pg_hba.conf:
# TYPE DATABASE USER CIDR-ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all md5
# IPv4 local connections:
host all all 127.0.0.1/32 md5
# IPv6 local connections:
host all all ::1/128 md5
In addition, I've found that I can connect to the database via psql as I would expect:
$ psql -U steve -h 127.0.0.1
...
steve=>
Anyone have any ideas as to what could be going on here? Thanks in advance!
Typical explanations include:
You are connecting to the wrong server.
Is the DB server running on the same host as Python does?
You got the wrong port.
Check the server log if you see a connection attempt. You have to log connections for that, of course. See the config parameter log_connections.
You did not reload (SIGHUP) the server after changing pg_hba.conf - or reloaded the wrong cluster (if you have multiple DB clusters).
Use pg_ctl or pg_ctlcluser on Debian and derivatives for that.
Or, on modern Linux installations with systemd (incl. Debian & friends), typically:
sudo systemctl reload postgresql
Or, if there are multiple installations, check with:
sudo systemctl status postgres*
And then realod the one you want with something like:
sudo systemctl reload postgresql#14-main
I recently got into this same issue and I found the solution for this problem.
System:
I have an application server (with these packages installed python, django, psycopg2 and postgres client 9.6.1 (postgresql-9.6.1.tar.gz)), for instance ip address 10.0.0.1(a private address).
And AWS postgres RDS server "aws_rds_host_name" or any database IP address.
Error:
django.db.utils.OperationalError: FATAL: no pg_hba.conf entry for host "10.0.0.1", user "your_user", database "your_db", SSL off
Solution:
While installing the postgres client 9.6.1 source package in application server 10.0.0.1, we have to pass an argument "--with-openssl". I suggest to remove the existing the postgres client and install with below steps.
Download the postgres client source package 9.6.1 (postgresql-9.6.1.tar.gz)
Untar the package postgresql-9.6.1.tar.gz.
./configure --prefix="your_preferred_postgres_path_if_needed" --with-openssl (this '--with-openssl' argument is important to get rid of that error)
make
make install
After successful installation, that error didn't occur when we ran the django project with psycopg2.
I hope this solution helps someone.
I first installed Macports so it would be easier. Then installed mysql5,mysql5-server and py26-mysql.
Everything went ok. When I typed: which mysql5 it returns `/opt/local/bin/mysql5
But when i try to enter the server: mysql5, an error is generated:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/opt/local/var/run/mysql5/mysqld.sock' (2)
How do I fix it?
I eventually wish to use mysql as a backend to a django project.
`
You need to start the server. You should be able to run:
sudo /opt/local/share/mysql5/mysql/mysql.server start
You can set it to run on boot with:
sudo launchctl load -w /Library/LaunchDaemons/org.macports.mysql5.plist
There is a useful guide with other details.
Make sure mysqld (mysql daemon) is actually running on your Mac.
Remember: MySQL is a SQLServer, so it needs a daemon, probably listening on some TCP port to process client's requests.
To check if MySQL daemon is running, type into a terminal session:
$ ps aux | grep mysql
And mysql daemon process should be printed.
If it is not running, start it as stated by Mac Ports documentation.
Another solution:
Find out where MySQL is actually using its sock by reviewing your my.cnf file and looking for
socket=<your mysql sock file>
Make sure this where your app, or whatever is using.
You are going to run into this problem when you hook up to Django. MySQLdb expects /tmp/mysql.sock. You need to override this in your settings.py file by setting:
SOCK: '<your actual sock file'>
There is a quick and easy tutorial at https://kidcodely.wordpress.com/2017/10/07/database-setup/
This will take you through the full installation on a mac where you'll learn how to create databases from Terminal.