Unable to Connect to Containerized MySQL Database container remotely - mysql

Using Docker Desktop, I've started a container of mysql/mysqlserver.
docker run --name=mysqlproject -e MYSQL_ROOT_HOST=% -p 3306:3306 -d mysql/mysql-server
I then went to create some users for my database.
ALTER USER 'root'#'localhost' IDENTIFIED BY 'password';
create user 'dev'#'%' identified by 'password';
grant select on optim.* to 'dev'#'%';
CREATE USER 'root'#'%' IDENTIFIED BY 'root'; GRANT ALL PRIVILEGES ON *.* TO 'root'#'%' WITH GRANT OPTION
When attempting to connect to my MySQL database from localhost, I was successful
sudo mysql -h 127.0.0.1 -P 3306 -p -u dev
I can also access the MySQL database from MySQL Workbench using the IP4 Address of my Wireless LAN adapter.
However, other users on the LAN cannot access my MYSQL database remotely
ERROR 2003 (HY000): Can't connect to MySQL server on <my Wireless LAN adapter IP> (110)
When looking at the firewall settings in place, I noticed that there was a rule in place blocking TCP traffic to 'Docker Desktop Backend'. Without the privilege to change the rule, I tried starting the container from Ubuntu instead, which is NOT integrated with Docker Desktop, at least according to Resources>WSL Integration.
docker start project
The issue persists - users cannot access my containerized MySQL database remotely.

You need to try with interactive mode from any host, but you should have the ssh key authentication to where the MySQL docker container running.
In my case, sometimes the developers want to access the DB from their local, the below commands help to connect to the DB interactively but the Mysql should listen to the socket path.
ssh -t "host-ip" \
docker run --rm -it \
--volume /db:/db:rw \
--entrypoint "/bin/bash" mysql-docker-image \
-lic /usr/bin/mysql -U username --host /mysql-db-socket-path/run/filename.socket

Related

Connecting to MySQL host DB from docker containers in host mode not work

I try connect my mysql host DB from docker container by using host mode and I get the error:
docker run --rm -it --network=host mysql mysql -h 127.0.0.1 -utestuser -p
ERROR 2003 (HY000): Can't connect to MySQL server on '127.0.0.1:3306' (111)
The base of my work is stackoverflow-answer https://stackoverflow.com/a/24326540/1514029
My host mysql version is 8.0.25.
I tried in my.cnf bind-address = 0.0.0.0 and bind-address = 172.17.42.1
Every binding have the same problem.
I grant the testuser user access to 127.0.0.1 by statements
CREATE USER testuser#127.0.0.1 IDENTIFIED BY 'blah'
grant all privileges on *.* to testuser#127.0.0.1 with grant option
On docker bridge mode my connection work fine only on docker host mode it fails!
It must be a network problem!?
you can use:
docker run --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 -d mysql:8.0.25

Unable to connect to Docker Mysql from windows host

I have taken the latest docker image of mysql but I am unable to connect to it from windows host machine.
Executed the following commands:
docker run -p 3306:3306 --hostname=sql --name=mysql_working -d mysql/mysql-server:latest
I can see the IP address with the following command:
docker inspect --format "{{ .NetworkSettings.IPAddress }}" 3ddbeeeb27e9enter
When I do telnet, it is timing out
telnet sql 3306
same for ping
ping <ip address from docker>
Can anyone please advise on whats missing?
You are exposing the port 3306 so the Sql container is available to your host.
If you are on Windows machine type ipconfig
Or for Linux:
ifconfig or ip addr to find your host machine's IP Address and use that IP to connect to Sql.
You can also check docker container logs by docker logs -f container_id here -f is for following the logs.
step1: you need to the changed the default password of MySQL after the first install in docker container
docker logs <container_name or container_id>
docker logs <container_name or container_id> 2>&1 | grep GENERATED
step2:notedown default password
step3:
docker exec -it <container_name or container_id> mysql -uroot -p
Enter default password
ALTER USER 'root'#'localhost' IDENTIFIED BY 'password';
For more info of step1 to step 3 check here
step4:Add new user in mysql as username root and host any with password
create user 'root'#'%' identified by 'password';
step5:Grant all permission to that user
grant all privileges on *.* to 'root'#'%' with grant option;
For more info of step4 to step 5 check here
step 6: Exit from docker container: press ctrl+p+q keys (not plus key combination of ctrl with p and q)
step7: suppose you are on hostmachine then
(else you give ipaddress of hostmachine instead of localhost)
telenet -l root localhost 3306
It asks for password enter password (we given password as password in step4)
press ctrl+] key (not plus key combination of ctrl with ])
Telent connect successfully ..!!

