Creating persistent variable in mysql(5.7) - mysql

Good afternoon,
I am trying to create a variable in MySQL (5.7), that will persist through restarts. It appears that user created variables will not give me the ability to accomplish this so I am looking at the global variables. These appear to be built-in to MySQL and I cannot seem to find a way to create a new persistent variable.
How can I create a global/system variable in MySQL (5.7) that will persist through restarts?

There is no simple way through the SQL interface or configuration to create new system variables -- they are, after all, "system" variables. Any solution to this that comes to mind would be an advanced operation, like one of these:
modify the MySQL server source code to create a new system variable
modify the MySQL server source code to reintegrate the "deprecated" logic from prior versions, where the old variable and new variable behave the same but the old variable throws a warning when you use it
write a MySQL plugin (in C) whose only purpose is to expose a new system variable, basically a dummy variable that doesn't actually do anything, other than having a default value and maybe being (or giving the appearance of being) writable if needed, in order to keep the application happy

Related

Does a MySQL backup save custom functions, events, and queries?

I successfully did a backup of my database, dropped the DB in workbench and created a new one, and attempted to restore it. First, it didn’t restore until I ran the creation of a custom function first (which is something I use in multiple views), and when I did restore it, the event I created wasn’t present.
FUNCTION library.daysout does not exist
I can understand why a query I had open would not be restored (although if you can tell me how to do that, please do,) but how can I make sure my custom function and event restore (especially since the DB doesn’t restore till I run the creation of the function before)?
A default database dump or export does not include stored procedures or stored functions by default.
MySQL Workbench has a checkbox to include stored procedures and functions in an export. You have to check this, and it's not checked by default.
This option is shown in the screenshots in the manual here: https://dev.mysql.com/doc/workbench/en/wb-admin-export-import-management.html Although it isn't described clearly in the text.
Likewise if you use the command-line mysqldump tool, you must use the --routines option to include stored procedure and stored function definitions in the export. This option is documented here: https://dev.mysql.com/doc/refman/8.0/en/mysqldump.html#option_mysqldump_routines

XXHash as a Mysql User Defined Function

We store representations of millions of chemical compounds as BLOBs in a MySQL database. We also keep hashes of these BLOBs when we need to query among these compounds and comparing these hashes in the queries.
Since we found out that standard hash functions(such as CRC) provided by MySQL library collides frequently for our use-case, we used a custom hash function specific to our data, wrapped it as a MySQL plugin and created a User Defined Function with this plugin as below:
CREATE FUNCTION customhash RETURNS INTEGER SONAME 'customhash.so'
Unfortunately, we need to move our MySQL installation to another managed data centre and because of the security reasons & data centre policy, we are not allowed to customize MySQL by adding plugins.
We've recently heard about the XXHash library, we made a few tests on it and we found out it has great performance and it doesn't generate collisions in our data. Also, it turns out it has already been used by MySQL standard distribution internally.
I wonder if it is possible to configure MySQL server to call XXH64_digest function in our MySQL routines without compiling it as a plugin.
I've checked MySQL source code and built-in functions and I could not find any way to run XXHash in MySQL routines. It seems XXHash is used by MySQL internally and it is not user-visible.
In order to run XXHash in MySQL routines, I have developed a plugin in case anyone needs to use XXHash algorithm in MySQL server.
This plug in can be found here: Github repository for xxhash_mysql_plugin.
After installing plug in you can run the xxhash function in your select statements as below:

DataGrip setting SQL_SAFE_UPDATES to ON by default

MySQL Workbench seemed to always have SQL SAFE UPDATES on by default which is really nice when working with our production database.
Is there a way to accomplish this with DataGrip? By default, it looks like SQL_SAFE_UPDATES is OFF by default.
An initialization script or JDBC driver option would work fine.
I believe you can set this via the sessionVariable option within DataGrip.
Go to File > Data Sources and select your connection from the list of Project Data Source.
Go to the Advanced tab which lists a lot of variables.
Locate the the variable named sessionVariables. (Tip: You can search for a particular variable by selecting any variable name, then start typing to seek.)
Set the value for the variable to, SQL_SAFE_UPDATES=1
There is no possibility of that kind in DataGrip for now.
I created a ticket in the tracker, please watch and vote.
https://youtrack.jetbrains.com/issue/DBE-2606

How to Save an Append or Delete Query in MySQL

So I'm moving from MS Access to MySQL:
In MS Access you can store certain INSERT, DELETE, and UPDATE queries as objects alongside your tables. Thus for anyone who don't understand computers that well, they can click on the objects and automatically run the queries to alter the master table for various business functions.
In MySQL, where and how do you store these queries, I seem to be only able to make tables. When I write a piece of code using the SQL editor, I can only save it to a remote location (such as my local desktop) and not onto the MySQL database, where it's accessible for my coworkers.
If you can't save it onto the server, how would I write a piece of code and execute it within the database that would be easily usable by others.
Thanks
The answer to this question is going to depend on your environment, your users, and your bandwidth to support any given solution. You are gaining a lot by making the switch from Access to MySQL, however you are losing some of the the WYSIWYG features. (e.g., Access forms that can bind directly to your data source.)
There are many approaches:
If your users are more advanced, simply having access to the database using MySQL Workbench may suffice. From there they would have access to run views, stored procedures, or to create their own custom queries.
Another option would be to script your objects using Python and provide a simple gui using TkInter. Python is generally thought of as an easy to use language; with built in suppport for MySQL and TkInter is its "default" interface.
Using the LAMP architecture is another largely popular paradigm using MySQL as the backend database.
There is also nothing stopping you from using Access to link to your MySQL db using MySQL as an external data source.
I hope this provides enough info to help you begin whittling down your options.

How does the phpMyAdmin export feature work?

If I were to want to create a PHP function that does the same thing as the Export tab in phpMyAdmin, how could I do it? I don't know if there is a MySQL function that does this or if phpMyAdmin just builds the export file (in SQL that is) manually. Without shell access. Just using PHP.
I tried the documentation for mysqldump, but that seemed to require using the shell. I'm not quite sure what that even is -- maybe my question is: how do you use shell?
My silly idea is to allow non-technical users to build a site on one server (say a localhost) using MySQL then export the site, database and all, to another server (eg. a remote server).
I think I'm pretty clear on the Import process.
You can check the phpMyAdmin source code (an advantage of open-source software). Check the export.php script and the supporting functions in the libraries/export/sql.php script file.
In summary, what phpMyAdmin does is:
get a list of the tables in the given database (SHOW TABLES FROM...),
get the create query for each table (SHOW CREATE TABLE...),
parse it and extract column definitions from it,
get all data (SELECT * FROM...)
build a query according to column data.
I've written similar code for my own apps (for backup purposes, when the GPL license of phpMyAdmin doesn't allow me to use it), however I use DESCRIBE to get column definitions. I think they rather parse the SHOW CREATE TABLE output because contains more information than DESCRIBE output.
This way to generate SQL sentences requires a bit of care to handle the escaping but it allows for some flexibility, as you can convert types, filter or sanitize data, etc. It is also a lot slower than using a tool like mysqldump and you should take care of not consuming all available memory (write soon, write often, don't keep everything in memory).
If you will implement a migration process (from server to server) maybe it would be easier to do it with some shell scripting and calling mysqldump directly, unless you will do everything with PHP.