I have two environments: my local machine Mac and linux on VPS. The problem is that I can not connect using localhost to my db from VPS.
On my local machine everything works fine.
Local:
mysql -u root mydb -h 127.0.0.1 --password=password --port=2345 //OK
mysql -u root mydb -h 0.0.0.0 --password=password --port=2345 //OK
mysql -u root mydb -h localhost --password=password --port=2345 //OK
Even using public ip of VPS I'm able to connect to remote db from local machine.
VPS:
mysql -u root mydb -h 127.0.0.1 --password=password --port=2345 //OK
mysql -u root mydb -h 0.0.0.0 --password=password --port=2345 //OK
mysql -u root mydb -h localhost --password=password --port=2345 //KO <-
Error:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
I used the following docker file to create my image.
DockerFile:
FROM mysql:8.0
COPY *.sql /docker-entrypoint-initdb.d/
docker-compose.yaml:
version: '3.7'
services:
ls:
image: ***/db-container:v6 # <-my image
container_name: db-container
restart: always
environment:
MYSQL_DATABASE: 'mydb'
MYSQL_USER: 'user'
MYSQL_PASSWORD: 'password'
MYSQL_ROOT_PASSWORD: 'password'
ports:
# <Port exposed> : < MySQL Port running inside container>
- '2345:3306'
expose:
# Opens port 3306 on the container
- '3306'
volumes:
- db-container:/var/lib/mysql
volumes:
db-container:
Query inside container:
mysql> SELECT host, user FROM mysql.user;
+-----------+------------------+
| host | user |
+-----------+------------------+
| % | root |
| % | user |
| localhost | mysql.infoschema |
| localhost | mysql.session |
| localhost | mysql.sys |
| localhost | root |
+-----------+------------------+
mysql> SELECT * FROM performance_schema.host_cache;
Empty set (0.02 sec)
"localhost" says to use a socket. But VPS probably allows only TCP/IP connections, not "socket".
I guess on local machine, you are not using docker. However, on vps, you are.
On you local, mysql is installed on the OS itself, hence, it has exposed its socket file at /var/run/mysqld/mysqld.sock. However, on VPS, the socket is inside docker.
The highlight here is the difference between localhost and 127.0.0.1 in reference to mysql. On unix based systems(including mac), localhost connects via socket, while 127.0.0.1 connects via TCP/IP.
Try adding following under docker-compose volumes section. See if it works then.
volumes:
- /var/run/mysqld/mysqld.sock:/var/run/mysqld/mysqld.sock
There are many different root causes for a connection denied.
Since it is possible to execute queries inside the container, the best course of action is to inspect the performance_schema.host_cache table:
SELECT * FROM performance_schema.host_cache;
It should point more precisely at the problem.
Doc:
https://dev.mysql.com/doc/mysql-perfschema-excerpt/8.0/en/host-cache-table.html
Related
I am trying to connect to the database remotely.
I changed the mysqld.cnf to
[mysqld]
bind-address = 192.168.x.yyy
mysqlx-bind-address = 192.168.x.yyy
skip-networking
I checked the ports with iptables -S
-A INPUT -p tcp -m tcp --dport 3306 -c 10 600 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 3306 -c 0 0 -j ACCEPT
I tried to run telnet 192.168.x.yyy 3306
telnet: Unable to connect to remote host: Connection refused
Of course, I called
sudo service mysql restart
The command mysql -u root -p is OK but mysql -h 192.168.x.yyy -u root -p returns
ERROR 2003 (HY000): Can't connect to MySQL server on '192.168.x.yyy' (111)
The table mysql.user contains:
mysql> select Host,User from user;
+---------------+------------------+
| Host | User |
+---------------+------------------+
| 192.168.x.yyy | root |
| ...
| localhost | root |
+---------------+------------------+
The ifconfig returns:
enp2s0f0: ...
inet 192.168.x.yyy netmask 255.255.255.0 broadcast 192.168.x.255
I am running Ubuntu 20.04.1 LTS and mysqld 8.0.23
In your quoted mysqld.cnf, you have included the skip-networking rule which disabled all external network access to the MySQL server.
If remote access is required, you should remove this rule and restart your mysql server process.
See the documentation for further details.
I have a docker setup that is running Mysql 8.
I am able to access Mysql inside docker container, however, I am unable to connect using Mysql workbench. I have another mysql container but this one is Mysql version 5.7 and I have no issues connecting with that one.
I have tried to allow root user full host access with % in the mysql.user
| root | % |
| mysql.infoschema | localhost |
| mysql.session | localhost |
| mysql.sys | localhost |
I have tried to connect to container using hostnames: localhost, 0.0.0.0, 127.0.0.1
and port 3306 but no luck
I also created a separate user and gave full privilege and still no luck
Below is my Docker compose file
version: '3.5'
services:
database:
build:
context: ./images/mysql
restart: always
command: ['--default-authentication-plugin=mysql_native_password']
environment:
- MYSQL_DATABASE=inim
- MYSQL_USER=inimuser
- MYSQL_PASSWORD=inimpass
- MYSQL_ROOT_PASSWORD=docker
ports:
- "3306:3306"
volumes:
- ./database/data:/var/lib
volumes:
my-datavolume:
my Dockerfile:
FROM mysql:8.0.20
CMD ["mysqld"]
EXPOSE 3306
Below is my Docker PS
b8832d9711b4 docker_inim_db_database "docker-entrypoint.s…" 14 minutes ago Up 14 minutes 0.0.0.0:3306->3306/tcp, 33060/tcp docker_inim_db_database_1
I have a similar setup with Mysql 5.7 and it connects fine. Not sure what I am doing wrong here.
You can debug your process by going to the container.
Please run:
$ docker exec docker_inim_db_database_1 /bin/bash
Now you should be in bash of the container.
Then login to mysql bash using:
$ mysql -uroot -p
and type your password or just press Enter if none.
There's two steps in that process:
a) Grant privileges. As root user execute with this substituting 'password' with your current root password :
GRANT ALL PRIVILEGES ON *.* TO 'root'#'%' IDENTIFIED BY 'password';
b) bind to all addresses:
The easiest way is to comment out the line in your my.cnf file:
#bind-address = 127.0.0.1
and restart mysql
service mysql restart
By default it binds only to localhost, but if you comment the line it binds to all interfaces it finds. Commenting out the line is equivalent to bind-address=*.
To check where mysql service has binded execute as root:
netstat -tupan | grep mysql
Update For Ubuntu 16:
Config file is (now)
/etc/mysql/mysql.conf.d/mysqld.cnf
Most of steps above are from answer: https://stackoverflow.com/a/11225588/659077
I already have installed mySql on my pc so port 3306 is already busy. This is the reason why I have to use a different port from 3306. I want to be able to connect with my machine to my docekr instance without using docker commands so I will be able to connect to that instance with my application (Spring web app).
Docker commands that I used:
docker run --name jt-mysql -e MYSQL_ROOT_PASSWORD=password -p 3307:3307 -d mysql
Then I tried to connect to that istance with:
mysql --user=root -P 3307 -p
In this case I get the following error:
ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using
password: YES)
Please note that if I tried to use the instance installed on my pc it works, using with:
mysql --user=root -P 3306 -p
Other information about my docker instance using:
docker ps
I get:
f52a94aa63da mysql "docker-entrypoint.s…" 4
minutes ago Up 4 minutes 3306/tcp, 33060/tcp,
0.0.0.0:3307->3307/tcp jt-mysql
with status insided my docker image (entering using docker commands) I get:
Connection id: 11
Current database:
Current user: root#localhost
SSL: Not in use
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server version: 8.0.19 MySQL Community Server - GPL
Protocol version: 10
Connection: Localhost via UNIX socket
Server characterset: utf8mb4
Db characterset: utf8mb4
Client characterset: latin1
Conn. characterset: latin1
UNIX socket: /var/run/mysqld/mysqld.sock
Binary data as: Hexadecimal
Uptime: 16 min 50 sec
using env command:
HOSTNAME=f52a94aa63da
MYSQL_ROOT_PASSWORD=password
PWD=/
HOME=/root
MYSQL_MAJOR=8.0
GOSU_VERSION=1.7
MYSQL_VERSION=8.0.19-1debian9
TERM=xterm
SHLVL=1
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
_=/usr/bin/env
Command to start MySQL container at port 3306 and expose at port 3307
docker container run -d --name=LocalMySQLDB -p 3307:3306 -e MYSQL_ROOT_PASSWORD=password mysql
OR
docker run -d --name=LocalMySQLDB -p 3307:3306 -e MYSQL_ROOT_PASSWORD=password mysql
The above command with start the MySQL database server inside "LocalMySQLDB" container
Now to connect to the containerized mysql instance use below attached command
mysql -h 127.0.0.1 -uroot -P 3307 -ppassword
I have tried this many a times on my local machine for testing purposes. It will definitely work for you as well.
Please comment if it will not work in your case.
Start Docker container using following command:
docker run -d -p 3307:3306 --name mysql_server -e MYSQL_ROOT_PASSWORD=123456 mysql
Connect to container from host using following command:
mysql -u root -P 3307 --protocol=tcp -p
When you run docker container, please try to add this param at the end.
docker run --name jt-mysql -e MYSQL_ROOT_PASSWORD=password -p 3307:3306 -d mysql --network host
I'm trying to get a docker container to work with mariadb and node.js images. The container will use an existing database in /home/mysql. However, when the container is launched, I'm getting this "failed to connect" error in node.js:
Error: ER_HOST_NOT_PRIVILEGED:
Host '172.18.0.5' is not allowed to connect to this MariaDB server
Here's my docker-compose.yml:
version: '3'
services:
mariadb:
image: mariadb
restart: always
volumes:
- /home/mysql:/var/lib/mysql
user: "mysql"
ports:
- "3306:3306"
watch:
build: .
restart: always
links:
- mariadb:mysql
environment:
- DOCKER_IP=172.18.0.2
depends_on: ['mariadb']
ports:
- "3000:3000"
After reading this thread, I found that mysql is actually running, but it fails to let other services connect:
These are some of the steps I have checked. As you can see, I can log in to the mysql instance:
$ docker exec -it 552aae9ea09c bash
mysql#552aae9ea09c:/$ mysql -u root -p
Enter password: *******
MariaDB [(none)]> SELECT host, user FROM mysql.user;
+-------------+------------------+
| host | user |
+-------------+------------------+
| 127.0.0.1 | root |
| ::1 | root |
| someusername| |
| someusername| root |
| localhost | |
| localhost | dbusername |
| localhost | databasename |
| localhost | root |
+-------------+------------------+
8 rows in set (0.00 sec)
mysql#552aae9ea09c:/$ mysqld --verbose --help | grep bind-address
2017-11-13 17:35:40 139825857279872 [Note] Plugin 'FEEDBACK' is disabled.
--bind-address=name IP address to bind to.
bind-address (No default value)
One thing to note is that even though I've explicitly set the user to mysql in the yml file, these three files in /home/mysql: ib_logfile0,ib_logfile1, ib_buffer_pool are still under the group of systemd-journal-remote, which I suspect has something to do with the connection failure.(reference)
The error you are receiving is caused by the fact that MariaDB thinks you are not authorized to connect to the server. This means that you haven't created a database user for the Node.js app or the grants for that user are incorrect.
A fool-proof way to solve this is to create a separate user for the Node.js application. You can automate this by writing the following SQL into a file and mounting the volume into /docker-entrypoint-initdb.d.
CREATE USER 'my-app-user'#'%' IDENTIFIED BY 'my-app-password';
GRANT ALL ON *.* TO 'my-app-user'#'%';
Change the username and password accordingly and reduce the given privileges from the ALL privilege. You can also change the wildcard hostname % to a specific IP address or hostname.
Simply run this sql query:
GRANT ALL PRIVILEGES ON *.* TO 'root'#'%' IDENTIFIED BY 'password'
(assuming that you are connecting as the root user)
Well, after reading topics with the same name without success I feel forced to ask again and show you my scenario:
I am on a Kali Linux machine, my mysql config file (/etc/my.cnf) is setup this way:
bind-address = 172.16.1.228
I reset the service I can't enter neither remotely nor localy, I got this 2 errors depending on how I access:
root#Adkadon:~# mysql -u root -p -h 172.16.1.228
Enter password:
ERROR 1130 (HY000): Host 'Adkadon' is not allowed to connect to this MySQL server
mysql -u root -p -h 127.0.0.1
Enter password:
ERROR 2003 (HY000): Can't connect to MySQL server on '127.0.0.1' (111)
Note that without the -h 127.0.0.1 I have never been allowed to access.
So, I change the my.cnf and set bind-address to 0.0.0.0.
I access this way: mysql -u root -p -h 127.0.0.1, I do the following:
GRANT ALL PRIVILEGES ON *.* TO root#172.16.1.228 BY ‘root‘ WITH GRANT OPTION;
Again change bind-address to 172.16.1.228 and no success.
This is the output of SELECT user,host FROM user; inside the database:
root | 127.0.0.1 |
| root | 172.1.16.228 |
| root | ::1 |
| debian-sys-maint | localhost |
| root | localhost |
| root | repo
I don't know what do to, any idea¿? Thank you very much
If I guess right, 172.1.16.228 is your IP of mysql server .
'Adkadon' is the host where you try to access mysql from.
If that is the case, get the IP address of Adkadon (ifconfig)
Say if 172.1.16.xxx is your host IP, then in my.cnf mention
bind-address = 172.16.1.xxx
This indicates connections are allowed only from 172.16.1.xxx
Create a user root#172.16.1.xxx
And for connecting to mysql , use command
mysql -u root -p -h 172.1.16.228
Please note, -h 172.1.16.228 indicates where to connect to, not where it is connecting from.
Does it solve your problem? Or am I missing something from your question?