{"id":775,"date":"2012-09-10T00:24:23","date_gmt":"2012-09-10T06:24:23","guid":{"rendered":"http:\/\/blog.supportpro.com\/?p=775"},"modified":"2019-10-30T05:43:04","modified_gmt":"2019-10-30T11:43:04","slug":"how-to-secure-mysql-in-linux-system","status":"publish","type":"post","link":"https:\/\/www.supportpro.com\/blog\/how-to-secure-mysql-in-linux-system\/","title":{"rendered":"How to  Secure Mysql in Linux system ?"},"content":{"rendered":"<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;\">an secure Mysql service using the following methods.<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;\">1. Restrict anonymous remote access :- Which meansnever provide grant access from all hosts. It must limit to specific users from specific hosts only. Do not grant the SUPER privilege and FILE privilege to non-administrative users. Any user who has this privilege can write a file anywhere in the file system with the privileges of the mysqld daemon.<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;\">2. Improve local security :- To improve local security use different socket file for both client and server connections. For that edit and add following line in the [client] section of \/etc\/my.cnf:<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;\">[client]<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;\">socket = \/tmp\/mysql.sock<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;\">Also the following parameter should be added in the [mysqld] section in \/etc\/my.cnf:<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;\">set-variable=local-infile=0<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;\">3. Change admin password :- This is the most important step in securing MySQL is changing the database administrator&#8217;s(root) password, which is empty by default. Below are the steps to change administrator password.<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;\">mysql -u root mysql&gt; SET PASSWORD FOR root@localhost=PASSWORD(&#8216;new_password&#8217;);<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;\">4. Change admin name :- It is strongly recommending to change the default name of administrator&#8217;s account (root), to a different for the more security. Such a change will help to prevent from the brute-force and dictionary attacks on the administrator&#8217;s password.<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;\">mysql&gt; update user set user=&#8221;mysqluser&#8221; where user=&#8221;root&#8221;; mysql&gt; flush privileges;<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;\">5. Remove history :- We also recommended to remove the content of the MySQL history file (~\/.mysql_history, ~\/.history, ~\/.bash_history,~\/.mysql_history) in which having all executed SQL commands (especially passwords are stored in this as plain text).<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;\">6. User Access Privileges :- Create accounts for specific databases which will be used by specific applications, so accounts should have access rights only to the databases which are used by the specific applications. In particular,they no longer will have any access rights to the mysql database, nor any system or administrative privileges (FILE, GRANT, ALTER, SHOW DATABASE, RELOAD, SHUTDOWN, PROCESS, SUPER etc.). Any Application user should not granted all privileges to database with Grant option from any host.<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;\">7. Disable remote access :- Only few applications on the same server will be allowed to access the database. So we need MySQL not to even listen on port 3306 for TCP connections like it does by default.<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;\">Edit \/etc\/my.cnf and uncomment the skip-networking line (comment the leading #).<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;\">8. Remove default users\/db :- Remove the sample database (test) and all accounts except the local root account:<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;\">mysql&gt; drop database test;<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;\">mysql&gt; use mysql;<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;\">mysql&gt; delete from db;<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;\">mysql&gt; delete from user where not (host=&#8221;localhost&#8221; and user=&#8221;root&#8221;);<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;\">mysql&gt; flush privileges;<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;\">This is why because it will prevent the database from establishing anonymous connections and irrespective of the skip-networking parameter in \/etc\/my.cnf remote connections as well.<\/div>\n<p>We can secure Mysql service using the following methods.<\/p>\n<p><strong>1. Restrict anonymous remote access :-<\/strong> Which meansnever provide grant access from all hosts. It must limit to specific users from specific hosts only. Do not grant the SUPER privilege and FILE privilege to non-administrative users. Any user who has this privilege can write a file anywhere in the file system with the privileges of the mysqld daemon.<\/p>\n<p><strong>2. Improve local security :-<\/strong> To improve local security use different socket file for both client and server connections. For that edit and add following line in the [client] section of \/etc\/my.cnf:<\/p>\n<blockquote><p>[client]<\/p>\n<p>socket = \/tmp\/mysql.sock<\/p><\/blockquote>\n<p>Also the following parameter should be added in the [mysqld] section in \/etc\/my.cnf:<\/p>\n<blockquote><p>set-variable=local-infile=0<\/p><\/blockquote>\n<p><!--more--><\/p>\n<p><strong> 3. Change admin password :- <\/strong>This is the most important step in securing MySQL is changing the database administrator&#8217;s(root) password, which is empty by default. Below are the steps to change administrator password.<\/p>\n<blockquote><p>mysql -u root mysql&gt; SET PASSWORD FOR root@localhost=PASSWORD(&#8216;new_password&#8217;);<\/p><\/blockquote>\n<p><strong> 4. Change admin name :-<\/strong> It is strongly recommending to change the default name of administrator&#8217;s account (root), to a different for the more security. Such a change will help to prevent from the brute-force and dictionary attacks on the administrator&#8217;s password.<\/p>\n<p>mysql&gt; update user set user=&#8221;mysqluser&#8221; where user=&#8221;root&#8221;; mysql&gt; flush privileges;<\/p>\n<p><strong> 5. Remove history :-<\/strong> We also recommended to remove the content of the MySQL history file (~\/.mysql_history, ~\/.history, ~\/.bash_history,~\/.mysql_history) in which having all executed SQL commands (especially passwords are stored in this as plain text).<\/p>\n<p><strong>6. User Access Privileges :-<\/strong> Create accounts for specific databases which will be used by specific applications, so accounts should have access rights only to the databases which are used by the specific applications. In particular,they no longer will have any access rights to the mysql database, nor any system or administrative privileges (FILE, GRANT, ALTER, SHOW DATABASE, RELOAD, SHUTDOWN, PROCESS, SUPER etc.). Any Application user should not granted all privileges to database with Grant option from any host.<\/p>\n<p><strong>7. Disable remote access :-<\/strong> Only few applications on the same server will be allowed to access the database. So we need MySQL not to even listen on port 3306 for TCP connections like it does by default.<\/p>\n<p>Edit \/etc\/my.cnf and uncomment the skip-networking line (comment the leading #).<\/p>\n<p><strong>8. Remove default users\/db :-<\/strong> Remove the sample database (test) and all accounts except the local root account:<\/p>\n<blockquote><p>mysql&gt; drop database test;<\/p>\n<p>mysql&gt; use mysql;<\/p>\n<p>mysql&gt; delete from db;<\/p>\n<p>mysql&gt; delete from user where not (host=&#8221;localhost&#8221; and user=&#8221;root&#8221;);<\/p>\n<p>mysql&gt; flush privileges;<\/p><\/blockquote>\n<p>This is why because it will prevent the database from establishing anonymous connections and irrespective of the skip-networking parameter in \/etc\/my.cnf remote connections as well.<\/p>\n<p>If you require help, <a href=\"https:\/\/www.supportpro.com\/requestquote.php\">contact SupportPRO Server Admin<\/a><\/p>\n<p style=\"text-align: center;\"><!--HubSpot Call-to-Action Code --><span id=\"hs-cta-wrapper-9d590242-d641-4383-94b4-8cfd62f0af6b\" class=\"hs-cta-wrapper\"><span id=\"hs-cta-9d590242-d641-4383-94b4-8cfd62f0af6b\" class=\"hs-cta-node hs-cta-9d590242-d641-4383-94b4-8cfd62f0af6b\"><!-- [if lte IE 8]><\/p>\n\n\n\n\n\n<div id=\"hs-cta-ie-element\"><\/div>\n\n\n<![endif]--><a href=\"https:\/\/www.supportpro.com\/freecheckup.php\"><img decoding=\"async\" id=\"hs-cta-img-9d590242-d641-4383-94b4-8cfd62f0af6b\" class=\"hs-cta-img\" style=\"border-width: 0px;\" src=\"https:\/\/no-cache.hubspot.com\/cta\/default\/2725694\/9d590242-d641-4383-94b4-8cfd62f0af6b.png\" alt=\"Server not running properly? Get A FREE Server Checkup By Expert Server Admins - $125 Value\" \/><\/a><\/span><script charset=\"utf-8\" src=\"https:\/\/js.hscta.net\/cta\/current.js\"><\/script><script type=\"text\/javascript\"> hbspt.cta.load(2725694, '9d590242-d641-4383-94b4-8cfd62f0af6b', {}); <\/script><\/span><!-- end HubSpot Call-to-Action Code --><\/p>\n","protected":false},"excerpt":{"rendered":"<p>an secure Mysql service using the following methods. 1. Restrict anonymous remote access :- Which meansnever provide grant access from all hosts. It must limit to specific users from specific&hellip;<\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_lmt_disableupdate":"","_lmt_disable":"","footnotes":""},"categories":[1],"tags":[],"class_list":["post-775","post","type-post","status-publish","format-standard","hentry","category-miscellaneous"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/posts\/775","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/comments?post=775"}],"version-history":[{"count":4,"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/posts\/775\/revisions"}],"predecessor-version":[{"id":4444,"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/posts\/775\/revisions\/4444"}],"wp:attachment":[{"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/media?parent=775"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/categories?post=775"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/tags?post=775"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}