MySQL is an open-source relational database that is free and widely used. It is a good choice if you know that you need a database but don’t know much about all of the available options. This article describes a basic installation of a MySQL database server on CentOS Linux Server. You might need to install other packages to let applications use MySQL, like extensions for PHP. Check your application documentation for details.
Install the database server
Follow the steps in this section to install the core database server.
- Install the MySQL database through the CentOS package manager (yum) by running the following commands at a command prompt:
sudo yum install mysql-server sudo /sbin/service mysqld start
- Run the following command:
- Press Enter to give no password for root when prompted for it.
- To apply some reasonable security to your new MySQL server answer yes to all the prompts. In order, those prompts enable you set the root password, remove anonymous users, disable remote root logins, delete the test database that the installer included, and then reload the privileges so that your changes will take effect.
Allow remote access
If you have iptables enabled and want to connect to the MySQL database from another computer, you must open a port in your server’s firewall (the default port is 3306). You don’t need to do this if the application that uses MySQL is running on the same server. If you need to open a port, add the following rules in iptables to open port 3306:
iptables -I INPUT -p tcp --dport 3306 -m state --state NEW,ESTABLISHED -j ACCEPT iptables -I OUTPUT -p tcp --sport 3306 -m state --state ESTABLISHED -j ACCEPT
Start and stop the database service
After the installation is complete, you can start the database service by using the commands in this section. If the system is already started, a message informs you that the service is already running.
Start and stop MySQL
Use the following command to start MySQL:
sudo /sbin/service mysqld start
Use the following command to stop MySQL:
sudo /sbin/service mysqld stop
Launch at reboot
To ensure that the database server launches after a reboot, you must enable the chkconfig utility. Use the following commands to do this. Enable chkconfig on MySQL
sudo chkconfig mysqld on
Enable chkconfig on MariaDB
sudo systemctl enable mariadb.service
Start the mysql shell
There is more than one way to work with a MySQL server, but this article focuses on the most basic and compatible approach: the mysql shell.
- At the command prompt, run the following command to launch the mysql shell and enter it as the root user:
/usr/bin/mysql -u root -p
- When you’re prompted for a password, enter the one that you set at installation or, if you haven’t set one, press Enter to submit no password.
The following mysql shell prompt should appear:
Set the root password
Because you have just installed the MySQL database server, the root account within MySQL has no password set yet. If you are logged in to the database server, set the root password by running the following command:
/usr/bin/mysqladmin -u root password 'new-password'
If you are not logged in to the database server you can remotely set the root password by specifying the hostname of your database server:
/usr/bin/mysqladmin -u root --password='new-password' -h hostname-of-your-server 'new-password'