Login as root user and install MySQL server by executing following commands:
# yum install mysql-server
# service mysqld start
# mysql_secure_installation
Press enter when prompted for the current root password.
During installation, use "yes" for all the questions.
You need to set a root password, remove any anonymous users, disable the remote root logins and then remove the test database.
At last, reload the privileges for the changes to take effect.
Configure firewall to allow MySQL port to accept connections from other machines
If iptables is in use in the system, then you need to allow MySQL port (3306) so that other machines can connect to MySQL server. You can run following command to allow port 3306 at firewall:
# 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
How to start MySQL
To start mysql, run below command:
# service mysqld start
How to restart MySQL
To restart mysql, you need to run following command:
# service mysqld restart
Configure auto start of MySQL service during system boot.
To make sure that MySQL server will launch when machine restarts, you need to execute following command:
# chkconfig mysqld on
Login to MySQL
Execute following command to login to MySQL server.
# mysql -u root -p
How to find database users
Login to MySQL shell and run below query to find the database.
> SELECT User, Host, Password FROM mysql.user;
Note: All SQL queries end with a semicolon.
How to create a database
Run below command at MySQL shell to create a database:
> CREATE DATABASE testdb;
It will create the database named "testdb".
To verify the databases, run the following command:
> SHOW DATABASES;
Output will be as below:
+--------------------+
| Database |
+--------------------+
| information_schema |
| testdb |
| mysql |
+--------------------+
3 rows in set (0.00 sec)
How to add a database user
To create a new user, execute the following command in MySQL shell:
> CREATE USER 'testuser'@'localhost' IDENTIFIED BY 'testpassword';
How to grant database user permissions
The newly created user will have no privileges. To give full permissions to the user, run following commands in MySQL shell:
> GRANT ALL PRIVILEGES ON testdb.* to demouser@localhost;
Flush the privileges for the changes to take effect:
> FLUSH PRIVILEGES;
Verify the privileges:
> SHOW GRANTS FOR 'testuser'@'localhost';