Connect localhost Mysql from Docker .net core - mysql

I have working WebAPI in .net core in Docker. I want to deploy this API on AWS EC2 instance with local mysql database working with other web app.
How can I reach this Mysql from inside docker?
Locally I can do it by using my private ip addres in
=> optionsBuilder.UseMySql(#"Server=$my_local_ip;database=db_name;uid=user;pwd=pass;");
Ho to determine which $my_local_ip should I use in order to connect to DB?
But while using the private ip on EC2 I got error while sending request that it can't connect to any MYSQL host.

For MySQL server you generally have to specify the port to connect to, as each instance on the same server uses a different port.

Use --network=host flag while starting the Web API container and use the localhost in you connection string as the host name. This will enable the Docker container to access the host network. Hence you can access MySQL Database from the container.
Note: This is an insecure for running containerized workloads. Click Here to learn more

Related

Not able to connect MySQL server running on docker with Node-Red container

I have a VM (OS: Ubuntu 18.04) running in azure cloud in which I have installed docker. In docker I have two containers MySQL(8.0.20) and node-red(v2.1.3), I am trying to connect my MySQL with node-red and I get the following error message in Node-Red
"Error: ER_NOT_SUPPORTED_AUTH_MODE: Client does not support authentication protocol requested by server; consider upgrading MySQL client"".
MySQL is working fine, I am able to access the database via putty but somehow I am not able to make a successful connection with node-red as well as Grafana.
I am using following configuration. My VM's IP address is 20.107.194.152 and MySQL port is 3306
Can some one tell me what I am doing wrong here. Thanks.

Connection refused from serverless-offline lambda to host database

This question is related to serverless-offline plugin, local mysql database connection. The scenarios for my test is as follows.
Using serverless-offline plugin, a lambda function is deployed locally on my machine.
The triggered lambda is not possible to connect with the local database.
Probably, serverless-offline creates a docker image to launch a lambda, and the address is not correct in the docker container and port mapping. However, serverless-offline does not support those docker options. I am stuck here to connect the database from the lambdas deployed locally with serverless-offline.
I used localhost:3306 for the db host, but it does not work. I tried port forwarding to connect the database via public ip address which does not work.
The database connection can be established somehow, but the connection is refused all the time. Any help?
I'll do my best to address several areas of your post in order of their appearance
serverless-offline creates a docker image to launch a lambda
Incorrect. Serverless Framework and its plugins (serverless-offline, etc.) have absolutely nothing to do with Docker, or Docker related technologies.
I used localhost:3306 for the db host, but it does not work
From your post, I am gathering that you simply do not have a MySQL service running on your local machine. Is that what you need? Reply to this post and I'll try to help, or simply google examples of how to install/start/configure a MySQL server.
I tried port forwarding to connect the database via public ip address which does not work.
I assume you're talking about the popular ssh -L trick to connect to a remote database over SSH connection? From your post, I am gathering that you simply are not performing this operation correctly. Do you need help doing that? Reply to this post and I'll try to help, or simply google examples of how to use SSH Port Forwarding to connect to a MySQL database.

Inter-Service communication between Kubernetes for mysql and tomcat

I have two services running on kubernetes using kubectl.
1. Tomcat
2. MYSQL
Scenario - Tomcat is external world facing. Hosted tomcat using node port. And MySQL service is backend and hence hosted it using cluster IP (default type).
Tomcat is not able to connect to MySQL. War file which I am using, need JDBC URL for connection.
I am able to connect to MySQL from my instance(master node). but tomcat service is not able to do.
We can do this by providing the service name directly.
Here, For MySQL service I can provide MySQL-middleware-service2(NAme of my MySQL service)

Connect to host postgres db from minishift

Im trying to connect to a postgres database, from a springboot application deployed in minishift.
The postgres server is running on the same host that minishift is running on.
I've tried setting the postgres serve to listen on a specific IP address, and use this same address in the springboot jdbc connection url but I still get org.postgresql.util.PSQLException: Connection to 172.99.0.1:5432 refused
I've also tried using 10.0.2.2
Also tried, in /etc/postgresql/9.5/main/postgresql.conf, setting:
listen_addresses = '*'
How can I connect to a database external to minishift, running on same host?
Besides the answer referenced in my comment, which suggests to make your database listen on the IP address of the Docker bridge, you could make your pod use the network stack of your host. This way you could reach Postgres on the loopback. This works only if can guarantee that the pod will always run on the same host as the database.
The Kubernetes documentation discourages using hostNetwork. If you understand the consequences you can enable it as in this example.
If a pod inside kubernetes can't see the IP address from the host then I guess its an underlying firewall or networking issue. Try opening a shell inside the pod...
kubectl exec -it mypodname bash
Then trying to ping, telnet, curl, wget or whatever to see if you can see the IP address.
It sounds like something's wrong with the networking setup of your minishift. It might be worth raising an issue with minishift: https://github.com/minishift/minishift/issues/new
If you can find an IP address on the host which is accessible from a docker pod you can create a Kubernetes Service and then an Endpoint for the service with the IP address of the database on your host; then you can use the usual DNS discovery of kubernetes services (i.e. using the service name as the DNS name) which will then resolve to the IP address. Over time you could have multiple IP addresses for failover etc.
See: https://kubernetes.io/docs/user-guide/services/#without-selectors
Then you can use Services to talk to all your actual network endpoints with your application code completely decoupled on if the endpoints are implemented inside kubernetes, outside with load balancing baked in!

Connect Amazon EC2 to my Local MySQL Database

Is it possible to do the following?:
I have a local Mac running OS X Lion with a MySQL Server installed which runs different processes regularly and stores data into a local DB.
On the other hand I have an Amazon EC2 instance.
What I would like to do is to use the Amazon instance to perform certain cronjobs (using its own resources) but connecting to the data that is on my localhost (my computer) and performing basic SQL actions like updating the data, inserting, etc.
I don't know if this helps, but I have a static IP. Is there any way I can "open" my IP so the Amazon instance can recognize my home computer as a valid MySQL server?
Thanks for your help, any tip in the right direction will be much appreciated.
If your EC2 instance is connecting to your local db you'll need a static IP locally. Well not need... but if you don't have it anytime you reset your router or loose power etc your ip will change. You can look into Dynamic DNS as well for your local instance.
Your ISP will not block your port on your local instance. This would be a firewall inside your network that is preventing you from connecting to mysql or a configuration with mysql itself. Users can only connect to mysql from certain IP addresses.
You would have to open up the mysql port on your firewall if you are using one, have the mysql client installed on your EC2 instance, and make sure that the user that had proper grantable permissions.
That said why not run cron locally on the mac?
If you can login to your server via SSH then there's no need to open any other port and no static IP is required. You can use SSH port forwarding instead. From your local machine run:
ssh -C -R 5555:127.0.0.1:3306 <your-server-host>
Now you should be able to connect to your Mac's database running on localhost:3306 from the remote server at localhost:5555.