mysql udf read my.cnf - mysql

I'm trying to write a MySQL UDF (User Definied Function), which should read the configuration file of MySQL - my.cnf -, or access MySQL session and status vars.
How do I do that ?
I'm sure, there are functions implemented in MySQL source code - somewhere ... for this functionality.
How do I find them?
Also, is there a good MySQL source API documentation?
Thanks,
krisy

The easiest solution I found, was starting MySQL from a script, which script contains commands, to set enviromental variables, and accesss these variables througg getenv() function from UDF.
If anyone has a better solution, I'm interested very much :-)

Related

How can I use system variable in mysql

How can I use system(operating system) variables like JAVA_HOME In my own mysql stored procedure.
Guess you may need to use an external library such as UDF Repository for MySQL to do this. A reference to the library can be found here. There should be a sys_get() function call that may serve the purpose.

How to run and link a .sql file from Perl

I am looking for a way to implement MySQL and Perl to make a program. Where I'm lost is that I have a .sql file, that creates 3 tables for the Perl program to use. How do you:
1) Execute the file fileName.sql in Perl to create the tables
2) Link those created tables into manipulable variables in Perl Program (like an example being able to add a user to one of the tables)
Execute the file fileName.sql in Perl to create the tables
Usually you would set up the database in advance and use the command line mysql client or a GUI such as PHPMyAdmin to load the .sql file.
You could use a call to system to do the former though.
Link those created tables into manipulable variables in Perl Program
Low level access to databases in Perl is usually handled via the DBI module.
Getting something along the lines of a variable per table calls for an ORM. DBIx::Class is a popular choice for this.
In Perl you use the DBI database interface. In your case, you will also be using something like the DBD::MySQL driver.
There is lots of help available on this topic (including lots of questions on this site).
As for the specific question of your .sql file, there are a few approaches you could take, depending on how fancy you want to get:
You could just copy and paste the commands into your program as you write it.
You could execute an external program that will run the .sql file (for example, by using system()).
You could programmatically read in the .sql file and send the commands from within your program. A module could help you with this (I found SQL::Script on CPAN, which looks useful, though I don't have any experience with it).
I suggest you pick an approach, try it, and ask if you have any specific problems.

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.

How to include a .sh (script) file in Mysql stored procedure?

Is it possible to include a Script file (.sh file) inside a stored procedure in Mysql?
If yes, then how?
And if no, then why?
Like #mu is too short said:
You need to install these UDF's from: http://www.mysqludf.org/lib_mysqludf_sys/index.php
Here's the download link: http://www.mysqludf.org/lib_mysqludf_sys/lib_mysqludf_sys_0.0.3.tar.gz
Note that you shell commands will run with the privileges of the MySQL user, not with the privileges of the user that's connecting to MySQL.
For a security point of view this is probably a bad idea.
Note the warning in the link:
A Note of Caution
Be very careful in deciding whether you need this function. UDFs are available to all database users - you cannot grant EXECUTE privileges for them. As the commandstring passed to sys_exec can do pretty much everything, exposing the function poses a very real security hazard.
Even for a benign user, it is possible to accidentally do a lot of damage with it. The call will be executed with the privileges of the os user that runs MySQL, so it is entirely feasible to delete MySQL's data directory, or worse.
The function is intended for specialized MySQL applications where one needs extended control over the operating system. Currently, we do not have UDF's for ftp, email and http, and this function can be used to implement such functionality in case it is really necessary (datawarehouse staging areas could be a case in example).
You have been warned! If you don't see the hazard, please don't try to find it; just trust me on this.
If you do decide to use this library in a production environment, make sure that only specific commands can be run and file access is limited by using AppArmor.

How do I start a program from VB.Net and pass it arguments?

Is possible? I plan to backup mysql database using vb.net. If you know any beginner tutorial on how to do this, then please let me know.
If I understand the question correctly, you want to start mysql.exe from VB.Net and supply some command-line parameters to tell it what to do (in your case, do a database backup). (FWIW, you might be better off using mysqldump.exe for that.)
You can start any process via the System.Diagnostics.Process.Start method. You'll probably want either this variant that gives you full control over the startup, or this one that just lets you specify the process (EXE) name and arguments. Here's a page with lots of examples. They're in C#, but it shows using these methods; the syntactic differences don't (I suspect) get in the way much.
Off-the-cuff example:
Imports System.Diagnostics
Process.Start("mysqldump.exe", "myDatabaseName")
' Or include the path:
Process.Start("C:\Program Files\MySQL\MySQL Server\bin\mysqldump.exe", "myDatabaseName")