Skip to main content
Redhat Developers  Logo
  • AI

    Get started with AI

    • Red Hat AI
      Accelerate the development and deployment of enterprise AI solutions.
    • AI learning hub
      Explore learning materials and tools, organized by task.
    • AI interactive demos
      Click through scenarios with Red Hat AI, including training LLMs and more.
    • AI/ML learning paths
      Expand your OpenShift AI knowledge using these learning resources.
    • AI quickstarts
      Focused AI use cases designed for fast deployment on Red Hat AI platforms.
    • No-cost AI training
      Foundational Red Hat AI training.

    Featured resources

    • OpenShift AI learning
    • Open source AI for developers
    • AI product application development
    • Open source-powered AI/ML for hybrid cloud
    • AI and Node.js cheat sheet

    Red Hat AI Factory with NVIDIA

    • Red Hat AI Factory with NVIDIA is a co-engineered, enterprise-grade AI solution for building, deploying, and managing AI at scale across hybrid cloud environments.
    • Explore the solution
  • Learn

    Self-guided

    • Documentation
      Find answers, get step-by-step guidance, and learn how to use Red Hat products.
    • Learning paths
      Explore curated walkthroughs for common development tasks.
    • See all learning

    Hands-on

    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.
    • Interactive labs
      Learn by doing in these hands-on, browser-based experiences.
    • Interactive demos
      Click through product features in these guided tours.

    Browse by topic

    • AI/ML
    • Automation
    • Java
    • Kubernetes
    • Linux
    • See all topics

    Training & certifications

    • Courses and exams
    • Certifications
    • Skills assessments
    • Red Hat Academy
    • Learning subscription
    • Explore training
  • Build

    Get started

    • Red Hat build of Podman Desktop
      A downloadable, local development hub to experiment with our products and builds.
    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.

    Download products

    • Access product downloads to start building and testing right away.
    • Red Hat Enterprise Linux
    • Red Hat AI
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Featured

    • Red Hat build of OpenJDK
    • Red Hat JBoss Enterprise Application Platform
    • Red Hat OpenShift Dev Spaces
    • Red Hat Developer Toolset

    References

    • E-books
    • Documentation
    • Cheat sheets
    • Architecture center
  • Community

    Get involved

    • Events
    • Live AI events
    • Red Hat Summit
    • Red Hat Accelerators
    • Community discussions

    Follow along

    • Articles & blogs
    • Developer newsletter
    • Videos
    • Github

    Get help

    • Customer service
    • Customer support
    • Regional contacts
    • Find a partner

    Join the Red Hat Developer program

    • Download Red Hat products and project builds, access support documentation, learning content, and more.
    • Explore the benefits

Using a MySQL database in your Red Hat OpenShift application

