OperationalError, Django Cannot Connect to MySQL Database - mysql

I am working a Django tutorial and I want to connect my project to a MySQL database. I did everything as told by the djangoproject tutorial to connect to database. When running python manage.py syncdb , however, I got the
following error:
_mysql_exceptions.OperationalError: (2003, "Can't connect to MySQL server on 'localhost' (10061)")
What seems problematic? I have XAMMP downloaded and I have created databases through XAMMP before but I don't know how
that affects the connection with Django.
Thanks.

Actually under the settings.py, I set HOST to "127.0.0.1" and PORT to 3306, filled out the rest of DATABASE
portion and it worked!

If you are using Windows 7, you might run into problems if your MySQL is configured to use named pipes. Set your MySQL config to use TCP instead.
 Once your reconfigured MySQL allows local network connections, your MySQLdb should work over TCP.  
It doesn't seem to be able to use
Windows 7 named pipes, although the "mysql" command line client can.
There may be a bug.  This wouldn't be noticed unless MySQL
was configured without network connections, which is rare.  I set
up MySQL that way on a development machine with a local MySQL instance.
A detailed article here - http://interconnectit.com/764/using-mysql-workbench-with-xampp/ - shows you the screen where you can switch your Connection Method from named pipes (Local Socket/Pipes) to TCP/IP via Xampp.
Alternatively, if using named pipes (windows) or sockets (linux/mac) for your python/django app to connect to your MySQL is important to you and you do not want to change your connection method to TCP/IP, you should try setting the servername to "." in your xampp's my.conf configuration file; instead of "localhost" or "127.0.0.1". Using pipes/sockets bypasses the TCP/IP network protocol, giving slightly better performance.

Related

Unable to connect to MySql db from Airflow

So I have 2 EC2s in a single VPC. One EC2 is running airflow, the other ec2 is running a wordpress db.
I've created a new Airflow job that uses the MySqlHook to connect to the wordpress db.
mysql_hook = MySqlHook(mysql_conn_id='wp')
however, when I run the above code, I get the following response:
_mysql_exceptions.OperationalError: (2003, "Can't connect to MySQL server on '{PRODHOSTURL}.us-east-1.rds.amazonaws.com' (110)")
I've tried making sure there are inbound rules that allow for 3306 port connections on the wordpress db. I've tried adding bind address rules to the .conf file for mysql. I've checked and re-checked the db rules. But nothing is working. Any ideas?
PS The airflow ec2 has many jobs, most of which connect to other dbs in this same VPC...so I don't know why MySql is acting differently. Could I use a different hook?
We found the answer. The security rules were incorrect. To troubleshoot, i went to one machine and ran the command "ping {other machine's ip address}" until I could get through, in both directions. Then re-ran the code, and it worked. Thanks!

OperationalError Lost connection to MySQL server at 'reading initial communication packet' DigitalOcean

