Mac: Cannot connect to SQL server through Docker - mysql

I have an application which uses LocalDB and runs fine on a Windows PC, however I am trying to run this through my mac.... I'm struggling with connecting the application to a mySQL server which sits on docker. I have been following the other guides such as (https://learn.microsoft.com/en-us/sql/linux/quickstart-install-connect-docker?view=sql-server-ver15&pivots=cs1-bash) related to this topic with no luck...
Here's what I've done so far, through terminal:
sudo docker pull mcr.microsoft.com/mssql/server:2019-CU5-ubuntu-18.04
sudo docker run -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=<MyPassword123!>"
-p 1433:1433 --name sql_container
-d mcr.microsoft.com/mssql/server:2019-CU5-ubuntu-18.04
Verified this is running by using sudo docker ps -a
Also verified by using Kitematic, container seems to be running well
MySQL looks well set-up in System Preferences
I am able to connect to the container through Azure Data Studio using IP address as server name, as shown below
Within the Application, this is is what the ConnectionStrings look like within the appsettings.json folder...
However, when I run the application and try and hit the container, through a GET request OR on Swagger, I am met with the following reply; "error": "Cannot connect to SQL Server Browser. Ensure SQL Server Browser has been started."
I have researched this and tried the solution of including the port number after the IP number as such Server=(19X.XXX.X.XX, 1433) but this also comes back with the error; "error": "A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: TCP Provider, error: 25 - Connection string is not valid)"
I'm not sure where to go from here... any help to get this working would be greatly appreciated!

As trivial as it was, the correct syntax to make this work is Server=XXX.XXX.X.XX,1433
DO NOT INCLUDE PARENTHESES AND NO SPACE BETWEEN IP AND PORT

Related

Connecting to Remote MySQL DB in docker container using PDO

I am a bit of a newbie and new to the php coding world and have a small task I am trying to find an answer to, but I cannot seem to find the relevant question asked anywhere before.
Basically I have a server where I create reports from mySQL DBs located on other remote machines.
So far these other servers have had a basic mysql server running on them and I can easily connect to them with PDO like this:
$DBcon = new PDO("mysql:host={$DB_host};dbname={$DB_name}",$DB_user,$DB_pass);
$DBcon->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
But I now have a server running mySQL inside of a docker container on this remote server.
So my question is, is it possible to connect to this mySQL db as well using PDO?
I am able to connect to the DB remotely with mySQL workbench using Standard TCP/IP over SSH (as I also do with the other servers), but the one thing I have to specify differently here compared to the other servers not using docker is the 'MySQL server host relative to the SSH server'. Usually just having 127.0.0.1 here is fine, but with mySQL inside a docker container I first have to find this relative address with
docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' database-name
you then get something like 172.21.0.5 and add this in that field
workbench connection
How are you able to represent this small change in the PDO statement? Is it still possible to connect in this way?
actually, no. You can't connect inside code as mysql workbench over ssh. But you can use ssh jumb for this connection as below link.
https://www.tecmint.com/access-linux-server-using-a-jump-host/
You should configure jump command to a mysql servers with port. this jump command provides your connection via bastion you mentioned instance for ssh to the mysql server.
for example: ssh -N -i pemfile.pem(if you have to connect to jump server) -L fakemysqlserverip:3306:mysqlserverrealip:3306 ec2-user#jumpserverip
fakemysqlserverip may be localhost,127.0.0.1 or different
jumpserverip is your server which is for using over ssh
Regards

MySQL cli client hangs when connecting to Azure managed database

I have a MySQL database on Azure, using the Azure managed database service, and two Ubuntu 20.04 VMs (running PHP applications) in the same VNET. I can connect to the database from both PHP, and from a remote GUI client (SequelPro), so I'm confident the firewall is configured correctly and I'm using the right details.
However, when I try to connect using the mysql CLI client on either of the VMs, it just hangs with no output. I've tried on both VMs, I get the same behaviour.
The command I'm using is:
mysql -u "username#hostname" -p -h "ip_address" -P 3306 database_name -e "SHOW TABLES"
It prompts for the password, so I enter it... and then nothing. On top the mysql process is consuming 100% of CPU.
I can telnet port 3306 on the IP address, I get the usual gibberish asking for mysql_native_password.
If I change the hostname part of the username#hostname to an invalid hostname, then it says "The servername cannot be found". Whereas if I enter an invalid username (or an invalid password) then it hangs just the same. So I'm guessing this has something to do with the gateway part of Azure managed database service that's trying to resolve that name. Everything was working normally up until a few days ago.
There's nothing in any of the logs, and no output on the screen, so I can't work out where to begin trying to fix this.
This appears to be due to https://bugs.mysql.com/bug.php?id=105288 assuming your client is 8.0.27, i hit the same issue today.

