Difference between revisions of "Installing MySQL on CentOS 7"

From rdkwiki
Jump to: navigation, search
 
m
Line 8: Line 8:
  
 
== Installing MySQL ==
 
== Installing MySQL ==
* '''sudo yum install mysql-server''' --> install the MySQL server
+
* '''yum install mysql-server''' --> install the MySQL server
* '''sudo systemctl start mysqld''' --> start the MySQL server
+
* '''systemctl start mysqld''' --> start the MySQL server
* '''sudo systemctl status mysqld''' --> check if the MySQL server is running
+
* '''systemctl status mysqld''' --> check if the MySQL server is running
* '''sudo systemctl enable mysqld''' --> make the MySQL server start at boot
+
* '''systemctl enable mysqld''' --> make the MySQL server start at boot
  
 
== Securing MySQL ==
 
== Securing MySQL ==
* '''sudo grep "temporary password" /var/log/mysqld.log''' --> get the random password that is generated during the MySQL 5.7 installation
+
* '''grep "temporary password" /var/log/mysqld.log''' --> get the random password that is generated during the MySQL 5.7 installation
* '''sudo mysql_secure_installation''' --> default script with a few questions to harden the MySQL security
+
* '''mysql_secure_installation''' --> default script with a few questions to harden the MySQL security
 
* '''mysql -h localhost -u root -p''' --> connect with the new password from the previous step
 
* '''mysql -h localhost -u root -p''' --> connect with the new password from the previous step
 
* '''create user '<user>'@'<ip/subnet>' identified by '<password>';''' --> here you can use wildcards for the ip/subnet (for example: '192.168.0.%' or '%' for all hosts)
 
* '''create user '<user>'@'<ip/subnet>' identified by '<password>';''' --> here you can use wildcards for the ip/subnet (for example: '192.168.0.%' or '%' for all hosts)
Line 22: Line 22:
  
 
== Configure the firewall to allow MySQL connections ==
 
== Configure the firewall to allow MySQL connections ==
* '''sudo firewall-cmd --permanent --add-service=mysql''' --> add MySQL service to firewall
+
* '''firewall-cmd --permanent --add-service=mysql''' --> add MySQL service to firewall
* '''sudo firewall-cmd --reload''' --> reload firewall with changed configuration
+
* '''firewall-cmd --reload''' --> reload firewall with changed configuration
  
 
== Move MySQL data (optional) ==
 
== Move MySQL data (optional) ==
* '''sudo systemctl stop mysqld''' --> stop the MySQL server
+
* '''systemctl stop mysqld''' --> stop the MySQL server
* '''sudo mkdir /<path>/<directory>''' --> create a directory of choice
+
* '''mkdir /<path>/<directory>''' --> create a directory of choice
 
* '''cd /var/lib/mysql''' --> goto the default directory
 
* '''cd /var/lib/mysql''' --> goto the default directory
* '''sudo mv * /<path>/<directory>''' --> move data to folder of choice
+
* '''mv * /<path>/<directory>''' --> move data to folder of choice
* '''sudo nano /etc/my.cnf'''
+
* '''nano /etc/my.cnf'''
 
  change '''datadir=/var/lib/mysql''' to match the new path
 
  change '''datadir=/var/lib/mysql''' to match the new path
* '''sudo chown mysql.mysql /<path>/<folder>''' --> make MySQL owner of the new location  
+
* '''chown mysql.mysql /<path>/<folder>''' --> make MySQL owner of the new location  
* '''sudo systemctl start mysqld''' --> start the MySQL server
+
* '''systemctl start mysqld''' --> start the MySQL server

Revision as of 08:06, 31 October 2016

Intro

The steps below will configure a working MySQL 5.6 or 5.7 server. The steps assume that you have a (fresh) CentOS 7 server running.

Make MySQL available for installation

!! This step will replace CentOS's default MariaDB

Installing MySQL

  • yum install mysql-server --> install the MySQL server
  • systemctl start mysqld --> start the MySQL server
  • systemctl status mysqld --> check if the MySQL server is running
  • systemctl enable mysqld --> make the MySQL server start at boot

Securing MySQL

  • grep "temporary password" /var/log/mysqld.log --> get the random password that is generated during the MySQL 5.7 installation
  • mysql_secure_installation --> default script with a few questions to harden the MySQL security
  • mysql -h localhost -u root -p --> connect with the new password from the previous step
  • create user '<user>'@'<ip/subnet>' identified by '<password>'; --> here you can use wildcards for the ip/subnet (for example: '192.168.0.%' or '%' for all hosts)
  • grant all privileges on *.* to '<user>'@'<ip/subnet>' with grant option; --> give the new user all rights
  • flush privileges; --> reload all privileges

Configure the firewall to allow MySQL connections

  • firewall-cmd --permanent --add-service=mysql --> add MySQL service to firewall
  • firewall-cmd --reload --> reload firewall with changed configuration

Move MySQL data (optional)

  • systemctl stop mysqld --> stop the MySQL server
  • mkdir /<path>/<directory> --> create a directory of choice
  • cd /var/lib/mysql --> goto the default directory
  • mv * /<path>/<directory> --> move data to folder of choice
  • nano /etc/my.cnf
change datadir=/var/lib/mysql to match the new path
  • chown mysql.mysql /<path>/<folder> --> make MySQL owner of the new location
  • systemctl start mysqld --> start the MySQL server