{"id":6303,"date":"2016-09-18T18:40:27","date_gmt":"2016-09-18T10:40:27","guid":{"rendered":"http:\/\/rmohan.com\/?p=6303"},"modified":"2016-09-26T09:41:42","modified_gmt":"2016-09-26T01:41:42","slug":"mysql-centos-7","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=6303","title":{"rendered":"MYSQL  mariadb CENTOS 7"},"content":{"rendered":"<p><strong>MYSQL CENTOS 7<\/strong><\/p>\n<p>wget http:\/\/repo.mysql.com\/mysql-community-release-el7-5.noarch.rpm<br \/>\nrpm -ivh mysql-community-release-el7-5.noarch.rpm<br \/>\nyum update<br \/>\nyum install mysql-server<\/p>\n<p>systemctl start mysqld<\/p>\n<p>[root@clusterserver1 ~]# systemctl start mysqld<br \/>\n[root@clusterserver1 ~]# systemctl enable mysqld<br \/>\n[root@clusterserver1 ~]# systemctl status mysqld<\/p>\n<p>[root@clusterserver1 ~]# mysql_secure_installation<\/p>\n<p>NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL<br \/>\nSERVERS IN PRODUCTION USE!\u00a0 PLEASE READ EACH STEP CAREFULLY!<\/p>\n<p>In order to log into MySQL to secure it, we&#8217;ll need the current<br \/>\npassword for the root user.\u00a0 If you&#8217;ve just installed MySQL, and<br \/>\nyou haven&#8217;t set the root password yet, the password will be blank,<br \/>\nso you should just press enter here.<\/p>\n<p>Enter current password for root (enter for none):<br \/>\nOK, successfully used password, moving on&#8230;<\/p>\n<p>Setting the root password ensures that nobody can log into the MySQL<br \/>\nroot user without the proper authorisation.<\/p>\n<p>Set root password? [Y\/n] y<br \/>\nNew password:<br \/>\nRe-enter new password:<br \/>\nPassword updated successfully!<br \/>\nReloading privilege tables..<br \/>\n&#8230; Success!<\/p>\n<p>By default, a MySQL installation has an anonymous user, allowing anyone<br \/>\nto log into MySQL without having to have a user account created for<br \/>\nthem.\u00a0 This is intended only for testing, and to make the installation<br \/>\ngo a bit smoother.\u00a0 You should remove them before moving into a<br \/>\nproduction environment.<\/p>\n<p>Remove anonymous users? [Y\/n] y<br \/>\n&#8230; Success!<\/p>\n<p>Normally, root should only be allowed to connect from &#8216;localhost&#8217;.\u00a0 This<br \/>\nensures that someone cannot guess at the root password from the network.<\/p>\n<p>Disallow root login remotely? [Y\/n] y<br \/>\n&#8230; Success!<\/p>\n<p>By default, MySQL comes with a database named &#8216;test&#8217; that anyone can<br \/>\naccess.\u00a0 This is also intended only for testing, and should be removed<br \/>\nbefore moving into a production environment.<\/p>\n<p>Remove test database and access to it? [Y\/n] y<br \/>\n&#8211; Dropping test database&#8230;<br \/>\nERROR 1008 (HY000) at line 1: Can&#8217;t drop database &#8216;test&#8217;; database doesn&#8217;t exist<br \/>\n&#8230; Failed!\u00a0 Not critical, keep moving&#8230;<br \/>\n&#8211; Removing privileges on test database&#8230;<br \/>\n&#8230; Success!<\/p>\n<p>Reloading the privilege tables will ensure that all changes made so far<br \/>\nwill take effect immediately.<\/p>\n<p>Reload privilege tables now? [Y\/n] y<br \/>\n&#8230; Success!<\/p>\n<p>All done!\u00a0 If you&#8217;ve completed all of the above steps, your MySQL<br \/>\ninstallation should now be secure.<\/p>\n<p>Thanks for using MySQL!<\/p>\n<p>Cleaning up&#8230;<br \/>\n[root@clusterserver1 ~]# systemctl restart mysqld<\/p>\n<p>[root@clusterserver1 ~]# mysql -u root -p<br \/>\nEnter password:<br \/>\nWelcome to the MySQL monitor.\u00a0 Commands end with ; or \\g.<br \/>\nYour MySQL connection id is 2<br \/>\nServer version: 5.6.33 MySQL Community Server (GPL)<\/p>\n<p>Copyright (c) 2000, 2016, Oracle and\/or its affiliates. All rights reserved.<\/p>\n<p>Oracle is a registered trademark of Oracle Corporation and\/or its<br \/>\naffiliates. Other names may be trademarks of their respective<br \/>\nowners.<\/p>\n<p>Type &#8216;help;&#8217; or &#8216;\\h&#8217; for help. Type &#8216;\\c&#8217; to clear the current input statement.<\/p>\n<p>mysql&gt;<\/p>\n<p>create database testdb;<br \/>\ncreate user &#8216;testuser&#8217;@&#8217;localhost&#8217; identified by &#8216;password&#8217;;<br \/>\ngrant all on testdb.* to &#8216;testuser&#8217; identified by &#8216;password&#8217;;<\/p>\n<p>mysql&gt; create database testdb;<br \/>\nQuery OK, 1 row affected (0.00 sec)<\/p>\n<p>mysql&gt; create user &#8216;testuser&#8217;@&#8217;localhost&#8217; identified by &#8216;password&#8217;;<br \/>\nQuery OK, 0 rows affected (0.00 sec)<\/p>\n<p>mysql&gt; grant all on testdb.* to &#8216;testuser&#8217; identified by &#8216;password&#8217;;<br \/>\nQuery OK, 0 rows affected (0.00 sec)<\/p>\n<p>mysql&gt;<\/p>\n<p>create database testdb;<br \/>\ngrant all on testdb.* to &#8216;testuser&#8217; identified by &#8216;password&#8217;;<\/p>\n<p>mysql -u testuser -p<\/p>\n<p>use testdb;<br \/>\ncreate table customers (customer_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, first_name TEXT, last_name TEXT);<\/p>\n<p>[root@clusterserver1 ~]# mysql -u testuser -p<br \/>\nEnter password:<br \/>\nWelcome to the MySQL monitor.\u00a0 Commands end with ; or \\g.<br \/>\nYour MySQL connection id is 4<br \/>\nServer version: 5.6.33 MySQL Community Server (GPL)<\/p>\n<p>Copyright (c) 2000, 2016, Oracle and\/or its affiliates. All rights reserved.<\/p>\n<p>Oracle is a registered trademark of Oracle Corporation and\/or its<br \/>\naffiliates. Other names may be trademarks of their respective<br \/>\nowners.<\/p>\n<p>Type &#8216;help;&#8217; or &#8216;\\h&#8217; for help. Type &#8216;\\c&#8217; to clear the current input statement.<\/p>\n<p>mysql&gt; use testdb;<br \/>\nDatabase changed<br \/>\nmysql&gt; create table customers (customer_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, first_name TEXT, last_name TEXT);<br \/>\nQuery OK, 0 rows affected (0.01 sec)<\/p>\n<p>mysql&gt;<\/p>\n<p>Reset the MySQL Root Password<\/p>\n<p>If you forget your root MySQL password, it can be reset.<\/p>\n<p>Stop the current MySQL server instance, then restart it with an option to not ask for a password.<\/p>\n<p>systemctl stop mysqld<br \/>\nmysqld_safe &#8211;skip-grant-tables &amp;<\/p>\n<p>Reconnect to the MySQL server with the MySQL root account.<\/p>\n<p>mysql -u root<\/p>\n<p>Use the following commands to reset root\u2019s password. Replace password with a strong password.<\/p>\n<p>use mysql;<br \/>\nupdate user SET PASSWORD=PASSWORD(&#8220;password&#8221;) WHERE USER=&#8217;root&#8217;;<br \/>\nflush privileges;<br \/>\nexit<\/p>\n<p>Then restart MySQL.<\/p>\n<p>systemctl start mysqld<\/p>\n<p>Tune MySQL<\/p>\n<p>MySQL Tuner is a Perl script that connects to a running instance of MySQL and provides configuration recommendations based on workload. Ideally, the MySQL instance should have been operating for at least 24 hours before running the tuner. The longer the instance has been running, the better advice MySQL Tuner will give.<\/p>\n<p>Download MySQL Tuner to your home directory.<\/p>\n<p>wget https:\/\/raw.githubusercontent.com\/major\/MySQLTuner-perl\/master\/mysqltuner.pl<\/p>\n<p>To run it:<\/p>\n<p>perl .\/mysqltuner.pl<\/p>\n<p>You will be asked for the MySQL root user\u2019s name and password. The output will show two areas of interest: General recommendations and Variables to adjust.<\/p>\n<p>MySQL Tuner is an excellent starting point to optimize a MySQL server but it would be prudent to perform additional research for configurations tailored to the application(s) utilizing MySQL on<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>recently upgraded my development laptop to CentOS 7 and while I was at it built a spare laptop running the same operating system. After manually creating a dozen MySQL databases and users and then importing their data from dump files on one laptop, I wasn\u2019t interested in going through exactly the same process again on the second laptop.<\/p>\n<p>As is now the default on CentOS 7, the databases were actually MariaDB (<span class=\"st\">a community-developed fork of MySQL) <\/span>rather than MySQL, but that\u2019s not relevant here. The following procedure describes how I migrated an entire MariaDB\/MySQL RDBMS installation in one go without recreating any databases or users or dumping and re-importing any files. The same procedure would of course work for migrating similar data between CentOS 7 servers.<\/p>\n<p>Before proceeding I should emphasise that this data migration was between two machines with identical freshly installed operating systems. In cases where the operating systems, distributions or version were different things might not go so smoothly. One concern would be that global database configuration parameters on both machines were compatible.<\/p>\n<h2>Original Database Server<\/h2>\n<p>1: Create a directory to hold the data being migrated:<\/p>\n<pre># mkdir mariadbdata<\/pre>\n<p>2: Stop the MariaDB\/MySQL server:<\/p>\n<pre># systemctl stop mariadb<\/pre>\n<p>3: Copy contents of <strong>\/var\/lib\/mysql<\/strong> to the directory just created:<\/p>\n<pre># cp -r \/var\/lib\/mysql\/* mariadbdata<\/pre>\n<p>4: Restart the MariaDB\/MySQL server again:<\/p>\n<pre># systemctl start mariadb<\/pre>\n<p>5: Compress the data:<\/p>\n<pre> # tar -czvf mariadbdata.tar.gz mariadbdata<\/pre>\n<p>6: Copy the compressed file to new server.<\/p>\n<p>&nbsp;<\/p>\n<h2>New Database Server<\/h2>\n<p>1: Install MariaDB <em><strong>but don\u2019t start it yet<\/strong><\/em>.<\/p>\n<p>2: Uncompress data file:<\/p>\n<pre># tar -xzvf mariadbdata.tar.gz<\/pre>\n<p>3: Move contents of data directory to <strong>\/var\/lib\/mysql<\/strong>:<\/p>\n<pre># mv mariadbdata\/* \/var\/lib\/data<\/pre>\n<p>4: Change ownership of all files in <strong>\/var\/lib\/mysql<\/strong> to mysql user:<\/p>\n<pre># chown -R mysql.mysql \/var\/lib\/mysql\/*<\/pre>\n<p>5: Restore correct SELinux security contexts:<\/p>\n<pre># restorecon -R \/var\/lib\/mysql\/<\/pre>\n<p>6: Enable and start MariaDB:<\/p>\n<pre># systemctl enable mariadb\r\n# systemctl start mariadb\r\n\r\n\r\n<\/pre>\n<p>Typically the mysql database are located in <strong>\/var\/lib\/mysql<\/strong><\/p>\n<p>I want to change it to <strong>\/var\/data\/mysql\u00a0<\/strong><\/p>\n<p>Modify the paths as required in the below commands<\/p>\n<p>stop mysql<\/p>\n<pre>systemctl stop mysqld.service<\/pre>\n<p>create new mysql data directory<\/p>\n<pre>mkdir \/var\/data\/mysql<\/pre>\n<p>modify <strong>\/etc\/<span class=\"skimlinks-unlinked\">my.cnf<\/span><\/strong> and point to new data directory \u2013 add the client section to the top<\/p>\n<pre>[client]\r\nport=3306\r\nsocket=\/var\/data\/mysql\/<span class=\"skimlinks-unlinked\">mysql.sock<\/span>\r\n\r\n[mysqld]\r\ndatadir=\/var\/data\/mysql\r\nsocket=\/var\/data\/mysql\/<span class=\"skimlinks-unlinked\">mysql.sock<\/span><\/pre>\n<p>copy all files from <strong>\/var\/lib\/mysql<\/strong> to the new directory <strong>\/var\/data\/mysql<\/strong><\/p>\n<pre>cp -r \/var\/lib\/mysql\/* \/var\/data\/mysql<\/pre>\n<p>permissions for the new directory<\/p>\n<pre>chown -R mysql \/var\/data\/mysql;\r\nchgrp -R mysql \/var\/data\/mysql;\r\nchmod -R g+rw \/var\/data\/mysql;<\/pre>\n<p>also modify SELINUX settings to allow mysql to use the different path<\/p>\n<pre># add context and make it permanent \r\nsemanage fcontext -a -s system_u -t mysqld_db_t \"\/var\/data\/mysql(\/.*)?\"\r\nrestorecon -Rv \/var\/data\/mysql<\/pre>\n<p>start mysql<\/p>\n<pre>systemctl start\u00a0mysqld.service<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>MYSQL CENTOS 7<\/p>\n<p>wget http:\/\/repo.mysql.com\/mysql-community-release-el7-5.noarch.rpm rpm -ivh mysql-community-release-el7-5.noarch.rpm yum update yum install mysql-server<\/p>\n<p>systemctl start mysqld<\/p>\n<p>[root@clusterserver1 ~]# systemctl start mysqld [root@clusterserver1 ~]# systemctl enable mysqld [root@clusterserver1 ~]# systemctl status mysqld<\/p>\n<p>[root@clusterserver1 ~]# mysql_secure_installation<\/p>\n<p>NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!<\/p>\n<p> [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[73,72],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/6303"}],"collection":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6303"}],"version-history":[{"count":3,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/6303\/revisions"}],"predecessor-version":[{"id":6308,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/6303\/revisions\/6308"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6303"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6303"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6303"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}