Enable MySQL syslog to log aborted connections and access-denied - mysql

What is the command in mysql that is required to that I can implement aborted connections and access-denied logs to be written to the syslog?

The commands are first to see what your settings are:
select ##general_log; -- a 1 indicates it is turned on for capture
select ##general_log_file; -- the file that it logs to
select ##datadir; -- directory location where the log lives
To turn logging on for the General Query Log, use the following:
SET GLOBAL general_log = 'ON'; -- 0 is off, 1 is on
Note that the datadir is read-only and requires a restart of the server after changes. Basically, don't change this. It is the home of your database schemas.
For expanded connection failures perform a
select ##log_warnings; -- make a note of your prior setting
set global log_warnings=2; -- setting above 1 increases output
The immediate above relates to the Error log written out in the same datadir.
See the Percona article Auditing login attempts in MySQL and my prior answer Here.

Related

Mysql Event Not Working

I have added the following simple test event on my mysql database via phpmyadmin:
CREATE DEFINER=`root`#`localhost` EVENT `my_event`
ON SCHEDULE EVERY 1 MINUTE STARTS '2013-05-27 00:00:00'
ON COMPLETION NOT PRESERVE ENABLE DO
BEGIN
UPDATE `test` SET `name`="z";
END
My environment is mac + MAMP Pro. I am expecting to change all rows on my 'test' table with name 'z' within a minute. But not happening so.
Do I have to something additional to get my events start working?
Output of "SHOW PROCESSLIST":
Thanks.
Events are run by the scheduler, which is not started by default.
Using SHOW PROCESSLIST is possible to check whether it is started. If not, run the command
SET GLOBAL event_scheduler = ON;
to run it.
Verify if the event_scheduler is On - execute the following command:
SHOW PROCESSLIST;
It'll output a table/entries, you must look for an entry with User event_scheduler, and Command Daemon:
Id User Host db Command Time State Info
22870 event_scheduler localhost \N Daemon 23 Waiting for next activation \N
OR, you can also verify using the following command:
SELECT ##global.event_scheduler;
The result should be ON, otherwise set it off (will get 0 for the command), as stated in the next section.
If you don't have any such entry (as above), you may start the event scheduler using the following command:
SET GLOBAL event_scheduler = ON;
Once done, you can verify if it has been executed properly using the SHOW PROCESSLIST command, as mentioned above.
For those wondering how to enable it by default at startup, add the following to your config file (my.ini, my.cnf):
#Event scheduler can be set to 1 (On), 0 (Off), or Disabled
event_scheduler=1
Restart of the service is required in this case, so if you want minimal disruption, add this to the config file, and then run the SQL:
SET GLOBAL event_scheduler = ON;
That way, it will run for the current process, and if the server is restarted it will still work.
Note that this doesn't work if the event_scheduler was set to disabled. In that case the only option is to restart the service.
If you want your event_scheduler to startup automatically every time mysql server restarts, anywhere under the [mysqld] section of the my.ini or my.cnf file that you find in /etc/mysql you should place
[mysqld]
# turning on event_scheduler
event_scheduler=ON
restart mysql to check if it is running (in command line terminal!)
sudo service mysql restart
then check your processlist
SHOW PROCESSLIST
you can check if your events are running by checking the last time they ran
SELECT * FROM INFORMATION_SCHEMA.events
Temporal
SET GLOBAL event_scheduler = ON;
Will not work if event_scheduler is explicitly DISABLED, see the method below
Permanent (needs restart)
In your config file (In Ubuntu it's /etc/mysql/mysql.cnf):
[mysqld]
event_scheduler = ON
Notes:
The event_scheduler variable can have this possible states:
OFF (or 0) (default)
ON (or 1)
DISABLED: you cannot use the temporal enabling, you can only change state through the config file and restarting the server
WARNING: Keywords ON / OFF are preferred over their numerical equivalents.
And in fact Mysql Workbench doesn't recognize the configuration event_scheduler=1, it shows as OFF in the Options File section.
Tested in Ubuntu with Mysql Workbench 8.0.17 and Mysql Server 5.7.27
Although ON and OFF have numeric equivalents, the value
displayed for event_scheduler by SELECT or SHOW VARIABLES is always
one of OFF, ON, or DISABLED. DISABLED has no numeric
equivalent. For this reason, ON and OFF are usually preferred
over 1 and 0 when setting this variable.
Source: https://dev.mysql.com/doc/refman/5.7/en/events-configuration.html
I just figured out that on MariaDB, after adding an event (in my case, it was the first one), you have to restart the event-scheduler
SET GLOBAL event_scheduler = OFF;
and then
SET GLOBAL event_scheduler = ON;
to make it actually bring the scheduler into "waiting for activation"-state.
I would just like to add to this thread. I dumped my database to another server and as a result the definer of my event had no such grant defined for the user. I updated my definer with
ALTER DEFINER='root'#'localhost' EVENT event.name COMMENT '';
Make sure your definer has the correct PRIVILEGES.
Remember to add in 'Commit', after 'DO BEGIN' or 'DO'. Works for me after that.
Try
SET GLOBAL event_scheduler = ON;
DELIMITER $$
CREATE DEFINER=`root`#`db01` EVENT `PRICEALERT_STATUS`
ON SCHEDULE EVERY 1 DAY STARTS TIMESTAMP(CURRENT_DATE)
DO BEGIN
// Your Query
END $$
DELIMITER ;

MYSQL - general log file

Is here a way to check if there is already a log file where mysql is writing general logs?
I'm trying to start my sql with command: --general-log-file="" to see what kind of queries are being sent to the server but that command is not working.
check for log_output
If the value is NONE log entries are not written even if the logs are enabled. If the logs are not enabled, no logging occurs even if the value of log_output is not NONE.
if it is not None then check for log file name using this file_name

How to find mysql DB is slave?

How to find mysql DB is slave with out using "show slave status" by query?
Here are 3 options you have to detect if Replication is running
OPTION #1 : Check Status Variable 'Slave_running'
Using MySQL 5.1/5.5
select variable_value from information_schema.global_status
where variable_name = 'Slave_running';
Using MySQL 5.0 and back
SHOW VARIABLES LIKE 'Slave_running';
OPTION #2 : Check the Process List
Using MySQL 5.1+/5.5
select COUNT(1) SlaveThreads
from information_schema.processlist
where user = 'system user';
If SlaveThreads = 2, Replication is Running
If SlaveThreads = 1, Replication is Broken
If SlaveThreads = 0, Replication is Stopped or Disabled
Using MySQL 5.0 and back
SHOW PROCESSLIST;
Look for 2 DB Conenctions thaty have 'system user' in the user column.
OPTION #3 : Check for presence of master.info
If replication is setup on a DB Server, look for master.info. By default, master.info is usually in /var/lib/mysql or wherever datadir is defined.
Simply run 'cat master.info' multiple times (For Windows community, type master.info). If the log position is moving, replication is on. If the log position is not moving, it could mean that replication is either broken (SQL Error in SQL Thread), stopped (due to STOP SLAVE;), or disabled (by running CHANGE MASTER TO MASTER_HOST='';).
According to MySQL doc - Checking Replication Status:
Slave_IO_Running: Whether the I/O
thread for reading the master's binary
log is running. Normally, you want
this to be Yes unless you have not yet
started replication or have explicitly
stopped it with STOP SLAVE.
Slave_SQL_Running: Whether the SQL
thread for executing events in the
relay log is running. As with the I/O
thread, this should normally be Yes.
Prior to MySQL 5.7, you can check the 'slave_running' variable by executing the following query:

 SHOW GLOBAL STATUS LIKE 'slave_running';
Since MySQL 5.7, the slave_running has been removed and the above query returns an empty set
You can enable "show_compatibility_56" to get the value but “show_compatibility_56” is deprecated and will be removed soon. The reason for this is because MySQL is moving away from the information_schema GLOBAL_STATUS and SESSION_STATUS tables in preference for performance_schema.
The correct way to get the status of the slave running in MySQL 5.7 outside of SHOW SLAVE STATUS is to use the new replication-based performance_schema tables.
You can execute the following query to get the status of the replication service:

SELECT SERVICE_STATE FROM performance_schema.replication_connection_status;

MySQL slave I/O thread not running

I have set up replication for MySQL server. I can connect from the slave machine to the master server using the replication user/password. I have got the slave SQL thread running, but the slave I/O thread is not running and the slave I/O status comes as empty when checked using 'show slave status'. What could be the problem?
How do I solve this? Restarting the slave does not help.
This was my bad: Instead of giving a 'replication slave' privilege to *.*, I was only giving it for my_db.*.
Instead of giving a 'replication
slave' privilege to ., I was only
giving it for my_db.*.
Replication slave is only a global privilege (i.e. per-user only), this means that a command such as
GRANT REPLICATION SLAVE on mydb.* TO 'someuser'#'%';
has no effect as you can't grant it per-database/column/table.
The command you need to run is:
GRANT REPLICATION SLAVE on *.* TO 'someuser'#'%';
Then do a START SLAVE. You might also find it useful to look in the mysql error log.
I'd suggest a good read of the replication setup documentation, as it explains all of this in detail.
I have encountered the same problem and I try this steps
First add this code somewhere below [mysqld] in my.cnf or my.ini slave-skip-errors=1046
this will skip all duplicate entry since we will execute the whole binary log file where the replication stop, you may comment this code after successful replication.
1.STOP SLAVE;
2.RESET SLAVE;
3.CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000049';
Note: MASTER_LOG_FILE must be the last file where it stop from replicating
4.CHANGE MASTER TO MASTER_LOG_POS=98;
5.START SLAVE;
check if you are successful
I faced same issue and fixed using following steps. Complete thread link is http://www.percona.com/forums/questions-discussions/percona-xtrabackup/11842-backup-stopped-working-slave-sql-running-no
Steps are same as mentioned by #Luxknight007 except his step 2. However this thread contains more detail which is very helpful. Following is solution which i used and it worked.
"The first issue is that you changed the replication position instead of fixing the error, and used an incorrect binlog file name format (you likely just used the one from that post you linked I'd guess). To get back to where you started, you need to find the binlog file and position that the slave sql_thread stopped at. Based on your slave status output, it looks like the slave is reading from a new binlog file (you can see that the Read_Master_Log_Pos value is smaller than the Exec_Master_Log_Pos value, which means it has to be reading a newer binlog file than where the slave sql_thread stopped at), so you need to find the binlog file that the slave sql_thread actually failed on. So look in the error log for something like the below:
Code:
2013-10-08 12:48:51 37545 [ERROR] Slave SQL: Error 'Table 'testdb.test2' doesn't exist' on query. Default database: 'testdb'. Query: 'insert into test1 select * from test2', Error_code: 1146
2013-10-08 12:48:51 37545 [Warning] Slave: Table 'testdb.test2' doesn't exist Error_code: 1146
2013-10-08 12:48:51 37545 [ERROR] Error running query, slave SQL thread aborted. Fix the problem, and restart the slave SQL thread with "SLAVE START". We stopped at log 'mysql-bin.000001' position 3427
This is a sample I re-created, so yours will be a bit different. Note the ERROR is similar to what you see in your slave status. So find your specific error message in the error log file, and then locate the end part where is gives you the file name and position ("We stopped at log 'mysql-bin.000001' position 3427" in my example). The position should be 315098143 based on your show slave status, as that is when it the slave sql_thread stopped executing events (Exec_Master_Log_Pos ) but the io_thread kept reading in new ones (Read_Master_Log_Pos).
Once you find the correct binlog file name and position, re-run your change master statement on your slave using the information you located in the error log. Note that your file name should be something like "newcrmdb1-bin.XXXXXX", not mysql-bin.XXXXXX (you can see this naming convention your show slave status above).
Code:
mysql> change master to MASTER_LOG_FILE='newcrmdb1-bin.XXXXXX', Master_Log_Pos=315098143;
change master to MASTER_LOG_FILE='mysql-bin.000082' , Master_Log_Pos=47914844;
Once you get pointed back to the original replication location where the slave sql_thread failed, you need to then fix the error that it was complaining about to start with.
The initial replication error appears to be telling you that the table asteriskcdr.bpleadcf does not exist on the slave, so the insert statement is failing when it attempts to select the data from that table. So the problem there is that your slave appears to be already out of sync with your master. If the table in question on the master is static or mostly static, you could likely solve this by exporting the data from just that table on the master using mysqldump and loading it into the slave. If that is not possible, or you do not care about that data, you could always just skip the replication statement with sql_slave_skip_counter, but then the slave would be further out of sync with the master.
And if all else fails, you can always rebuild the slave from the master as a last resort as well. =)"

Disable MySQL general logging without restarting?

Can I disable general logging completely without restarting the server?
Because, per the documentation:
SET sql_log_bin = {0|1}
Disables or enables binary logging for the current connection (sql_log_bin is a session variable) if the client has the SUPER privilege. The statement is refused with an error if the client does not have that privilege.
Can I enable/disable general log without restarting MySQL?
For anyone using 5.1 now, you can use these commands (had to look them up and this Q&A came up)
SET GLOBAL log_output='TABLE'; #or FILE
SET GLOBAL general_log='OFF'; #or ON
SET GLOBAL slow_query_log='ON'; #or OFF
TABLE will store them in the mysql.general_log and mysql.slow_log tables instead of files which is nice in development for reviewing and truncating.
For MySQL 5.0 "The session sql_log_off variable can be set to ON or OFF to disable or enable general query logging for the current connection." [MySQL Doc, Log file]