MySQL remote connection takes more than 3 minutes to display result - mysql

I am trying to connect from a local server to my remote server with mysql command...
Result after long delay:
[root#local ~] mysqlshow -u test -p*** -h XXX.XXX.XXX.XXX
ERROR 2013 (HY000): Lost connection to MySQL server at 'reading authorization packet', system error: 0
(The connection is done through my second remote server without any error or delay)
Update 1
Retry connecting from local server after add connect_timeout=500 to the remote server's /etc/my.cnf:
[root#local ~] time mysqlshow -u test -p*** -h XXX.XXX.XXX.XXX
+---------------------+
| Databases |
+---------------------+
| foo |
| bar |
+---------------------+
real 3m21.174s <======== (3m!)
user 0m0.004s
sys 0m0.015s
Successful connection but with more than 3 minutes delay to display the result!
The interesting thing is that when the connection is established, other mysql requests (e.g. mysql> SELECT) take effect immediately!
Update 2
Same result after add skip-host-cache and skip-name-resolve to remote server's /etc/my.cnf...
Update 3
Same request through my second remote server: (Everything seems good)
[root#remote2 ~] time mysqlshow -u test -p*** -h XXX.XXX.XXX.XXX
+---------------------+
| Databases |
+---------------------+
| foo |
| bar |
+---------------------+
real 0m0.016s <======== (0.016s)
user 0m0.007s
sys 0m0.002s
Update 4
Check MySQL Connection from local server with telnet: (Everything seems good!)
[root#local ~] time echo X | telnet -e X XXX.XXX.XXX.XXX 3306
Telnet escape character is 'X'.
Trying XXX.XXX.XXX.XXX...
Connected to XXX.XXX.XXX.XXX.
Escape character is 'X'.
telnet> Connection closed.
real 0m0.136s <======== (0.136s)
user 0m0.000s
sys 0m0.005s
Update 5
Try to connect through PHP mysqli from local server:
<?php
$servername = "XXX.XXX.XXX.XXX";
$username = "test";
$password = "***";
$dbname = "dbname";
$conn = new mysqli( $servername, $username, $password, $dbname );
if( $conn->connect_error ) {
die($conn->connect_error);
}
?>
Result:
Warning: mysqli::__construct(): (HY000/2002): Permission denied in /var/www/html/test.php on line 7
Permission denied
(The PHP mysqli connection is done through the second remote server without any error or delay)
Update 6
Retry with disabled SELinux on the local server solves the PHP connection error but the connection still takes more than 3 minutes!
When the connection is established, other requests (e.g. SELECT) take effect immediately.
Update 7
After trying to connect from local server, On the server's phpMyAdmin (or mysqladmin proc command), new connection added with unauthenticated user...
Update 8
Connection try through XAMPP on windows (from different PC and network) to the remote server:
Same result (connecting with unauthenticated user and more than 3 minutes delay to display results) ...
I'm about to go crazy!!

Related

Bash script fails to connect remote MySQL while it is possible to access it manually

I need to write a bash script which would connect to MySQL server on remote host with below command:
$MYSQL_HOME/bin/mysql --socket=$MYSQL_UNIX_PORT -u$MYSQL_USER -p$MYSQL_PWD --host $x --connect-timeout=5 -e 'show master status'
The script finds 2 database servers (10.5.5.233 and 10.5.5.234) in the config file and it tries to access to each of these hosts. However, the connection fails with "No route to host":
ERROR 2003 (HY000): Can't connect to MySQL server on '10.5.5.233' (113 "No route to host")
ERROR 2003 (HY000): Can't connect to MySQL server on '10.5.5.234' (113 "No route to host")
On the other hand, the connection to the master database succeeds when I try to connect it manually:
$MYSQL_HOME/bin/mysql --socket=$MYSQL_UNIX_PORT -u$MYSQL_USER -p$MYSQL_PWD --host 10.5.5.233 --connect-timeout=5 -e 'show master status'
+------------------------+----------+-----------------------------------------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------------+----------+-----------------------------------------------+------------------+
| xxx-bin.000006 | 65284117 | xxx,sched,qua,tadb,nodb | |
+------------------------+----------+-----------------------------------------------+------------------+
I could not find any similar post what could cause this behavior.
Could you please help me understand why bash script fails to connect to MySQL on remote host while the same command succeeds when I run it manually?
Thanks.

Losing connection to Cloud SQL while performing a mysqldump

Recently, I've started seeing issues performing a mysqldump on a Cloud SQL instance. It runs for about 20 minutes, then afterwards it fails with the following error:
mysqldump: Error 2013: Lost connection to MySQL server during query when dumping table "table3" at row: 3518748
Then, if I look in the MySQL error log on the CloudSQL console, I see the following:
"2018-06-29T14:46:11.143774Z 4320729 [Note] Aborted connection 4320729 to db: 'bobs_db' user: 'bob' host: '1.2.3.4' (Got timeout writing communication packets)"
Here is the command I run:
mysqldump -h 1.1.1.1 --port=3306 -u bob -pbobs_pwd --net_buffer_length=16m --compatible=ansi --skip-extended-insert --compact --single-transaction --skip-triggers --where="created_at < '2018-06-29 00:00:00' OR updated_at < '2018-06-29 00:00:00'" bobs_db "table1 table2 table3 table4"
Seems everything I read around this issue points to making the net_read_timeout and net_write_timeout bigger but CloudSQL doesn't provide access to those variables from what I can tell.
I'm running with the default database flags except for the following one:
max_allowed_packet=1073741824

MySQL User Permission Errors using Sqitch on Ubtunu

I am running into what I think is a very easy issue to fix, I am just out of possible ideas.
I have a brand new Ubuntu 14.04 x64 server. I just installed MySQL. Not Apache, php or phpMyAdmin, just plain MySQL.
I have run through mysql_secure_installation and created a password for my root user.
I then put my root password in the /etc/mysql/my.cnf file under the [client] section.
I can run mysql -u root and get to the MySQL console just fine.
However, if I run sqitch deploy I get:
Access denied for user 'root'#'localhost' (using password: NO)
Sqitch is pointing to:
[target "database name_v1"]
uri = db:mysql://root#/databasename_v1
[engine "mysql"]
target = db:mysql://root#/databasename_v1
EDIT
It turns out the problem was with Sqitch and my configuration. Sqitch is a Perl application and needed the perl module MySQL Config in order to read the my.cnf file and access the database.
It turns out the problem was with Sqitch and my configuration. Sqitch is a Perl application and needed the perl module MySQL::Config in order to read the my.cnf file and access the database.
If you are using -v to get MySQL version, here's your answer : mysql -V
-v is for verbose output.
See this thread for more details.
"mysql -v" command line error(linux/ubuntu)
There are multiple root users in mysql. They are in the format user#remote.
You can check if the user password is set for all of those by running the query select Host, User, Password from mysql.user where User="root";
The output should show something like:
+--------------+----------+-------------------------------------------+
| Host | User | Password |
+--------------+----------+-------------------------------------------+
| localhost | root | *XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX |
| testvm | root | *XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX |
| 127.0.0.1 | root | *XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX |
If the password is empty, set it via mysqladmin.
From here on, your connection to mysql, whether coming from localhost, or coming from the hostname will be allowed to login.

Play Framework 2.3 - Can't connect to remote database Linux (Play) -> Win 7 (MySQL)

I have a Linux machine (on 10.0.0.10) which I deployed my Play app to using:
activator dist
I have a Windows machine (on 10.0.0.51) running MySQL.
The database has 3 user accounts set up root#localhost, db_user#localhost, db_user#% all users have all permissions (it's just for testing).
I can access the db from the Linux machine using the mysql shell:
[neil#localhost ~]$ mysql -u db_user -p -h 10.0.0.51
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.12 MySQL Community Server (GPL)
Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| test_db |
| mysql |
| performance_schema |
| sakila |
| test |
| world |
+--------------------+
8 rows in set (0.04 sec)
mysql>
Where test_db is the test database I set up on the Windows machine.
However when I try to run the Play application I get the following:
[neil#localhost ~]$ ~/TEST_APP-1.0-SNAPSHOT/bin/test_app
Play server process ID is 5908
[error] c.j.b.h.AbstractConnectionHook - Failed to obtain initial
connection Sleeping for 0ms and trying again. Attempts left: 0.
Exception: java.net.ConnectException: Connection
refused.Message:Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago.
The driver has not received any packets from the server.
Oops, cannot start the server.
Configuration error: Configuration error[Cannot connect to database
[db_user]]
at play.api.Configuration$.play$api$Configuration$$configError
(Configuration.scala:94)
...
Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:
Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago.
The driver has not received any packets from the server.
...
Here is the db config in my application.conf:
db.db_user.driver=com.mysql.jdbc.Driver
db.db_user.url="jdbc:mysql://10.0.0.51:3306/test_db?allowMultiQueries=true"
db.db_user.user=db_user
db.db_user.pass="password"
db.db_user.partitionCount=3
db.db_user.maxConnectionsPerPartition=20
db.db_user.minConnectionsPerPartition=5
db.db_user.acquireIncrement=5
It is worth noting that this configuration works when I am running the app in dev mode on Windows. But I do not know what's wrong on the Linux box and I have run out of things to try. (note I tried this with all firewalls turned off and got the same issue).
UPDATE
Within the play application I use:
public static final String DB_USER = "db_user";
...
JdbcTemplate jt = new JdbcTemplate(DB.getDataSource(DB_USER));
So I shouldn't have to use db.default.etc... in the conf? Is this not correct?
UPDATE 2
I don't know how I missed this but there was something else further down in the stack trace that is indicative of the problem (the stack trace is quite long so I didn't want to list the whole thing, maybe I should have):
Caused by: java.net.ConnectException: Connection refused
So... the server is listening on port 3306, on the windows server running netstat -an yields:
TCP 0.0.0.0:3306 0.0.0.0:0 LISTENING
TCP [::]:3306 [::]:0 LISTENING
The firewall is turned off (I know... just for testing).
And MySQL is accepting connections from the CLI.
I've just had a brain wave though.... The application.conf was generated on windows... I wonder if I need to run dos2unix on the config file which would be why it cannot find "db_user".
UPDATE 3
No :(
You need to use db.default in conf.
from JavaDatabase
I think the correct key to specify the password is not pass, but password:
db.db_user.password="password"
Also you can try to use db.default.* and DB.getDataSource(), but I guess that will not make much difference...

Cannot connect to mysql server with MAMP nor with Community Server

I created a database with MySQL Workbench, and now I need to access it. So I've written a php script to access it:
<?
$db = mysql_connect("127.0.0.1:3306","root", "");
if (!$db){
echo "Could not connect to database";
exit();
}
$db_name = "pfc_db";
if (!mysql_select_db($db_name, $db)){
die ("Could not select database");
}
$sql=mysql_query("select * from CAPAS");
while($row=mysql_fetch_assoc($sql)){
$output[]=$row;
if (isset($output)){
echo "yes";
echo $output[0];
}
else{echo "no";}
}
mysql_close();
?>
I have doubts about MAMP and MySQL Community Server and I need a push on the right path.
I've installed MAMP on my mac and switched the ports to the default mysql ones 3306. I've placed the PHP test script in a folder "Api" I've created under htdocs. So to try it out i type on my browser http://127.0.0.1/api/test.php and the result is "Could not connect to databse". Am I doing it right?
And what about Community Server? Is it better than MAMP? Should it be running for MAMP to work? Can I just use Community Server? If so, where should I place the PHP scripts? Which folder? Because when I try to do the same test with Community Server, instead of showing the error message, the browser starts downloading the php script to my downloads folder. Why happens that?
As you can see I have a bit of a mess in my mind with these server stuff and I need some help to figure it out.
You do not need to install MySQL Community Server. MAMP already comes with MySQL.
You need to ouput more specific errors within your code by using the mysql_error() function.
if (!$db){
echo "Could not connect to database" . mysql_error();
exit();
}
$db_name = "pfc_db";
if (!mysql_select_db($db_name, $db)){
die ("Could not select database" . mysql_error());
}
mysql_error will return the error message(s) from MySQL.
Following the asnwer I was giving to user775263, i get this table of users:
+
--------------------+------+----------+
| Host | User | Password |
+--------------------+------+----------+
| localhost | root | |
| SquirrellJoe.local | root | |
| 127.0.0.1 | root | |
| ::1 | root | |
| localhost | | |
| SquirrellJoe.local | | |
+--------------------+------+----------+
As you can see, any of the users has any password set. But then I found out there's a config.inc.php script under bin/phpmyadmin where there are these sentences:
$cfg['Servers'][$i]['auth_type'] = 'config'; // Authentication method (config, http or cookie based)?
$cfg['Servers'][$i]['user'] = 'root'; // MySQL user
$cfg['Servers'][$i]['password'] = 'root'; // MySQL password (only needed
I tried again to connect with $db = mysql_connect("127.0.0.1:3306","root", "root"); and the connexion was successful. Why the users table in mysql command line doesn't reflect this? Is it normal? Should I change it?
And then I run into another problem. It can't select the database 'pfc_db' I created on MySQL Workbench. I get the error message Unknown database 'pfc_db'. However, when I type mysql> show databases; the mentioned db does appear on the table:
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| pfc_db |
| test |
+--------------------+
So what might be the problem here?
Well, I'm gonna answer myself so that if someone runs into the same problem.
The error of password happened because I had two installations of mysql in the same computer, so that I was checking the passwords in one of them and the browser was looking at the other.
Therefore, if you have problems with password access, check that you don't have two mysql installed and also check the file config.inc.php
I also changed TCP/IP by a socket connexion, which improves the speed if you're using your localhost as server.
This worked for me
sudo mkdir /var/mysql
sudo ln -s /Applications/MAMP/tmp/mysql/mysql.sock /var/mysql/mysql.sock
I ran into this problem and fixed it by using the default TCP/IP port instead of the socket port.
So in my case instead of 3306, I used 8889
$db = mysql_connect("127.0.0.1:8889","root", "");