Im having trouble connecting to my sql server on digital ocean through my flask webapp. Im using flask-sqlalchemy to bind the mysql database to Flask.
Im able to access the mysql server through the phpmyadmin interface at myipaddress:5000/phpmyadmin
Since Im using Nginx (I bound it to port 80). Bound Apache to Port 5000. So my phpmyadmin interface is accessible at
myipaddress:5000/phpmyadmin
In my flask app, i specify the
SQLALCHEMY_DATABASE_URI ='mysql://root:password#myipaddress:5000/databasename'
when i try to create the tables on my database using the shell with db.create_all() - it just doesnt respond. The cursor blinks forever and then i get the operational error that i quote on the title afte a few minutes
Im able to get the same setup running on my local dev machine. So i know its not a flask configuration problem but just a mysql access issue. I have my webapp up on digitalocean (Not sure if mysql server is behind a firewall or something like that making it inaccessible
On the
/etc/mysql/my.cnf
for the bind-address under mysql_d section, i tried all possible combinations and restarted the mysql server with no success
i tried localhost, 127.0.0.1, 127.0.0.1:5000, myipaddress for the bind-address (Also tried commenting it out) without any results.
Also i tried to get the user, current_user on the table properties from the mysql command line, it's listed as root#localhost for both
From this post:Lost connection to MySQL server at 'reading initial communication packet', system error: 0; i get the idea that its related to firewall but not sure how to rectify this.
Anyidea how can i connect my flask app to the mysql server ? Any help would be much appreciated
I was specifying the mysql address as
mysql://root:password#myipaddress:5000/databasename
But since my flask app and the mysql server are running on the same server, the flask app was be able to access the mysql server when i replaced the myipaddress:5000 with localhost:5000
mysql://root:password#localhost:5000/databasename
The phpmyadmin is just a web interface for your database, if you're looking at connecting your Flask application to your MySql database, you need to point the SQLALCHEMY_DATABASE_URI to the actual MySQL database, rather then the web interface to that database.
Usually MySql runs on port 3306. I believe that SQLAlchemy is clever enough to know that's the default, so your connection string should just be: SQLALCHEMY_DATABASE_URI ='mysql://root:password#localhost/databasename' or if you are running on a different port/external IP address: SQLALCHEMY_DATABASE_URI ='mysql://root:password#myipaddress:4444/databasename'. Remember, if you're connecting to a MySQL database on an external server (not the same one as your Flask app is running on) you will have to change the configuration to allow that kind of access.

MySQL connection works for Workbench but not Tomcat Application using same credentials

I am working on a remote Redhat system over SSH. Tomcat 7 and MySQL 5.5 server are both installed and appear to be running correctly.
I can connect to MySQL from a local install of Workbench over SSH tunneling using localhost and a defined MySQL user. I can run queries and navigate tables without incident.
When I try to connect to the same MySQL Server with a Tomcat application running in the same environment using the MySQL defined user (same credentials) that Workbench is connecting with successfully, it fails.
At this point I don't know the exact error message that gets generated (I am not an admin) so any guidance here is appreciated.
My question is why can I connect with Workbench (which is effectively connecting locally), but not with my Tomcat application, which actually is local to the server?
Might it be a permissions issue with the context Tomcat is running under?
References:
Connection string example (credentials have been altered):
jdbc:mysql://localhost/mysql_database_name?user=ApplicationUsername&password=ApplicationPassword"
Furthermore, I have verified that
mysql-connector-java-5.1.27-bin.jar
is in the WEB-INF folder, and:
Class.forName("com.mysql.jdbc.Driver");
resolves.
This web application works in both dev and test on another server connecting using respective credentials.
Be sure the port number of mysql server exists in connection string. Normally if port is not specified, default value would be used as 3306. Maybe your mysql runs on different port?
jdbc:mysql://[host][,failoverhost...][:port]/[database] »
[?propertyName1][=propertyValue1][&propertyName2][=propertyValue2]...
If the host name is not specified, it defaults to 127.0.0.1. If the port is not specified, it defaults to 3306, the default port number for MySQL servers.

Unable to connect to MYSQL on a remote computer

I am working on windows and having a remote desktop connection of another machine. I am trying to connect to the mysql running on the remote machine through my MySql workbench, installed on my machine.
When I specify the details to connect to mysql i.e.
IP- a.b.c.d
port-3306
username=root
password=
But everytime it shows me an error saying-
Your connection attempt to connect to user='root'failed from your host to server at a.b.c.d:3306
Is it possible that I can connect to mysql on a different machine?
Certainly that is possible, mysql is a network transparent service. However obviously the normal access authorization rules apply.
do you have network access to the mysql port, this might be blocked by a firewall. You can check that by using telnet ip-address-of-server 3306. Does the server answer or do you get a timeout or are blocked? (the answer would look cryptic, soomething like 5.5.33-MariaDB[*[n7p~g!�iXccI$r9``Y{$mysql_native_password or similar )
the mysql server can be configured to not listen to remote connections. This can add security to the setup, but would obviously block any remote connection attempts. You will have to check the configuration files of mysql for that.
mysql itself implements an authorization level. So check if that 'root' user actually is allowed to connect from the outside. Those authorizations are stored in the internal "mysql" database.
Also it is not clear from the question what your network topology is: is the mysql server running on that system you have a "remote connection" to? Is the mysql-workbench running on that remote system or on your local system? This might affect the ip address you have to use inside the workbench.

MySQL does not work on localhost without Local Area Connection

I use WampServer (Apache, PHP, MySQL) and have no problems when some kind of network adapter(wireless or lan) is connected (i.e. Local Area Connection has status connected) even if i am not connected to the internet (for example when i am connected to the router but that is not connected to the internet).
When there is no network connection, I get a php error like MySQL could not connect to 127.0.0.1 on port 3306.
Interestingly, telnet 127.0.0.1 3306 says that it could not connect to the port, even when the server and MySQL are running fine (i.e. when some kind of local area connection is connected).
So I turned off all kinds of firewall (antivirus and Windows) but still no difference in anything. And that is why this issue is quite puzzling.
Things I have already tried (will update this list along the way):
The skip-networking directive in my.ini.
You could modify your MySQL server and client configuration to connect to one another using a named pipe instead of a TCP/IP loopback connection. That way, the current state of the network connection should have less impact.
To do so, start the server with --enable-named-pipe or the corresponding config file setting, and execute the client with --pipe or --protocol=PIPE. Similar configurations should be available for your PHP connector as well. It may depend on which library you use there, and whether or not it will take the mentioned configuration settings from the my.ini file (settings without leading -- there).