[SOLVED] Is there workstationID and host_name() equilvalents for MYSQL
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Yup, that gives me my hostname, but I can't set it. It's read-only.
Is there a way to query for my connection string?
also, I can set a variable after connecting from a java program:
set @myvar = 'something';
Then I can query it:
select @myvar;
And I get the set value. If I connect again later, but don't set the variable, the value is null. That tells me it persists for the duration of the connection. But, will that be set for my session only (hopefully)? I don't want someone else who happens to connect at the same to to get the wrong value, or set it themselves to something different.
In other words, is setting a variable like this global or local to the session?
Yup, that gives me my hostname, but I can't set it. It's read-only.
Is there a way to query for my connection string?
I don't get it, why whould you want to change the server hostname once connected, also what do you mean with connection string, user name, password, hostname, database?
A connection string is used by any program, be it visual basic, java, etc., wanting to connect to a database instance, whether SQL Server, MYSQL, etc. The string, for MYSQL, is of the form:
For SQL server, one of those attributes is workstationID. Normally, this does contain the hostname.
My application is a web server with a MYSQL backend. In this case, the workstation is always the same: the hostname of the web server. I want to override that with the ID of the logged in user. I'm using this information, as NevemTeve guessed, to log who made what changes to database tables from their browser. In SQL Server, I can retrieve this override setting in a query, stored procedure or trigger using the SQL Server function host_name().
None of that works in MYSQL. There is no host_name() function and the @@hostname variable is read only.
I have, however, figured out a solution for MYSQL. As I wrote in my post of 06-17-16, 05:48 PM, I can set my own variable simply by:
set @webuser = 'someUserId';
This can then be retrieve via `select @webuser` in any query, stored procedure or, most importantly for my case, triggers. If not set, it returns null. The variable persists during the connection and is gone upon disconnection. I am assuming this variable is local to the connected session and not global to all concurrent connections, but I've no easy way of testing that.
If any of you know for sure about the scope of such variables, please let me know. For the time being, this mechanism works.
User-defined variables are session-specific. A user variable defined by one client cannot be seen or used by other clients. (Exception: A user with access to the Performance Schema user_variables_by_thread table can see all user variables for all sessions.) All variables for a given client session are automatically freed when that client exits.
I'm using this information, as NevemTeve guessed, to log who made what changes to database tables from their browser. In SQL Server, I can retrieve this override setting in a query, stored procedure or trigger using the SQL Server function host_name().
keefaz, workbench looks like a great tool and I'll keep it in mind. In my case, the web user logs in via a web page and the ID and password entered are compared with values retrieved from the database. Tools like workbench would have no knowledge of internal java session values. I use this userId in an trigger attached to tables so I can log the userId, time and column changed into a changelog table.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.