How to see the create time of a MySql user? - mysql

How to see the create time of a MySql user?
I want to know the create time of a MySql user called "local".
Therefore, I have queried the mysql.user table, but that does not display the creation time for the user. Furthremore, there are no MySql logs in /var/log.
I need the information for doing forensics analysis on the system.
Is there any way to obtain the create time for MySql users?

You would query the information_schema for the create_time of the table.
For instance:
SELECT create_time FROM INFORMATION_SCHEMA.TABLES
WHERE table_schema = 'your_schema'
AND table_name = 'your_table'
Reference: http://dev.mysql.com/doc/refman/5.0/en/tables-table.html

If you want to get the user creation date then you can add a timestamp column in mysql.user table and set it to CURRENT_TIMESTAMP as default. So next time when a user is created, you will get the timestamp.

Related

MySQL information_schema.tables UPDATE_TIME refresh on specific column change with ANALYZE TABLE

Is it possible to update the INFORMATION_SCHEMA.TABLES UPDATE_TIME for a specific table when a specific column is changed in this table?
I know that MySQL saves its cache to the TABLES on a timeout specified in the information_schema_stats_expiry variable and the timeout is 24h by default which is ok for me. Also I know I can query ANALYZE TABLE 'table_name_1', ... 'table_name_N' to force MySQL update the TABLES. But it will cause the update with no conditions. So is it possible to call ANALYZE TABLE only for specific columns? Thanks

How to get the user ID from MySQL/Maria DB?

In PostgreSQL, for instance, the query SELECT * FROM pg_user return the usesysid column in which you can see the users IDs. So I can get the oldest/newest user from this table through this ID.
How can I do the same in Mysql and Maria DB?
I am surprised that there is no field that shows the creation date of each user in any driver.
Listing the users should be as simple as:
select * from mysql.user
Column user give you the user id. Combined with host, this is the unique identifier of each account.
Documentation for MariaDB.
Documentation for MySQL.
Please note, however, that MySQL / MariaDB do not track the date when users are created. A solution is to add a custom column to the users table that defaults to the current date:
ALTER TABLE mysql.users ADD date_created TIMESTAMP DEFAULT CURRENT_TIMESTAMP;
Starting with MariaDB 10.4.1 the source for users and privileges is mysql.global_privs see MariaDB Docu.
Users, that are authorized by password have a property password_last_changed.
That's not the creation date you are looking for but can be a hint for the age of users entries, i.e.:
| localhost | phpmyadmin | {"access":0,"version_id":100515,"plugin":"mysql_native_password","authentication_string":"xxxx","password_last_changed":1660639196} |

Get all record from table till last updation in table

I want all records from table till I updated last time that table I am trying update_time of information_schema.tables but unable to understand how to use. Is there any way to do it Please suggest me.
Note : I don't have createdate column in my table that's why I am trying from information_schema update_time
For Example : I have a table tblstock and I want to fetch stock for condition (where date <= "searching date for stock" ) but i don't have createdate column that update after every entry in table that's why I am facing problem to get all record from that table for this condition.
If you have not a column to store the update time, you can't get what you want in this case.
Even you get the UPDATE_TIME from INFORMATION_SCHEMA.TABLES, you still can't know update/insert datetime for each rows in table.
You need to store the update_time in your own table.
Get the last update time of your table through information_schema
SELECT UPDATE_TIME
FROM information_schema.tables
WHERE TABLE_SCHEMA = 'dbname'
AND TABLE_NAME = 'tabname'

Go get last access time of a table in mysql

Is there any way to know the time when last select statement has been performed on a table? I am using the InnoDB storage engine.
I have tried with the following query:
select update_time,table_name from information_schema.tables where table_schema='databasename';
..but I'm receiving NULL in the update_time column.
Unless you manually update a last_accessed-field on the table, my best bet would be to add query logging and parse the log-files.
I googled and found these relates questions:
When was the last time a mysql table was accessed?
How do you get the last access (and/or write) time of a MySQL database?
SELECT UPDATE_TIME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'dbname'
AND TABLE_NAME = 'tabname'`

information_schema tables and update_time behaviour

I'd like to have some explanation about information_schema and its behaviours.
Let's say I want to know when a myisam table has been modified.
I write this query
select update_time from information_schema.tables
where table_schema = 'my_db' and table_name = 'my_table'
Even though I apply some change to my table nothing happens in it until I run a flush tables.
Unluckily it seems to me that update_time stores date and time of the moment that I run flush tables, not the one when table changes really occur. Is it true?
Thanks in advance.
I personally prefer to add a last_change timestamp to each of my tables, then define it as "DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP", which means that mysql will automatically update it each time the row is modified.
With this, you could just query the table directly and know not only that the table was touched, but also which rows were touched.
It doesn't work to detect deleted rows though.