Can't use 127.0.0.1 to connect mysql server docker container, but can use IP?

In my personal PC, there's a docker mysql container binding with port 3306. It works well.
I can connect the mysql server in this container over another PC.
sh$ mysql -hxxx.xxx.xxx.110 -uroot -p
In the host PC, I can connect the container by this way:
sh$ mysql -hxxx.xxx.xxx.110 -uroot -p
But when I try to connect the container to 127.0.0.1, it fails:
sh$ mysql -uroot -p
Known:
In the container, I can connect the mysql server by:
sh$ mysql -uroot -p
there's nothing wrong with the password
Meanwhile, I can connect 127.0.0.1:6376 over the host to the redis container....
I'm only speculating, but the Docker container likely has its own [pseudo] network interface. But you're trying to connect over the host's loopback interface. Those are two separate networks.
Remember, 127.0.0.1 is not just a special IP — it's an IP assigned by a separate network interface.
You can configure Docker to share the host's network stack; perhaps that would be best here.
Please try follow steps:
1.login mysql with ip and use "select host,user,password from mysql.user;" to check 127.0.0.1 exists or not in command result;
2.if not please execute grant command and ensure 127.0.0.1 has privileges,then tray login again.
grant all on *.* to root#'127.0.0.1' identified by 'root';
flush privileges;
You're going to want to check your my.cnf for the bind-address configuration:
#
# File: my.cnf
#
[mysqld]
bind-address = 192.168.1.100
In good practice and depending on your use-case, this should be 127.0.0.1.

Make MYSQL on Linux able to be accessed remotely

I have my database built in MYSQl on Linux.
I want to be able to connect to the server remotely.
General steps for connecting to MySQL remotely:
1.- Create an account for the remote user
GRANT ALL PRIVILEGES ON db_name.* TO 'remoteuser'#'%' IDENTIFIED BY 'secret-passwd'
2.- Open port 3306, this will very depending on your linux distribution, eg: in CentOS 6.5 will be:
iptables -I INPUT -p tcp --dport 3306 -j ACCEPT
3.- Connect remotely, in this example I will use the mysqli client
mysql -h remote.mysqlserver.com -u remoteuser -p
Good luck!

Access mysql remote database from command line

I have a server with Rackspace. I want to access the database from my local machine command line.
I tried like:
mysql -u username -h my.application.com -ppassword
But it gives an error:
ERROR 2003 (HY000):
Can't connect to MySQL server on 'my.application.com' (10061)
What causes this error and how can I connect to the remote database?
To directly login to a remote mysql console, use the below command:
mysql -u {username} -p'{password}' \
-h {remote server ip or name} -P {port} \
-D {DB name}
For example
mysql -u root -p'root' \
-h 127.0.0.1 -P 3306 \
-D local
no space after -p as specified in the Using Options on the Command Line documentation
It will take you to the mysql console directly by switching to the mentioned database.
simply put this on terminal at ubuntu:
mysql -u username -h host -p
Now hit enter
terminal will ask you password, enter the password and you are into database server
edit my.cnf file:
vi /etc/my.cnf:
make sure that:
bind-address=YOUR-SERVER-IP
and if you have the line:
skip-networking
make sure to comment it:
#skip-networking
don't forget to restart:
/etc/init.d/mysqld restart
For Mac, use the following command:
mysql -u app -h hostaddress -P port -D dbname -p
and then enter the password when prompted.
If you want to not use ssh tunnel, in my.cnf or mysqld.cnf you must change 127.0.0.1 with your local ip address (192.168.1.100) in order to have access over the Lan. example bellow:
sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf
Search for bind-address in my.cnf or mysqld.cnf
bind-address = 127.0.0.1
and change 127.0.0.1 to 192.168.1.100 ( local ip address )
bind-address = 192.168.1.100
To apply the change you made, must restart mysql server using next command.
sudo /etc/init.d/mysql restart
Modify user root for lan acces ( run the query's bellow in remote server that you want to have access )
root#192.168.1.100:~$ mysql -u root -p
..
CREATE USER 'root'#'%' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON *.* TO 'root'#'%' WITH GRANT OPTION;
FLUSH PRIVILEGES;
If you want to have access only from specific ip address , change 'root'#'%' to 'root'#'( ip address or hostname)'
CREATE USER 'root'#'192.168.1.100' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON *.* TO 'root'#'192.168.1.100' WITH GRANT OPTION;
FLUSH PRIVILEGES;
Then you can connect:
nobus#xray:~$ mysql -h 192.168.1.100 -u root -p
tested on ubuntu 18.04 server
Try this command mysql -uuser -hhostname -PPORT -ppassword.
I faced a similar situation and later when mysql port for host was entered with the command, it was solved.
try telnet 3306. If it doesn't open connection, either there is a firewall setting or the server isn't listening (or doesn't work).
run netstat -an on server to see if server is up.
It's possible that you don't allow remote connections.
For more details see:
How Do I Enable Remote Access To MySQL Database Server?
I assume you have MySQL installed on your machine. Execute the command below after filling missing details:
mysql -uUSERNAME -pPASSWORD -hHOSTNAME -P3306
mysql servers are usually configured to listen only to localhost (127.0.0.1), where they are used by web applications.
If that is your case but you have SSH access to your server, you can create an ssh tunnel and connect through that.
On your local machine, create the tunnel.
ssh -L 3307:127.0.0.1:3306 -N $user#$remote_host
(this example uses local port 3307, in case you also have mysql running on your local machine and using the standard port 3306)
Now you should be ale to connect with
mysql -u $user -p -h 127.0.0.1 -P 3307
There is simple command.
mysql -h {hostip} -P {port} -u {username} -p {database}
Example
mysql -h 192.16.16.2 -P 45012 -u rockbook -p rockbookdb
you can use the following code to connect to a remote MY SQL database
mysql -u {database_user} -p{db_password} -h {host_name} -P {port_number}
mysql -u admin -p'your_password' -h your-company.aws.com -P 3306
Must check whether incoming access to port 3306 is block or not by the firewall.
this solution worked for me:
On your remote machine (example: 295.13.12.53) has access to your target remote machine (which runs mysql server)
ssh -f -L 295.13.12.53:3306:10.18.81.36:3306 user#295.13.12.53
Explained:
ssh -f -L your_ssh_mashine_ipaddress:your_ssh_mashine_local_port:target_ipaddress:target_port user#your_ip_address -N
your_ssh_mashine_ipaddress - it is not local ip address, it is ip address
that you ssh to, in this example 295.13.12.53
your_ssh_mashine_local_port -this is custom port not 22, in this example it is 3306.
target_ipaddress - ip of the machine that you trying to dump DB.
target_port - 3306 this is real port for MySQL server.
user#your_ip_address - this is ssh credentials for the ssh mashine that you connect
Once all this done then go back to your machine and do this:
mysqldump -h 295.13.12.53 -P 3306 -u username -p db_name > dumped_db.sql
Will ask for password, put your password and you are connected.
Hope this helps.
Try this, Its working:
mysql -h {hostname} -u{username} -p{password} -N -e "{query to execute}"
This one worked for me in mysql 8, replace hostname with your hostname and port_number with your port_number, you can also change your mysql_user if he is not root
mysql --host=host_name --port=port_number -u root -p
Further Information Here
You should put your password with 'p'
mysql -u root -u 1.1.1.1 -p'MyPass'
I was too getting the same error.
But found it useful by creating new mysql user on remote mysql server ans then connect. Run following command on remote server:
CREATE USER 'openvani'#'localhost' IDENTIFIED BY 'some_pass';
GRANT ALL PRIVILEGES ON *.* TO 'openvani'#'localhost WITH GRANT
OPTION;
CREATE USER 'openvani'#'%' IDENTIFIED BY 'some_pass';
GRANT ALL PRIVILEGES ON *.* TO 'openvani'#'%' WITH GRANT OPTION;
FLUSH PRIVILEGES;
Now you can connect with remote mysql with following command.
mysql -u openvani -h 'any ip address'-p
Here is the full post:
http://openvani.com/blog/connect-remotely-mysql-server/
If you are on windows, try Visual Studio Code with MySQL plugins, an easy and integrated way to access MySQL data on a windows machine. And the database tables listed and can execute any custom queries.
If port is default, some version required data base name which you trying to connect.
mysql -u <<your username>> -h <<your host>> <<your db name >> -p
This will prompt password Then type your password. If port is not default 3306
Then:
mysql -u <<your username>> -h <<your host>> -P <<your port>> <<your db name >> -p