Not able to connect with mysql on linux(Cent OS) server

I am trying to connect mysql in my node js application. It's working fine into my local but when I tried to deploy application onto live server, I am not able to connect with the mysql.
My mysql credentials are correct, I tried by using below command:
mysql -u username -p
I also tried below command:
service restart mysql
But this command giving me error which is: "service will not execute and completion _service exists."
Don't know what's getting wrong. It's a fresh server which I bought yesterday. You can see the attached error
Can anyone please help with the same? Thanks in advance.

How do you set up MySQL in Docker and connect to it?

I'm trying to set up an e2e test. For this, I need a local test database, preferably in Docker so that test setup is simple. Here is a minimal project showing what I currently have: https://gitlab.com/MakotoE/mysql-test If this issue is fixed, npm test will finish with exit code 0. This is being run on Windows 10.
In main.js, I start an instance of the Docker image mysql:8.0 like this:
const command = 'docker run -e MYSQL_ALLOW_EMPTY_PASSWORD=yes -p 3306:3306 -d mysql:8.0';
ChildProcess.execSync(command);
I want to connect to it with mysql2, but it fails with an Error: connect ETIMEDOUT.
await mysql.createConnection({
host: 'localhost',
user: 'root',
});
If I try to connect to the database created in the JavaScript code, through command prompt (mysql -uroot), I get this error: ERROR 2013 (HY000): Lost connection to MySQL server at 'reading initial communication packet', system error: 0. If I create the container in command prompt with the same docker run command used in the code, I can connect successfully, so this issue might be related to ChildProcess.execSync, but I don't know how I can fix this.
How can I fix my code so that I can start a MySQL server in Docker and connect to it in Node.js running on Windows 10?
Edit: I'm not sure if this is related, but running this on Debian shows this error message when creating the container:
Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock:
Post http://%2Fvar%2Frun%2Fdocker.sock/v1.39/containers/57f5b631e551466e1f9928ba62f7a5e2a5ad2a481da4686bda8684f1a71b8d7/stop: dial unix /var/run/docker.sock: connect: permission denied
And connection fails with ETIMEDOUT.
Edit: I added branch secondAttempt where I unsuccessfully tried running all commands outside the .js file, through NPM scripts. After running the test for this branch, you must manually stop the Docker container.
In the mysql -uroot command, it fails with:
ERROR 2013 (HY000): Lost connection to MySQL server at 'reading initial communication packet', system error: 0
which I believe is the same error as experienced previously, with different messages (due to different clients).
This experiment tells me two things: ChildProcess.execSync is not the root cause to this issue, and that there is something about this system (whether it is Docker, or the TCP connection to the Docker) that prevents scripted commands.
I finally figured this out. All I need to do is wait 15 seconds before connecting.
The mistake I made is a common one: not reading the readme. In the mysql Docker Hub page, under section "No connections until MySQL init completes", it mentions how you must wait for database initialization to complete. The example repos achieve this via looping to retry the connection, but I think its simpler to delay for a set period of time.
Before, I was under the impression that I only needed to wait ~1 second after docker run, but actually it takes ~14 seconds for the database to be initialized. The fix was as simple as using setTimeout to wait 15-20 seconds before connecting to the database. I have given an example in branch fixed!.

unable to connect cf ic login

I am trying to have a container running in Bluemix. I have cf version 6.15, and I want to deploy a docker file I have a connection failed. Any experience around to help me with this? I appreciate in advance.
The commands I am running from windows are the following:
c:\build>cf login -u myusername -o myusername -s myspace
All authentication at this point is ok.
c:\build>cf ic login
And I have this error:
Client certificates are being retrieved from IBM Containers...
FAILED
Put https://containers-api.ng.bluemix.net:8443/v3/tlskey/refresh: dial tcp
198.23.117.148:8443: ConnectEx tcp: A connection attempt failed because the
connected party did not properly respond after a period of time, or
established connection failed because connected host has failed to respond.
In particular, I am trying to do the following coding which I followed step by step.
Also, this is a Windows 7 and I have no docker for windows installed (not sure it is needed). Many thanks!