How to Install MySQL on CentOS 7Updated Thursday, August 27th, by Linode
Use promo codeDOCS10For $ credit to a new account. Contribute on GitHub View Project | View File | Edit File
MySQL is a popular database management system used for Web and server applications. However, MySQL is no longer in CentOS's repositories and MariaDB has become the default database system offered. MariaDB is considered a drop-in replacement for MySQL and would are sufficient if you just need a database system in genera L. See we MariaDB in CentOS 7 guide for installation instructions.
If you nonetheless prefer MySQL, this guide would introduce how to install, configure and manage it on a Linode running Cen TOS 7.
Large MySQL databases can require a considerable amount of memory. For the reason, we recommend using a high memory Linode for such setups.
This are written for a non-root user. Commands that require elevated privileges is prefixed with sudo
. If you're not sudo
a familiar with the command, you can check our Users and Groups guide.
Before you Begin
Ensure that has followed the Getting Started and securing Your Server guides, and the Linode ' s hostname is set.
To check your hostname run:
The first command should show your short hostname, and the second should show your fully qualified domain name (FQDN).
Update Your system:
Install MySQLMySQL must is installed from the Community repository.
-
Download and add the repository, then update.
123 |
wget Http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpmsudo RPM-IVH Mysql-community-release-el7-5.noarch.rpmyum update |
-
Install MySQL As usual and start the service. During installation, you'll be asked if your want to accept the results from the. rpm file's GPG verification. If no error or mismatch occurs, enter y
.
12 |
sudo y Um install Mysql-serversudo systemctl start mysqld |
MySQL would bind to localhost (127.0.0.1) by default. Please reference our MySQL Remote Access Guide for information on connecting to your databases using SSH.
Allowing unrestricted access to MySQL in a public IP isn't advised but the address it listens on by modifying The bind-address
parameter in /etc/my.cnf
. If you decide to bind the MySQL to your public IP, you should implement firewall rules that is only allow connections from SPECIF IC IP addresses.
Harden MySQL Server
Run the mysql_secure_installation
script to address several security concerns in a default MySQL installation.
1 |
sudo mysql_secure_installation |
You'll be given the choice to change the MySQL root password, remove anonymous user accounts, disable root logins Outsid E of localhost, and remove test databases. It is recommended so answer to yes
these options. You can read more about the script in the the MySQL Reference Manual.
Using MySQLThe standard tool for interacting with MySQL are the mysql
client which installs with the mysql-server
. The MySQL client is used through a terminal.
Root Login
To log on to MySQL as the root user:
When prompted, enter the root password your assigned when the Mysql_secure_installation script is run.
You'll then being presented with a welcome header and the MySQL prompt as shown below:
To generate a list of commands for the MySQL prompt, enter \h
. You'll then see:
123456789101112131415161718192021222324252627282930 |
List of all MySQL commands:note This all text commands must is first on line and end with '; '? (\?) Synonym for ' help '. Clear (\c) Clear command. Connect (\ r) reconnect to the server. Optional arguments is DB and host. Delimiter (\d) Set statement delimiter. Note:takes the rest of the line as new delimiter. Edit command with $EDITOR. \e. Ego (\g) Send command to MySQL server, display result vertically. Exit (\q) exit MySQL. Same as quit. Go (\g) Send command to MySQL server. Help (\h) Display. Nopager (\ n) Disable Pager, print to stdout. Notee (\ t) Don ' t write into outfile. Pager (\p) Set Pager [To_pager]. Print the query results via PAGER. Print (\p) print current command. Prompt (\ r) Change your MySQL prompt. Quit (\q) quit MySQL. Rehash (\#) Rebuild completion hash. Source (\.) Execute an SQL script file. Takes a file name as an argument. Status (\s) Get status information from the server.System (\!) Execute a system shell command. Tee (\ t) Set outfile [To_outfile]. Append everything into given outfile. Use (\u) use another database. Takes database name as argument. CharSet (\c) Switch to another charset. Might is needed for processing binlog with Multi-Byte charsets. Warnings (\w) Show warnings after every statement. Nowarning (\w) Don ' t show warnings after every statement. For server side help, type ' help Contents ' mysql> |
Create a New MySQL User and Database
The example below, is the name of the testdb
database, is the testuser
user, and is the password
user ' s password.
123 |
Create DATABASE TestDB; Create user ' testuser ' @ ' localhost ' identified by ' password '; Grant all on testdb.* to ' testuser ' identified by ' password '; |
You can shorten this process by creating the user while assigning database permissions:
12 |
Create DATABASE TestDB; Grant all on testdb.* to ' testuser ' identified by ' password '; |
Then exit MySQL.
Create a Sample Table
-
Log back in as testuser
.
-
Create a sample table called customers. This creates a table with a customer ID field of the Type INT to Integer (auto-incremented for new records, used as the P Rimary key), as well as both fields for storing the customer's name.
12 |
use Te Stdb;create Table Customers (customer_id INT not NULL auto_increment PRIMARY KEY, first_name text, last_name text); |
-
Then exit MySQL.
Reset the MySQL Root PasswordIf you forget your root for MySQL password, it can be reset.
Stop the current MySQL server instance, then restart it with an option to not ask for a password.
12 |
sudo systemctl stop Mysqldsudo mysqld_safe--skip-grant-tables & |
Reconnect to the MySQL server with the MySQL root account.
Use the following commands to reset root ' s password. Replace with password
a strong password.
1234 |
Use Mysql;update user SET Password=password ("PASSWORD") WHERE user= ' root '; flush Privileges;exit |
Then restart MySQL.
1 |
sudo systemctl start mysqld |
Tune MySQLMySQL Tuner is a Perl script this connects to a running instance of MySQL and provides configuration recommendations based On workload. Ideally, the MySQL instance should has been operating for at least hours before the running. The longer the instance has been running, the better advice MySQL Tuner would give.
Download MySQL Tuner to your home directory.
1 |
wget https://raw.githubusercontent.com/major/MySQLTuner-perl/master/mysqltuner.pl |
To run it:
You'll be asked for the MySQL root user ' s name and password. The output would show the areas of interest:general recommendations and Variables to adjust.
MySQL Tuner is an excellent starting point to optimize a MySQL server it would was prudent to perform additional resear CH for configurations tailored to the application (s) utilizing MySQL on your Linode.
More informationwish to consult the following resources for additional information on this topic. While these is provided in the hope that they would be useful, please note this we cannot vouch for the accuracy or Timeli Ness of externally hosted materials.
- MySQL 5.6 Reference Manual
- PHP MySQL Manual
- Perl DBI examples for dbd::mysql
- MySQLdb User ' s Guide
- MySQL Tuner Tutorial