Sharding configuration for mysql db - mysql

I want to test a sharding example using spring boot and mysql database. If I start my mysql database on jdbc:mysql://localhost:3306/test and jdbc:mysql://localhost:3307/test. Or I could also use the same port but different schemas for testing purpose. Can I use nginx to configure as a reverse proxy to connect to the db depending on the userid(which would be my key for sharding? Or what is the best way to test it out. Is it better to shard at the application level itself. I would be using a springboot application for this.

It turns out that I was overcomplicating things. I needed to use a custom version AbstractRoutingDataSource along with multiple db configuration in spring to get it working. The whole solution can be see here

Related

How do I achieve persistence even when I stop running Spring?

I have just completed a Spring Boot tutorial of 34 videos. Im looking online and everything seems to show me how to create persistence of my data when the spring application is running. However, once I stop the program and restart it, it doesn't have the data I want saved.
So what it sounds like is I need to store this information on a database. I think I've set up a MySql server on my laptop and I have the workbench app/interface.
What might be more appropriate is connecting to an online server that I have with phpmyAdmin. In any case, how do I connect my spring application to a database instead of to a localhost:8080
Please let me know where I can look and what resources I have here as I'm kind of new to this sort of thing. Much appreciated!
There 2 options
Either use persistent DB instead of in-memory storage
You are using spring.jpa.hibernate.ddl-auto=create-drop hibernate property that will recreate db schema every time you start the application - thus its "clear" of the start. Use update or validate instead.
Maybe you can set spring.sql.init.mode=never to not always initialize your database using scripts.
This way you'll find all data you saved even when re-run the app.

How can one connect Prisma/GraphQL to an existing MySQL DB (specifically WordPress)?

I've been teaching myself GraphQL using Prisma -- very much a beginner at both.
I just got a programming test where the brief is to connect a React Native frontend to a WordPress backend. I thought this might be a fun opportunity to put some Prisma and GraphQL into practice. My thought was to connect Prisma to the WP MySQL DB and then use Prisma-GraphQL to connect to React Native.
However, it seems that connecting Prisma to MySQL isn't so straightforward -- at least for me, a beginner. I'd welcome any info as to how one might go about this -- not detailed code, of course, but more whether it's reasonable to do what I'm thinking of, and what packages/route would be the best way forward. Thanks!
An easy answer would be: All you need to connect Prisma to any MySQL database is a Host and a port(3306 because this is MYSQL) of where the DB is running.
And this leads us to where do you have your Prisma Server running?
if you are just a front-end dev and you don't know or don't wanna deal with stuff like docker then you better off using something like Prisma Cloud which will provision a Prisma server for you and can also host the MySQL Database for you or you can host it yourself and choose the have an existing database option and provide the hostname(URL) where you have it during creating your Prisma server.
on the other hand, if you comfortable running it locally or on a cloud provider with Docker(Local Prisma Setup) then you just provide the host and port as you can see in the docs link above in your docker-compose configuration file to Prism.
update:
Regard connecting to an existing DB, If you tried to do a prisma deploy on a DB with a different schema it probably gonna blow and complain about the schema conflict, that's why you need introspection for before but as you mentioned in the comment it's only for PostgreSQL at the moment, they are working on creating a tool for MySQL but it's no one knows when it could be out you can check for updates on the open issue with this feature here
hope this can help simplify things a bit.
For learning project you can do choose any solution. You will have fun and pain with MySQL shared between Prisma/ReactNative and WordPress.
For real project, you should avoid sharing one DataBase between different applications. In your case right architect decision is using WPGraphQL instead Prisma. It will provide you GraphQL from the WordPress Plugin.

SpringBoot Redis integration testing

I have an application with MySQL and SpringBoot.
Redis is being used for caching with spring cache annotations.
Now, starting up Redis etc is not an issue as I am using Docker compose to dynamically allocate containers for testing.
But, what is the proper way of verifying that the data is actually being written and read from Redis cache not from Mysql?
You could prevent the client code to communicate with the MySQL database during the part of the integration test where you want to ensure that only Redis is used.
You don't precise the exact way to communicate with the MySQL database, so I cannot give you a specific advise.
But here some ideas :
rely on an MySQL backend service implementation that throws exception as any method is invoked
shutdown the MySQL database
use an empty MySQL database
To test it, I create the record from rest endpoint, then I remove it from the db using spring repository directly. Then, since it is cached, it is should still be accessible by rest endpoint from cache even it is not present in the db.

Replicate mnesia database to Mysql

I have installed ejabberd on an AWS EC2 instance and am using the smack library to connect to it with my android app. At the moment the ejabberd server is using mnesia as the database, however I want to perform some complex queries on some of the data (mainly the MUC room names), as SQL will predominantly the best solution for this, I was wondering if it was possible to replicate the required data to an external MySQL database that I could then query.
Is this possible or am i better looking at a different approach to this problem?
There is no module built into ejabberd to replicate data in Mnesia to MySQL. However, the usual approach is to use the backend you need for each feature. If you want mod_muc to store data into MySQL instead of Mnesia, you can just change the backend to odbc (which means it will store data for that module in a relational database).
You can refer to ejabberd documentation for MUC module: http://docs.ejabberd.im/admin/guide/configuration/#modmuc
Once your MySQL is configured and schema is loaded, you can set db_type to odbc on a case by case basis to choose MySQL for that module.

hibernate change database dynamically for maintenance

I have a java webapp using hibernate over MYSQL db. I need at least an hour of data maintenance daily, hence I need to bring down the db and switch to the backup db.
I don't see an elegant way to switch to another db from my app using hibernate. Is there?
Other totally different ways for database maintenance is welcome.
As you are using a Java WebApp, I assume you are using a container (like Tomcat) or an application server (like JBoss). In both cases, you should be using managed connections by the container, via JNDI. In such case, you can just use JMX (or the admin console) to change the actual datasource, to point to the backup DB, clean the pool and reopen new connections. When you are done, just perform the same steps, pointing the datasource to the actual database.
If you are not using managed connections by the container, you are out of luck. As the Hibernate configuration is static, you'll need to bring down your app, deploy a new version of it with your persistence.xml/hibernate.cfg.xml pointing to the backup, do the maintenance, and deploy the "old" version when you are done.