Mysql python - randomly (every other attempt) gets access denied - mysql

I can't figure this out and i am not sure how to code for it.
def get_connection():
cnx = MySQLdb.connect(**DB_CONFIG)
print("Connected")
cnx.close()
print("Closed")
12:08 $ python test_mysql.py && python test_mysql.py
Connected
Closed
Traceback (most recent call last):
File "test_mysql.py", line 4, in <module>
get_connection()
File "XX"/mysql/tbred_mysql.py", line 7, in get_connection
cnx = MySQLdb.connect(**DB_CONFIG)
File "XX/lib/python2.7/site-packages/MySQLdb/__init__.py", line 81, in Connect
return Connection(*args, **kwargs)
File "XX/lib/python2.7/site-packages/MySQLdb/connections.py", line 193, in __init__
super(Connection, self).__init__(*args, **kwargs2)
_mysql_exceptions.OperationalError: (1045, "Access denied for user 'XXXX'#'10.0.8.5' (using password: YES)")
I ran them right after each other because it was easier to demonstrate but you can wait 10 or 15 seconds and it will still happen. I works and then it doesn't.
When it fails like this nothing is written to the mysql error log. If i change the user to something that doesn't exist to force this error a record is written to the mysql error log.
EDIT:
I can reproduce problem without python. If i try and remote connect via the mysql command line client in linux i get the same results. Also i have discovered its not random its every other connection regardless of time between attempts. 1st works, 2nd error, 3rd works, 4th errors again it doesn't matter the time between them. Also with these failures they are not recorded to the mysql error log like a normal access denied message is.
Losing my mind!

Repeating this from my comment above.
Setting option skip_name_resolve fixes your issue.
By default, MySQL Server tries to do a reverse DNS lookup of the client IP address to make sure your user is logging in from an authorized hostname. This is a problem if your local DNS server is slow or intermittently flaky. It can slow down or even cause errors for MySQL connections, and it's not clear why.
Using skip_name_resolve tells the server to skip this validation. This should eliminate errors and slow performance due to DNS.
One implication of this is that you cannot use hostnames in GRANT statements. You must identify users' authorized client hosts by IP addresses or wildcards like %.
https://dev.mysql.com/doc/refman/5.7/en/host-cache.html says:
To disable DNS host name lookups, start the server with the --skip-name-resolve option. In this case, the server uses only IP addresses and not host names to match connecting hosts to rows in the MySQL grant tables. Only accounts specified in those tables using IP addresses can be used. (Be sure that an account exists that specifies an IP address or you may not be able to connect.)
Wildcards work too. You can GRANT ALL PRIVILEGES ON *.* TO 'username'#'192.168.%' for example.

Had to use the ip of the server instead of the hostname. Problem was caused by dns resolution issues on the server. The way that it failed still boggles my mind.

Related

MariaDB Refusing LOCALHOST connections

I'm wondering how I can troubleshoot what's happening, there is not enough details to reproduce the problem and find a fix, here it is what I found:
1)The script does many queries every couple of minutes to the localhost MariaDB server
2)The queries are Async, so multiple queries start failing at some point, once these queries fail, I'm unable to access MariaDB command line, with error:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
In some rare cases I'm able to access it(the commandline admin "mariadb"), any query would fail with the same error.
In the mysql logs file I can see the error:
[Warning] Aborted connection x to db: 'Dbname' user: 'useraccessingdb' host: 'localhost' (Got an error reading communication packets)
Following that line there are many more, at the same time, usually having a connection number from 9 to 19 (aborted connection x=9/19).
How can I debug the issue?
What could be the issue?
Thanks for Your time.
The OS is Ubuntu 19
MariaDB is version 10.3.22
It's likely a firewall error.
Try launching gufw and see what rules you have enabled.
Possibly just the default allow outgoing, reject incoming
Even though localhost is the same machine, it sees any traffic trying to access a port as incoming, so you need to specifically allow it for your database port.
You'll have to check the documentation to find out what port that is. I've played around with those before, but I don't remember which the default is, though it may be specific to your purposes.
https://www.linuxquestions.org/questions/ubuntu-63/what-ufw-rule-will-allow-port-80-to-localhost-but-only-from-localhost-4175595450
Alternatively, you can just use a blanket allow all local traffic which should be fine for what you're doing.
Also, doublecheck that your hosts file has localhost defined
https://en.wikipedia.org/wiki/Hosts_%28file%29
I've run into a case where it wasn't.

