LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-17-2016, 11:43 AM   #1
mfoley
Senior Member
 
Registered: Oct 2008
Location: Columbus, Ohio USA
Distribution: Slackware
Posts: 2,555

Rep: Reputation: 177Reputation: 177
Is there workstationID and host_name() equilvalents for MYSQL


In MS SQL Server, I can set workstationId in the connection string and, in queries and procedures, retrieve that value via the host_name() function.

I've tried setting workstationId in mysql, but there is no host_name() function. The only thing I can find in MYSQL is:

select variable_value from information_schema.global_variables where variable_name = 'hostname'

which indeed returns the hostname, but not my intended override.

Any way to do what I want?
 
Old 06-17-2016, 12:54 PM   #2
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
n/m.
 
Old 06-17-2016, 02:57 PM   #3
mfoley
Senior Member
 
Registered: Oct 2008
Location: Columbus, Ohio USA
Distribution: Slackware
Posts: 2,555

Original Poster
Rep: Reputation: 177Reputation: 177
Sorry, don't understand "n/m"
 
1 members found this post helpful.
Old 06-17-2016, 03:48 PM   #4
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
There is a mysql hostname system variable also
Code:
select @@hostname;
http://dev.mysql.com/doc/refman/5.7/...variables.html
 
Old 06-17-2016, 04:48 PM   #5
mfoley
Senior Member
 
Registered: Oct 2008
Location: Columbus, Ohio USA
Distribution: Slackware
Posts: 2,555

Original Poster
Rep: Reputation: 177Reputation: 177
Quote:
Originally Posted by keefaz View Post
There is a mysql hostname system variable also
Code:
select @@hostname;
http://dev.mysql.com/doc/refman/5.7/...variables.html
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?

Last edited by mfoley; 06-17-2016 at 05:00 PM.
 
Old 06-18-2016, 12:04 AM   #6
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,863
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
These variables are memory-only objects. Use tables for shared/permanent data.
 
Old 06-18-2016, 10:53 AM   #7
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Quote:
Originally Posted by mfoley View Post
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?
 
Old 06-18-2016, 11:23 AM   #8
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,863
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Guess he wants to record the user's (client's) metadata for later analysis. (Eg client computer name/IP, client process's username/userid (cf identd))
 
Old 06-19-2016, 11:41 PM   #9
mfoley
Senior Member
 
Registered: Oct 2008
Location: Columbus, Ohio USA
Distribution: Slackware
Posts: 2,555

Original Poster
Rep: Reputation: 177Reputation: 177
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:

jdbc:mysql://localhost/members?user=username&password=pw

Where "members" is the db_name. There are many, many additional attributes for a connection string: https://www.connectionstrings.com/mysql/

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.

Last edited by mfoley; 06-19-2016 at 11:45 PM.
 
Old 06-20-2016, 06:20 AM   #10
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 447Reputation: 447Reputation: 447Reputation: 447Reputation: 447
You are correct. Here's a link:

http://dev.mysql.com/doc/refman/5.7/...variables.html

Quote:
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.
 
Old 06-20-2016, 10:22 AM   #11
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Quote:
Originally Posted by mfoley View Post
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().
Something like workbench maybe?
https://dev.mysql.com/doc/workbench/...nnections.html

I would also look at performance schema feature
http://dev.mysql.com/doc/refman/5.7/...ce-schema.html
 
Old 07-03-2016, 12:35 PM   #12
mfoley
Senior Member
 
Registered: Oct 2008
Location: Columbus, Ohio USA
Distribution: Slackware
Posts: 2,555

Original Poster
Rep: Reputation: 177Reputation: 177
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.
 
  


Reply

Tags
mysql



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Squid: adding unique host_name to each tcp_outgoing_address cybersysop Linux - Software 2 10-02-2012 11:56 AM
host host_name do not retrive IP - how to set a loopback? hfelix Linux - Newbie 15 07-25-2012 05:48 AM
mysql error Can't connect to local MySQL server through socket '/var/lib/mysql/mysql. SpellChainz Linux - Newbie 1 06-23-2007 03:35 PM
mysql error Can't connect to local MySQL server through socket '/var/lib/mysql/mysql. Dannux Linux - Software 3 03-24-2006 08:44 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:23 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration