Showing posts with label mysql. Show all posts
Showing posts with label mysql. Show all posts

Friday, February 03, 2023

MYSQL Error: Lock wait timeout exceeded; try restarting transaction

I have not seen this error in a while with MySQL replication, " Lock wait timeout exceeded; try restarting transaction"

Check the slave status

 show slave status \G; and noticed the following.

Slave_IO_Running: Yes

  Slave_SQL_Running: No 

 Slave_IO_Running: Yes

  Slave_SQL_Running: No

   Last_SQL_Errno: 1205

   Last_SQL_Error: Lock wait timeout exceeded; try restarting transaction


Solution

Stopped the Slave

stop slave;

Restarted the Slave

start slave;

Check the status and all is well


References


Monday, January 21, 2019

MYSQL and New IP for Master in Replication

There comes a time you might need to change the IP of a Master in a MySQL Replication setup. The steps are fairly simple.

You will need to know the IP of the Master.

Important!
When you’re using CHANGE MASTER TO to set start position for the slave you’re specifying the position for SQL thread and so you should use Relay_Master_Log_File:Exec_Master_Log_Pos. 
Otherwise, you’re going to ruin your replication.

SSH into the MYSQL SLAVE and run SHOW SLAVE STATUS
Slave_IO_State: Reconnecting after a failed master event read
Master_Host: 10.0.0.1
Master_User: replicate
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: binlog.002933
Read_Master_Log_Pos: 832187423
Relay_Log_File: mysql-relay-bin.000230
Relay_Log_Pos: 832187707
Relay_Master_Log_File: binlog.002933
Slave_IO_Running: Connecting
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 832187423
Relay_Log_Space: 832188044
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: NULL
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 2003
Last_IO_Error: error reconnecting to master 'replicate@10.0.0.1:3306' - retry-time: 60  maximum-retries: 86400  message: Can't connect to MySQL server on '10.0.0.1' (110 "Connection timed out")
Last_SQL_Errno: 0
 Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
Master_SSL_Crl:
Master_SSL_Crlpath:
Using_Gtid: No
Gtid_IO_Pos:
Look for the values

  • Read_Master_Log_Pos:
  • Exec_Master_Log_Pos:
These should be the same, note them and note Relay_Master_Log_File

  • Exec_Master_Log_Pos: 832187423
  • Relay_Master_Log_File: binlog.002933
SSH into SLAVE and run SHOW SLAVE STATUS
STOP SLAVE
CHANGE MASTER TO MASTER_HOST='xxx.xxx.xxx.xxx', MASTER_LOG_FILE='binlog.002933', MASTER_LOG_POS=832187423;
START SLAVE
SHOW SLAVE STATUS
Reference

Friday, June 16, 2017

Sleeping MySQL threads

Saw this interesting script on how to deal with sleeping MySQL connections. From the beginning I would say this should only be used as a temporary solution until you can fix the real issue.

I have modified the original script somewhat.

$link = @ mysql_connect('localhost', 'xxxx', 'xxxxxxxx');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully'."\n";

$result = mysql_query("SHOW processlist");
while ($myrow = mysql_fetch_assoc($result)) {
if ($myrow['Command'] == "Sleep" && $myrow['Time']>0 && $myrow['User']!="root") {
//if ($myrow['Command'] == "Sleep" && $myrow['Time']>0) {
//mysql_query("KILL {$myrow['Id']}");
echo  $myrow['User']." ".$myrow['Command']." ".$myrow['Time']." ".$myrow['State']."\n";
if($myrow['Time']>20){
//mysql_query("KILL {$myrow['Id']}");
echo "Killed process id: ". $myrow['Id']."\n";
}
}
}
mysql_close($link);
?>
For starters you can see all the sleeping MySQL processes by running the command
show processlist
You will need to look out for all the threads with state being 'sleep. MySQL 5.1.7 allows filtering  For e.g. you can find  sleeping processes running over 5 seconds using the command below.
SELECT user, time, state, info FROM information_schema.processlist WHERE command = 'Sleep' AND time >5 ORDER BY time DESC, id;

What causes

Most sleeping MySQL connections are caused by queries initiated client side that not be properly closed on the clients side. Most times these are cleaned up by wait_timeout variable. Sometimes depending on the query a sleeping connection can lock up a table (MyISAM) or row(InnoDB) and lead to problems especially to the timeouts are high and the number of connections waiting on the locked resource,

References



Monday, January 18, 2016

MySQL Replication and slave_net_timeout

