I'm working on detect(ip:port) the login behavior of MySQL on different client, But I only get one client machine to use, what I want is to use the same IP and various(explicitly specific, not random select) PORT to connect the MySQL server, because you can specific a port to bind in your client code.
Is there a way(On command line mysql or MySQL C API) to specific the port number on connecting the MySQL server?
Try This
ssh -i /Users/xxxx/key.pem user#data.server.com -L 53306:localhost:3306 -f sleep 60 >> logfile
mysql -u user -p -h 127.0.0.1 -P 53306
Related
By default, Database Manager from PhpStorm works well. But currently on a special Provider (1u1.de) I have some trouble to got this work.
I can connect to the Provider via SSH. If I want to connect to MySQL database, I have to use:
mysql --host=localhost --user=dbo123123123 -S /tmp/mysql5.sock --password='123123123';
That's works well via CLI on Server, but I didn't find a way to connect via PhpStorm to this Database.
For me it seems that the "socket-connection" may be the Problem. Does anybody have a clue how to got this to work?
Part of the Solution (?!):
Maybe a first part of an solution, I found that you be able to forwarding an Socket to your local pc as own socket this way:
ssh -nNT -L $(pwd)/yourLocal.sock:/var/run/mysqlREMOTEMYSQL.sock user#somehost
Source of Information
This show me, that the Socket is established:
netstat -ln | grep mysql
unix 2 [ ACC ] STREAM LISTENING 3713865 /myFolder/mysql5.sock
But I'm still unable to connect to this Socket with:
mysql -h localhost --protocol=SOCKET -u'username' -p'mypassword' -S /myFolder/mysql5.sock
Got this Error:
ERROR 2013 (HY000): Lost connection to MySQL server at 'reading initial communication packet', system error: 95 "Operation not supported"
ssh -L /tmp/mysql.sock:/var/run/mysqld/mysqld.sock sshuser#remotehost
and then
mysql -h localhost --protocol=SOCKET -u'username' -p'mypassword' -S /tmp/mysql.sock
seems to work fine for me
Use SSH to setup a port forward, this will allow you to connect securely to your database without exposing it to the world.
On ssh, use the -L argument to establish the tunnel.
ssh -L <local_port>:<remote_host>:<remote_port> user#host
This will open <local_port> on your local machine, and then redirect all packets out the other side of the tunnel, destened for the <remote_host>:<remote_port>
In your case, you might want to try something like this:
ssh -L 3306:127.0.0.1:3306 user#mybox.1u1.de
After establishing the tunnel, you will be able to connect to the database through a local port.
From your local machine, not the 1u1 host,
mysql -u <user> -p --host 127.0.0.1 --port 3306
If this works properly, you should be able to configure PhpStorm to use the same address, 127.0.0.1:3306
The SSH tunnel will need to remain open the entire time you need to be connected to the database.
I'm writing setup instructions for an application needing a MySQL database, and I'd like it to be easy even for people that don't have a MySQL installation.
Therefore, I'd like to run the MySQL server in a container:
docker run -p 3306:3306 \
--name mysql \
-e MYSQL_ROOT_PASSWORD=test \
-e MYSQL_DATABASE=my_db \
-d mysql
And to be able to connect to it this way:
mysql -u root -ptest -D my_db
Which doesn't work because the MySQL server only listens locally on the container.
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
So I see three options, none of which fitting my needs:
1) Using the container IP everytime
mysql -u root -ptest -D my_db -h 172.17.0.4
Annoying, the IP will often change and I don't want people to have to update their configuration this much.
2) Changing the configuration of MySQL inside the container
Requires to run a docker exec ... each time the container is run, so it's annoying as well.
3) Making a custom image where the configuration suits my needs
Seems a bit overkill, I'm pretty sure there is a better solution.
Thoughts?
When the mysql client is invoked as you did:
mysql -u root -ptest -D my_db
(i.e. without the hostname) or with localhost as hostname it tries to connect to the local server using Unix pipes. A pipe is a special file type and the client communicate with the server through it.
Your MySQL server is not local, it runs on a separate machine. You cannot connect to it using pipes.
As you already noticed, it works if you use the IP address of the container as argument for the -h command line option of mysql.
You also map the port 3306 of the container to port 3306 of the host machine. This means any TCP connection to port 3306 of the local machine goes through this mapping to the port 3306 of the container, where the MySQL server is listening.
Combining the two above, all you have to do is to put 127.0.0.1 as host name in the mysql command line:
mysql -u root -ptest -D my_db -h 127.0.0.1
I created my container like this:
$ docker run -d -p 33060:3306 myimage
Then I try connect from host to mysql server in container:
$ mysql -uroot -proot -P 33060
I got this error:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
It odd because in Navicat only I changed the port and work fine:
But If I have the IP of the container:
$ docker inspect --format '{{ .NetworkSettings.IPAddress }}' mycontainer
172.17.0.55
Then I can connect to mysql server successfully:
$ mysql -uroot -proot -h 172.17.0.55
But it is a tedious task have to check the ip each time I create a new container to connect to mysql. There any settings I can do to make this task simpler?
This is not a Docker issue. By default the mysql command-line client will connect to a local (Unix) socket instead of a network one, even if you specify -P.
This behavior is described in the documentation:
On Unix, MySQL programs treat the host name localhost specially, in a way that is likely different from what you expect compared to other network-based programs. For connections to localhost, MySQL programs attempt to connect to the local server by using a Unix socket file. This occurs even if a --port or -P option is given
You have to pass the -hlocalhost option, or you can set your connection defaults in /etc/mysql/my.cnf
i got a remote webserver running with a mysql database.
Right now im using SSH to do any serverside management, and i access the MySQL often. I wondered if its possible for me to make a script that would ssh into the server and if run with "-sql" (subject to change) on the command line it would instead go into mysql.
What i made so far:
#!/bin/bash
if [ "$1" == "-l" ]; then
ssh user#192.168.0.101 //local address for privacy;
mysql -u root -p;
else
ssh user#192.168.0.101
fi
This results in an SSH session and when it ends my computer will try and create a mysql connection on the local machine.
#!/bin/bash
if [ "$1" == "-l" ]; then
ssh user#192.168.0.101 'mysql -u root -p';
else
ssh user#192.168.0.101
fi
This results in a password request and then nothing. I'm assuming its because using ssh with a command expects a response and then shuts down the connection.
Is there any way to do this, i realise that it's not of any significant importance, but it is fun to play around with
Thanks in advance
The mysql command only executes interactively if it's input is a terminal. When you run ssh with a command argument, it doesn't normally allocate a pseudo-tty on the server. So mysql just processes its standard input without displaying a prompt.
Use the -t option to force this:
ssh -t user#192.168.0.101 'mysql -u root -p'
One option you might want to consider for solving this kind of access problem is through the use of the tunneling facility in ssh:
ssh -l user -L 33306:192.168.0.101:3306 192.168.0.101
or maybe
ssh -l user -L 33306:127.0.0.1:3306 192.168.0.101
This creates a port on your local machine (33306) which tunnels to the mysql port (3306) on the remote machine.
Then on your local machine you run a local copy of mysql:
mysql --port=33306 -u root -p
which should connect through the tunneled port to your database.
Try like this. Feed mysql password with the command. So you don't have to enter the password.
ssh user#192.168.0.101 'mysql --user="root" --password="password" --database="database" --execute="My Query;"'
Also I suggest you to set keybased ssh authentication. Hence you can also avoid typing ssh password every time.
i have a problem with our new database. The only way to access the database is throw a SSH tunnel. But it doesn't work...
I use following commands:
sshpass -p <PASSWORD> ssh <USER>#<DOMAIN> -p <PORT> -L 3306:localhost:3306 -f -N
I think the ssh tunnel works and is established.
Now I want to connect via Shell-File the database.
deposit=`mysql -h localhost --port=3306 -u <DATABASEUSER> --password=<DATABASEPASSWORD> --skip-column-names -e "<MYSQLSYNTAX>"`
But there is always folowing error:
ERROR 1045 (28000): Access denied for user '<DATABASEUSER>'#'localhost' (using password: YES)
Do you have any ideas or am I doing something wrong?
Thank you very much!
Assuming all your permissions are okay, it may be worth swapping localhost for 127.0.0.1.
As per the MySQL docs: http://dev.mysql.com/doc/refman/5.5/en/connecting.html
On Unix, MySQL programs treat the host name localhost specially, in a
way that is likely different from what you expect compared to other
network-based programs. For connections to localhost, MySQL programs
attempt to connect to the local server by using a Unix socket file.
This occurs even if a --port or -P option is given to specify a port
number. To ensure that the client makes a TCP/IP connection to the
local server, use --host or -h to specify a host name value of
127.0.0.1, or the IP address or name of the local server.
As I understand you are trying to create a tunnel between your computer and a remote computer that's running SQL server. In your ssh command substitute 3306:localhost:3306 with the remote computer IP address. Note this should be its internal local IP address if you are not in the same local network as the remote computer. Check your SQL Database permissions, username and password as well.