Jump to content

Connect via WordPress "wpdb" to another database vs "mysqli_connect"


Recommended Posts

Hi, I'm currently using a second "wpdb" object to connect to a MariaDB database.  Google gemini says this is not recommended because:

  • Singleton Pattern: The wpdb object is designed to be a singleton, meaning there's only one instance of it per request. Creating a second instance can disrupt this pattern and lead to unexpected results.
  • Resource Management: WordPress's built-in resource management system is optimized for a single wpdb instance. Creating a second instance can complicate resource management and potentially lead to issues like connection timeouts or memory leaks.
  • Compatibility: WordPress's core functionality and many plugins and themes rely on the wpdb object. Introducing a second instance can interfere with these dependencies and cause compatibility problems.

I don't know what a singleton is so is what Gemini said true? I've seen many examples where they recommend a second wpdb object (in a second variable).

If it is OK, do I need to close this second connection myself?  If so what is the best way?  I'm assuming the open is an expensive operation. 

If I need to use "mysqli_connect", same question about opening/closing.

I'm looking at my use of "wpdb" because of these PHP error messages (and 500 & 503 server responses):

  1. [14-Oct-2024 02:56:44 UTC] PHP Warning:  mysqli_real_connect(): (HY000/1203): User xxxusernamexxx already has more than 'max_user_connections' active connections in /home/yyyy/public_html/wp-includes/class-wpdb.php on line 1982
  2. [14-Oct-2024 03:30:30 UTC] PHP Warning:  mysqli_real_connect(): (HY000/2006): MySQL server has gone away in /home/yyyy/public_html/wp-includes/class-wpdb.php on line 198

Now the website is low on resources and as a NFP organisation (which I volunteer at), getting it better resourced is problematic.  So the above could be due to lack of RAM.  But when it goes bad it is bad for a long time.  I suspect the timing of web crawlers is the main cause if load is the issue and I have have to turn them all off in "robots.txt"...

Thanks for any help :-)

If both databases are on the same server then you only need a single connection (A connection is made to the server, not a specific database. The database name in the connect function is just the default to use).

image.png.f86c10c6e0dd7a17d2ecdb10eef9179a.png

This allows you you access two databases in a single query. Specify the database.tablename when referencing the tables.

Suppose you want to copy tableA from DB1 to DB2...
 

CREATE TABLE DB2.tableA LIKE DB1.tableA;

INSERT INTO DB2.tableA SELECT * FROM DB1.tableA;

If DB1 is the default, it can be omitted EG

INSERT INTO DB2.tableA SELECT * FROM tableA;

 

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.