Can I write MySQL settings in drupal settings.php file? I am using Drupal 7 and i am getting MySQL server has gone away error in my current site.
By doing Google i figured out that i need to increase some of the parameters of my.ini file

key_buffer
max_allowed_packet
table_cache 
sort_buffer_size


Is there any way that I can write this in Drupals settings.php?
No, those need to be set in your MySQL server's my.ini/my.cnf file.

Those settings can't be changed on the fly from any process (PHP, Apache, even MySQL itself), you need to set them in the .ini/.cnf file and then restart the MySQL server for the changes to take effect.