In an article written in 2009, Jeremy Zawodny took on the MYSQL default for slave_net_timeout among other settings. Daniel Schneller, author of MySQL Admin Cookbook,  had written about his experience in 2006.

What is slave_net_timeout

It is the time in seconds for the slave to wait for more data from the master before considering the connection broken, after which it will abort the read and attempt to reconnect. The retry interval is determined by the MASTER_CONNECT_RETRY open for the CHANGE MASTER statement, while the maximum number of re-connection attempts is set by the master-retry-count variable. The first reconnect attempt takes place immediately.

For installations before MySQL 5.7 the default value for slave_net_timeout is 3600 seconds or 1 hour but as of  MySQL 5.7 it is 60 seconds. MariaDB still has 3600 seconds as the default.

The problem is summed up in the following statement.
When the network connection between a master and slave database is interrupted in a way that neither side can detect (like a firewall or routing change), you must wait until slave_net_timeout seconds have passed before the slave realizes that something is wrong. It will then try to reconnect to the master and pick up where it left off. This is bad. This could be 1 hour if the slave_net_timeout is set to 1 hour (3600 seconds)
I discovered this issue when I noticed the slave was not up to date but there were no errors. However if the slave was stopped (STOP SLAVE) and started (START SLAVE), suddenly replication started again and everything was up to date.

Before the restart the following would be noted. The  Read_Master_Log_Pos and Exec_Master_Log_Pos values did not match the log position (Show Master Status) on the master server.  On the slave, the Slave_IO_State is "Waiting for master to send event",  the Slave_IO_Running and Slave_SQL_Running values are both are "Yes". The Master_Log_File and Relay_Master_Log_File matched. In essence don't trust "Show Slave Status" alone.

The solution is to lower slave_net_timeout  to a more reasonable value 60 - 300 seconds.

Other values worth looking include:
  • skip-name-resolve -  enable and use IPs only in Grants
  • connect_timeout - Set higher than default
  • max_connect_errors - Set to High value
  • interactive_timeout - set to 300 seconds. Just lower that 28800 seconds
  • wait_timeout  - set to 300 seconds. Just lower that 28800 seconds
The last two have been of concern to Eliot Kristan since 2006.

As usual make one configuration change (or related changes only)  at a time in order to monitor and evaluate the effectiveness of the change.

References
  1. Fixing Poor MySQL Default Configuration Values
  2. Eliot Kristan - MySQL wait_timeout default is set too high!
  3. Some Reasonable Defaults for MySQL Settings 
  4. MySQL replication timeout trap
  5. MariaDB- Replication and Binary Log Server System Variables
  6. MySQL -  Replication Slave Options and Variables
  7. MySQL lowering wait_timeout value to lower number of open connections
  8. Changing MASTER_CONNECT_RETRY - Anything pit falls to keep in mind? 
  9. MySQL replication hung after slave goes offline and comes back online again

Sunday, April 26, 2015

Got fatal error 1236 from master when reading data from binary log

Today I encountered a MySQL replication error but thanks to Percona's Muhammad Irfan excellent blog the solution was quick and easy. While the error occurred with a MariaDB setup the MySQL instructions applied.

The error

Slave_IO_Running: No
Got fatal error 1236 from master when reading data from binary log: 'Client requested master to start replication from impossible position; the first event 'binlog.000202' at 307396531, the last event read from 'binlog.000202' at 4, the last byte read from 'binlog.000202' at 4.', Internal MariaDB error code: 1236 
It appear there was some issue on the server hosting the MariaDB and the server administrator opted to reboot. When the server restarted, replication stopped working.

Solution

On the Master
Firstly, on master, confirm that it is the end of the binary log. Browse to the directory

cd /var/lib/mysql (location will vary depending on your installation)
mysqlbinlog --base64-output=decode-rows --verbose --verbose --start-position=307396531 binlog.000202
I also peaked on the next log binlog.000203
mysqlbinlog --base64-output=decode-rows --verbose --verbose --start-position=0 binlog.000203
On the Slave
Stop the slave
All seems well, so I proceeded change the master log and master log position
CHANGE MASTER TO MASTER_LOG_FILE='binlog.000203', MASTER_LOG_POS=4;
Start the slave 
Once done, check the Slave Status and all should be well.
show slave status\G;
Please note that your binlog is e.g. mysql-bin.001 advance to the next log file mysql-bin-002

