How to connect to MySQL database cluster - mysql

I tried to install a MySQL cluster with the Docker image below.
mysql/mysql-cluster - Docker Image | Docker Hub
The Docker image is pulled and run successfully.
Despite that I could connect to the cluster in the terminal (as shown in the screen capture below), I don't know how to connect to it with MySQL Workbench or DBeaver.

In your docker run command, you can use -p 3306:3306 (or any available port). Then you can use <host>:<port> from Workbench or Dbeaver connection URL.

I assume that you already know how to add new DB connection to MySQL Workbench or DBeaver. The information that you want is the connection URL and the username/password of an authenticated user that you need to use to connect to your MySQL cluster.
For the connection URL: 192.168.0.10 (no port in your example)
You need to have your MySQL Workbench or DBeaver connect to the URL of the MySQL node, which is mysql1 node in your example. As shown in your screen capture, it is 192.168.0.10 without any explicit port. But if you have troubles with the URL, you can run docker ps to check what host and port that your mysql1 is running and exposed at.
For the username/password: root/tpffnrtm1 (the password is the value of MYSQL_ROOT_PASSWORD as shown in your docker run of MySQL node command)
I assume that you just want to connect the DB cluster by any means (root or non-root privileges is totally fine for you).

Related

Can't connect to MySQL server running inside custom docker