October 23, 2019
Don Schenck
Related topics:
ContainersMicroservicesKubernetes
Related products:
Red Hat OpenShift

    Creating a MySQL database in Red Hat OpenShift is useful for developers, there's no doubt about that. But, once the database is ready, with tables and data, how do you use the data in your application? Is there some special magic when using Red Hat OpenShift? What about the fact that pod names can change? This article will walk you through the steps necessary to access a MySQL database that is running in your OpenShift cluster.

    There is code

    The code to accompany this blog post is available at the associated GitHub repo.

    Let's get a database

    The first step — after creating an OpenShift cluster — is to create a MySQL database in OpenShift. My previous article describes how to create an ephemeral MySQL database in your OpenShift cluster. For the sake of this article, we make the following assumptions:

    1. The cluster name is mysql.
    2. The project name is mysqsl-test.
    3. The database name is sampledb.

    If you change any of these values, you'll need to change commands and scripts as necessary.

    Database up and running

    By following the previous article, you will now have a database up and running in OpenShift. In that article, we named the MySQL database instance mysql.  This is important; if you used a different name, make note of it, as we'll need it soon.

    The services

    For this demo, we're going to run two very simple microservices, getCustomer and getCustomerSummaryList, in pods in our OpenShift cluster. The source code for these services exists in a directory in a GitHub repo (mentioned previously), so rather than create an image on our local machine and push it to our cluster, we'll use OpenShift's slick Source-to-Image (or S2I) build feature.

    The S2I feature allows you to reference a GitHub repo in OpenShift and trigger automatic builds from source. OpenShift will fetch the source code, analyze it, and build it according to what type of source code it is (e.g., Node.js, Ruby, etc.). The resulting image will be stored in the OpenShift cluster's internal registry.

    You can optionally configure your GitHub repo to post a webhook to your OpenShift cluster whenever a pull request is approved, triggering a rebuild of the OpenShift-hosted image.

    To build our microservices from source, we'll use the following commands.

    Note: You need to alter the MYSQL_USER and MYSQL_PASSWORD values to match the credentials that were supplied when you created the database (in the previous article). If you don't have those values, you'll need to add a user with the proper rights to your MySQL instance.

    oc new-app https://github.com/redhat-developer-demos/mysql-openshift-ephemeral.git --context-dir=src/getCustomer --name getcustomer -e MYSQL_HOST=mysql -e MYSQL_DATABASE=sampledb -e MYSQL_USER=mysql_userid_goes_here -e MYSQL_PASSWORD=mysql_password_goes_here
    oc new-app https://github.com/redhat-developer-demos/mysql-openshift-ephemeral.git --context-dir=src/getCustomerSummaryList --name getcustomersummarylist -e MYSQL_HOST=mysql -e MYSQL_DATABASE=sampledb -e MYSQL_USER=mysql_userid_goes_here -e MYSQL_PASSWORD=mysql_password_goes_here

    These two commands will launch builds inside your OpenShift cluster that should take about a minute. If you run oc get pods, you can see them running. When they're finished, we have to services running in our OpenShift cluster. They are not reachable from outside the cluster; this is by design. We're going to add a website to the cluster as well. That website will use these two services, and we'll expose the website to the world via a public IP address and URL.

    The website

    To create the website, we'll use a different method. Rather than use source code, we'll pull a Linux image into OpenShift. The following command will pull the image into the OpenShift cluster's internal image registry and start it.

    oc new-app --name mvccustomer --docker-image=quay.io/donschenck/mvccustomer:latest -e GET_CUSTOMER_SUMMARY_LIST_URI="http://getcustomersummarylist:8080/customers" -e GET_CUSTOMER_URI="http://getcustomer:8080/customer"

    Note that we're supplying environment variables that define the path, inside OpenShift, to the two services we just built.

    There's one more small step. We need to expose this website to the world so can browse to it from our desktop browser. We do that, and create a "route" in OpenShift, by using the following command:

    oc expose service mvccustomer --insecure-skip-tls-verify=false

    At this point, you have two microservices that will retrieve a customer list and a single customer from your MySQL database, running in OpenShift — as is our ephemeral MySQL database. We also have a website that uses these two services. We can see the URL for our website by running the following command:

    oc get routes

    Looking at the URI, you can see the obvious parts that refer to my service, my project, my cluster, and my domain. The URI format is as follows:

    http://{service_name}-{project-name}.app.{cluster_name}.{domain_name}

    Remember when I mentioned, in the "Let's get a database" section above, how we assumed some names? Well, if you did decide to change things up, you would notice it here.

    Discovery

    This idea, that you can know ahead of time what the URI will be, is thanks to Kubernetes' feature known as "Service Discovery." Because you assign a name to service, Kubernetes will keep track of the pods associated with it, even if the pod names change (e.g., a pod is deleted and replaced by a new pod). Even better: As you scale up to multiple pods, you still have only one URI. Kubernetes takes care of the load balancing between the pods.

    Launching the website

    Simply paste the URL into your browser and start clicking.

    What's next?

    The next step is to create a permanent (i.e., not ephemeral) MySQL instance running in Red Hat OpenShift. That's yet another article.

    Last updated: July 1, 2020

    Recent Posts

    • Federated identity across the hybrid cloud using zero trust workload identity manager

    • Confidential virtual machine storage attack scenarios

    • Introducing virtualization platform autopilot

    • Integrate zero trust workload identity manager with Red Hat OpenShift GitOps

    • Best Practice Configuration and Tuning for Linux and Windows VMs

    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Platforms

    • Red Hat AI
    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Build

    • Developer Sandbox
    • Developer tools
    • Interactive tutorials
    • API catalog

    Quicklinks

    • Learning resources
    • E-books
    • Cheat sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site status dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit
    © 2026 Red Hat

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Chat Support

    Please log in with your Red Hat account to access chat support.