Why did this happen? According to Muhammad
I foresee master server crashed or rebooted and hence binary log events not synchronized on disk. This usually happens when sync_binlog != 1 on the master. You can investigate it as inspecting binary log contents as below:
user yogesh77 on the Percona Forum expanded this
"After sudden reboot mysql rolled back last transactions in binary logs however slave already incremented its binary position so after master is up slave is not able to get correct binary position. To resolve this issue you need to point slave to new binary file created after the server reboot and mysql restart. The same issue happened to be just 2 days back. After setting new binary position I skipped few entries on slave which were updated on slave and rolled back in binary position."
How to prevent
The suggestion is this little command.

On the master
SET GLOBAL sync_binlog=1;
Add to my.cnf or server.cnf for MariaDB to make permanent.
sync_binlog=1;
Also, try shutting down the server gracefully.

However there is a issue with sync_binlog. It appears enabling on certain file systems results in a significant performance hit. That discussion will have to be for another blog. Some discussions seem to indicate that it is getting better

n.b. If you have MYSQL replication set up, monitor it and have alerts emailed to you. Replication is a nice feature but must be monitored.

References

Saturday, August 18, 2012

Mysql and Innodb





http://forums.cpanel.net/f189/best-optimization-my-cnf-269391.html
part about query cache limit not right

Interesting take
http://www.trinitycore.org/f/topic/448-best-thread-cache-size-value/

http://dev.mysql.com/doc/refman/4.1/en/server-parameters.html


second paragraph
http://dev.mysql.com/doc/refman/4.1/en/table-cache.html

Usefull tool
http://www.mysqlcalculator.com/

skip-bdb
http://mysql.rjweb.org/doc.php/memory
http://www.tutorialspoint.com/mysql/mysql-database-tuning.htm


http://stackoverflow.com/questions/8665233/mysql-thread-cache-size-reduce-cpu-and-max-connection
http://dev.mysql.com/doc/refman/5.0/en/slow-query-log.html
http://jayant7k.blogspot.com/2009/09/innodb-configuration-and-optimization.html
http://www.tutorialspoint.com/mysql/mysql-database-tuning.htm
http://drupal.org/node/51263 - Tuning Mysql
http://www.highperfmysql.com/
http://serverfault.com/questions/354299/picking-the-right-innodb-buffer-pool-size?rq=1
http://serverfault.com/questions/316137/mysql-innodb-problem?rq=1  -> Very Goood
http://serverfault.com/questions/253059/mysql-innodb-optimisation?rq=1
http://serverfault.com/questions/220164/changing-innodb-buffer-pool-size-makes-error?rq=1


http://www.justin.my/2010/09/optimize-only-fragmented-tables-in-mysql/
http://dev.mysql.com/doc/refman/5.0/en/innodb-tuning.html
http://www.mysqlperformanceblog.com/2007/11/03/choosing-innodb_buffer_pool_size/
http://www.mysqlperformanceblog.com/2007/11/01/innodb-performance-optimization-basics/
http://mysqltuner.pl/mysqltuner.pl
http://openx.com/docs/whitepapers/performance-tuning
http://meinit.nl/optimize-only-fragmented-tables-mysql

Friday, August 17, 2012

MYSQL and TCP Wait isses



http://support.microsoft.com/kb/137984
http://blogs.technet.com/b/janelewis/archive/2010/03/09/explaining-close-wait.aspx
http://blogs.msdn.com/b/spike/archive/2008/10/09/tcp-connections-hanging-in-the-close-wait-and-fin-wait-2-state.aspx
http://bugs.mysql.com/bug.php?id=40662
http://books.google.com.jm/books?id=BL0NNoFPuAQC&pg=PA330&lpg=PA330&dq=net.ipv4.tcp_fin_timeout++mysql+tuning&source=bl&ots=COTOvpvG3U&sig=ID_ccSr1DHOePEjlkXiGaq_UuhA&hl=en&sa=X&ei=3jUuUMOgNLH7yAHRmICQCw&redir_esc=y#v=onepage&q=net.ipv4.tcp_fin_timeout%20%20mysql%20tuning&f=false

Saturday, May 24, 2008

Mysql TIps

Multiple inserts
http://www.petefreitag.com/item/379.cfm

Insert Delayed
http://www.petefreitag.com/item/430.cfm

Optimisation tips
http://www.petefreitag.com/item/613.cfm

More tips
http://www.petefreitag.com/item/455.cfm - Cheat sheets
http://www.petefreitag.com/item/505.cfm - Apache Security

Robots.txt and Search Engines

It is not sexy but it useful. The robots.txt is suppose to tell robots/bots/crawlers where they can crawl on a web site. The robots.txt mus...