I'm running this on a Ubuntu terminal on my Windows laptop. I'm using docker desktop and have enabled integration with Ubuntu like so:
I've also tried this with docker installed through the Ubuntu terminal (instead of Docker desktop on Windows), but experienced the same issue.
I have a MySQL database in a docker image, and an api docker image, and am running both:
I can access MySQL by running the following command: mysql -uroot -ppassword -h0.0.0.0 -P3306. The api is referencing is connected to this container with the following connection string located in appsettings.json: "Server=0.0.0.0; Port=3306; Uid=root; Pwd=#G3minar31; Database=cybersecuritydatabase". This is also shown in the image below. However, when I try to make a request to the endpoint from the backendapi container (http://127.0.0.1:5001/company/allcompanies), I receive a Can't connect to server error.
How can I resolve this?
UPDATE: When I run mysql -uroot -ppassword -h0.0.0.0 -P3306 in my Windows terminal I receive the following error: ERROR 2003 (HY000): Can't connect to MySQL server on '0.0.0.0:3306' (10049), but if I run mysql -uroot -ppassword -P3306 without specifying the host, MySQL starts successfully.
0.0.0.0 or 127.0.0.1 in config won't work it will point to the app container
either you:
change db address in appsettings.json to your machine local address e.g: 192.168 ..
create a docker network and connect the 2 containers, change db address in appsettings.json to mysql container name instead of an ip address
Run mysql and backendapi with docker-compose, change db address in appsettings.json to mysql service name instead of an ip address, I recommend this

Can't connect to MySQL running in docker

I'm trying to build docker image with some legacy LAMP development stack for development purposes.
Basically I'm taking ubuntu image and installing bitnami LAMP stack.
Here's Dockerfile I have so far:
FROM ubuntu
EXPOSE 80 443 3306
WORKDIR /opt
COPY setup.sh .
RUN chmod +x setup.sh
RUN ./setup.sh # this bash script downloads and runs installer
CMD /opt/bitnami/ctlscript.sh start && tail -f /opt/bitnami/apache2/logs/access_log
Then I'm running that container like this:
docker run --name dev -d -p 8080:80 -p 3307:3306 -v "C:\\dev\\project:/opt/bitnami/apache2/htdocs" aburov/lamp5.6
All works as expected (app from c:\dev\project is accessible throug localhost:8080 and it can access database) except the fact tha I can't connect to MySQL from the host using mapped 3307 port.
I've tried connect from MySQL Workbench and JetBrains' DataGrip both failing with similar error:
Communications link failure with primary. No active connection found for master.
java.io.EOFException: unexpected end of stream, read 0 bytes from 4 (socket was closed by server).
I've tried:
Using map to another host's port (3306, 3308, 10123) assuming there some conflicts;
Using different MySQL drivers.
MySQL version is 5.6.
What I'm missing?
Thank you in advance!
After checking if the ports are mapped correctky and also verified da alocal mysqlclient can connect to the server, there is another possibiliry.
MySQL is in default configuration as security measure , only accepting access from the localhost.
So you have to control and change the following parameter in the my.cnf
[mysqld]
bind-address=0.0.0.0
That would allow access from every ip
Additionally by default root has privileges for localhost only so it's not allowed to connect with that user from another hosts. To fix that we can execute following SQL commands from within container:
GRANT CREATE USER ON *.* TO 'root'#'%';
FLUSH PRIVILEGES;
Another option would be to create separate user (by runnin corresponding SQL from within container).

How to access mysql databases inside docker from a SQL GUI?

I am using vessel to run my Laravel project.
As I understand it basically simply creates a put-together docker image of my laravel project with mysql and everything.
Now since its running in docker, is it possible for me to access the mySQL databases with datagrip or tableplus on my host machine?
You just need to expose a port via docker. If you dont,you will have port 3306 which can be accessed via the local containers but if you bind port 3307:3306 for instance you can connect to mysql locally on 3307
you will need to connect to MySQL server using the docker containers IP that can be found using docker inspect.
This can also help you:
https://towardsdatascience.com/connect-to-mysql-running-in-docker-container-from-a-local-machine-6d996c574e55

Connecting to MySql gear on another gear

so I have two free gears on OpenShift.
One is a PHP with MySql 5.7 from:
https://github.com/icflorescu/openshift-cartridge-mysql
To which I can remote login from my PC without any problem by SSH tunnel.
Now on the second gear I wanted to create Spring Boot app that would connect to DB on the first gear. Using env | grep MYSQL on first gear I receive:
OPENSHIFT_MYSQL_DB_PORT=13306
OPENSHIFT_MYSQL_DB_HOST=127.10.104.130
So this + my logging data was put into Spring application.properties, after successfull build Spring crashed at data pool creation because it could not connect to database so I SSHed into second hear and tried accessing MySQL instance from first gear via:
mysql -u root -h 127.10.104.130 -P 13306 but I get error message:
Can not connect to MySQL Server on '127.10.104.130' (113)
After that I tried:
mysql -u root -h myAppName-domain.rhcloud.com -P 13306 which results in longer time of connection but ultimatelly failling with:
Can not connect to MySQL Server on 'myAppName-domain.rhcloud.com' (110)
And I can easly ping gear#1 from gear#2 so I am confused - do I need some extra sql config or firewall settings? I am also doing tail on mysql logs and nothing is showing up like connection is not even made.
If you are connecting on another gear you have to use the OPENSHIFT_MYSQL_DB_PROXY_PORT instead.
The easiest way I find to tell if its a database issue or a network issue is to simply try to telnet to the remote mysql host.
Ex - this should result in a connection timeout if you are on a different gear.
telnet 127.10.104.130 13306
But this should connect:
telnet <mysql app id>.domain.rhcloud.com <WHATEVER THE PROXY PORT IS>
I'm the author of the openshift-cartridge-mysql mentioned above.
It's been a while since I've published that cartridge, but if I remember correctly, the setup script is quite unassuming and there's nothing created by default, so you have to create your own users, databases and explicitly grant the appropriate privileges.
Connect from your PC either by ssh or by Workbench over ssh tunnel, create your user and database, then execute something like:
GRANT ... ; FLUSH PRIVILEGES;
You can learn more on MySQL's GRANT command in the official manual.
There's a line in the repo README exemplifying how you could grant remote access to root, which is not as unsafe as it looks because you can only access the DB gear from your main application gear.
But ideally you'd want to limit the access as much as possible (to a specific user coming from a specific host/IP, such as your main application gear). Something like this:
GRANT ALL ON appdb.* TO 'appuser'#'appgear';
Don't forget to FLUSH PRIVILEGES when you're done.
I hope this helps,
#icflorescu

Accessing Database created in XAMPP while working in Laravel 5 homestead

I'm starting a large project in Laravel 5. In the past I used XAMPP for all local work. Now we're going to work in Homestead. I got Homestead set up and running and I booted up a Laravel 5 project in it. works fine.
Because of various reasons we do NOT want to use Laravel migrations but are using MYSQL workbench in combo with XAMPP-PHPMyAdmin for work on the mysql database. My question is: how can I access this mysql database from within the Homestead VM?
I tried all kinds of settings in the app/Config/database.php. but none work.
I looked around for related questions here, but none seem to answer the base of my question: is it even possible to access a remote database (or one on my host machine) from within homestead?
I read somewhere that I can create migrations from an SQL dump to duplicate the DB in homestead using a package (https://github.com/Xethron/migrations-generator). using this as a workaround, I could connect my Laravel app to my DB I have set up in XAMPP, generate the migrations, then connect back to the Homestead DB and finally run the migrations. I think this could work but I would prefer direct connection. any suggestions on how to connect to a database outside the Homestead VM would be welcome.
<1>Open .env (can be found in root of laravel)
<2>Change DB_HOST to your LAN IP address(something like 198.168.0.11)
DB_HOST=LAN_IP
<3>Now go to phpmyadmin and in privileges tab
Add new user and fill login info
Select host as any_host(or fill host with your PC name)
I am also relatively new to Laravel and Homestead too but have had some luck with it.
First I'm not sure it's such a great idea not to use the MySQL instance inside Homestead but if you have your reasons I'll take you at your word. That said from your "Homestead" directory you can "vagrant ssh" into your Homestead VM.
Here you can mysql -u homestead -psecret dbname into your database and configure it at will just as you would your XAMPP instance. If you are using Laravel models as long as they match the database you'll be fine.
To your point of connecting to another database from within the VM have you tried to connect to that outside database from the command line? As long as your XAMPP MySQL instance is configured to receive remote connections and you have a user properly configured you should be able to test from the command line from within the Vagrant VM. See below where 192.168.1.196 is the IP address of the box/VM containing the MySQL instance to which you want to connect. I'm not running XAMPP but I was able to make a connection from within Vagrant Homestead out of the VM to another MySQL instance running on my local machine by providing my local network address. "127.0.0.1" won't work obviously because the Vagrant VM will perceive that as itself.
mysql -u user -ppassword dbname -h 192.168.1.196
Hope this helps.