Mysql remote connections

I've an application on windows forms that needs to connect to a remote server with a static ip, however I already installed mysql on the server, provided the next command
GRANT ALL PRIVILEGES ON *.* TO 'root'#'%' IDENTIFIED BY 'password' WITH GRANT OPTION; FLUSH PRIVILEGES;
and also put an exception in Windows Firewall, but I'm still unable to connect, I don't know if there is something else to do!, I forgot to say it is a Windows Server 2012 R2
This is not the issue with previliges of user. This is about connection rights. LIke mostly on CPanel we have an option of Remote MySQL. Under this option we either have to provide ip address which we want to allow to connect to mysql or wild card mask % to allow any ip to connect to the server. Check with your hosting how it allows you to connnect. yes these previliges are necessary thay you have given. that is ok but check Remote MySQl if you have cPanel.
Thanks
Check your logs and trap your errors.
Confirm your app can reach the sql port and connect. (From the windows machine, can you telnet to the sql port on the sql server?) If not, look at your firewall, and verify your sql instance is bound to a public ip as Bernd suggests.
Once you know the host can reach the sql server, verify your app is actually connecting. Trap errors from connect functions and read them out.
Then verify the credentials are accepted. Again, trap any errors and read them out in your program.
Then, when you're processing a sql statement, trap any errors and read them out to yourself in your program.
(You may find it useful to write a log file from your program containing any errors you detect on any line of code interacting with your database)
Once you've narrowed down where your issue actually is, a solution will probably present itself. Or at least you'll have narrowed it down, and can update us with a specific error message from where the failure has occured.
Good luck!

mysql on ec2 amazon instance attempting to login with incorrect user

I've set up a very basic LAMP setup on an ec2 server, all good it seems to work.
However i've seen for some queries a failure to connect to the mysql server with the following error:
[07-Jul-2013 20:15:41 Australia/Sydney] PHP Warning: mysql_real_escape_string(): Access denied for user 'ec2-user'#'localhost' (using password: NO) in /var/www/html/mycobber/class/sql/SqlQuery.class.php on line 40
[07-Jul-2013 20:15:41 Australia/Sydney] PHP Warning: mysql_real_escape_string(): A link to the server could not be established in /var/www/html/xxx/xxxx/sql/SqlQuery.class.php on line 40
the thing i dont' understand is the fact that in no location in my mysql connection configuration does it specify the user ec2-user. this is the default user when I log onto the unix server. I've set up a separate account to actually run all my processes, i dont' even use the es2-user.
I've looked online and can't see anything to explain this. does anyone have an idea what's going on here. it's not all the time
Your can't call mysql_real_escape_string without having opened a connection with mysql_connect first.
Quoting from the documentation:
A MySQL connection is required before using mysql_real_escape_string() otherwise an error of level E_WARNING is generated, and FALSE is returned.
The error message refers to your default username # localhost because you haven't opened a connection with specific credentials yet - it doesn't know what account details to use.

Connection forcibly closed when copying from MySQL to MongoDB

I've written a script that transforms my data from MySQL to MongoDB. During the handling of a table with 4,000,000 rows I got (when it was almost done):
Traceback (most recent call last):
File "C:\Python32\lib\site-packages\pymongo\connection.py", line 822, in _send_message
sock_info.sock.sendall(data)
socket.error: [Errno 10054] An existing connection was forcibly closed by the remote host
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "kolibri_to_mongo.py", line 94, in <module>
coll.update(..., upsert=True)
File "C:\Python32\lib\site-packages\pymongo\collection.py", line 411, in update
_check_keys, self.__uuid_subtype), safe)
File "C:\Python32\lib\site-packages\pymongo\connection.py", line 837, in _send_message
raise AutoReconnect(str(e))
pymongo.errors.AutoReconnect: [Errno 10054] An existing connection was forcibly closed by the remote host
Exception mysql.connector.errors.InternalError: InternalError() in <bound method SqlConn.__del__ of SQLConn(?)> ignored
Is that a PyMongo Error or an SQL Error? Can I check for any limits (size or timeout) on the MySQL or MongoDB side? Or did just someone kill my query?
EDIT: I've notice that now I cannot connect to the MongoDB anymore with a timeout error :( Are there any limits in MongoDB that need to be changed is it more likely to be another IT/Hardware problem?
That error is coming from MySQL, however, as to why is a bit of an unknown. The error appears to indicate that the remote end, in this instance, MongoDB closed the connection.
I would recommend looking in the pymongo logs to see if there's any further information there.
I would also run tcpdump on the MySQL side to see if the MongoDB server is rejected the connection attempts with a RST or if the SYNs are leaving the MySQL server and simply being ignored (for the latter, you should see the syns spaced out accordingly based on the retransmission timer, e.g. Attempt 1 # 0s, 2 # +3s, 3 # +9s, 4 # +21s).
On the MongoDB server, does netstat -an | grep LIST or sudo lsof -c mongod show that MongoDB is still listening on port 27017 (assuming you haven't changed the default)?
With regard to MongoDB connections errors, the classic case is where ulimit settings are too low and the server runs out of file descriptors. Here are two good links for you to read:
Production Notes
Ulimit
This is a disconnect on the MySQL server - I wonder if you are hitting a query timeout or a deadlock.
Do you have any slow queries or long queries or are you getting any errors in your MySQL logs?

Remote mySQL connection throws "cannot connect to MySQL 4.1+ using the old insecure authentication" error from XAMPP

I'm running a local copy of WordPress on XAMPP/WinXP for development, but would like to maintain a connection to the remote database. I keep getting "Error establishing database connection" no matter what I try.
On the same PC, I can connect to the remote mySQL DB using any number of mySQL clients, and on the mySQL side, the both the user and the database are set to accept incoming requests from any wildcard domain. I can also easily ping the remote database server from my PC (though I don't know how to do it from WITHIN XAMPP).
Is XAMPP its own little universe that can't reach through to the outside world? Or is there something I'm clearly overlooking that's not letting me connect?
Errors
Warning: mysql_connect() [function.mysql-connect]: Premature end of data (mysqlnd_wireprotocol.c:553) in C:\xampp\htdocs\dbtest.php on line 5
Warning: mysql_connect() [function.mysql-connect]: OK packet 1 bytes shorter than expected in C:\xampp\htdocs\dbtest.php on line 5
Warning: mysql_connect() [function.mysql-connect]: mysqlnd cannot connect to MySQL 4.1+ using the old insecure authentication
Edit
Thanks to #Michael for suggesting I just create a simple connection script so I can get better insight into the actual error that's being thrown. This revealed that it had to do with the old_password setting in mySQL. See my Answer below for a full description of how to resolve this issue.
Here's the test script I put inside my xampp\htdocs folder and tested out:
<?php
$mysqli = new mysqli('my.server.address', 'user_name', 'password', 'database_name');
if ($mysqli->connect_error){
die ("Connect error: " . $mysqli->connect_error );
}
I'm not really clear on why this became an issue on my XAMPP installation, since I'm also running PHP 5.3.x on the server's local box and wasn't experiencing those issues there. However, it has to do with my mySQL server running in "old password" encryption mode. Newer versions of PHP won't allow those kinds of connections, so you need to update your mySQL server to use the newer password encryption. Here are the steps, assuming you have control over the mySQL server. If you don't, that falls out of the scope of my knowledge.
locate the configuration file for the mysql server called my.cnf. I found mine at /etc/my.cnf. You can edit it with sudo nano /etc/my.cnf
Look for a line that says old_passwords=1 and change that to old_passwords=0. You have now told the server that the next time it is run, and it is asked to encrypt a password using the PASSWORD() command, it use the new 41-character encryption rather than the 16-character 'old' style encryption
Now you have to restart your mysql server / service. YMMV, but on Fedora that was easily done with sudo service mysqld restart. Check your OS' instructions for restarting the mysql daemon or service
Now we have to actually edit our user table within mysql. So open up an interactive shell to mysql (on the server you can type mysql -uYourRootUsername -pYourRootPassword)
Change to the mysql database. This is the database that holds all the good stuff for server operation and authentication. You must have root access to work with this database. If you get an 'access denied' you're SOL. Sorry. use mysql; will switch to that database
Now we want to update the user that was giving you grief. Ultimately you'll probably want to update all your users, but for now, we're just focusing on the user that threw the error. update user set Password=password('YOUR_PASSWORD') where User='YOUR_USERNAME';
Now you just need to tell mysql to use the new password for authentication when that user attempts to connect. flush privileges;.
You should be good to go!