MariaDB on CentOS 7
Enable MariaDB to start on boot and then start the service:
- sudo systemctl enable mariadb
- sudo systemctl start mariadb
Harden MariaDB Server
- Run the
mysql_secure_installation
script to address several security concerns in a default MariaDB installation: - sudo mysql_secure_installation
Root Login
- To log in to MariaDB as the root user:
- mysql -u root -p
Create a New MariaDB User and Database
In the example below,
testdb
is the name of the database, testuser
is the user, and password
is the user’s password:- 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:
- create database testdb;
- grant all on testdb.* to 'testuser' identified by 'password';
View the new Databases:
- show databases;
View the new table:
- show tables;
To retrieve all Users in MariaDB, you can execute the following SQL statement:
- SELECT User FROM mysql.user;
Reset the MariaDB Root Password
If you forget your root MariaDB password, it can be reset.
- Stop the current MariaDB server instance, then restart it with an option to not ask for a password:
- sudo systemctl stop mariadb
- sudo mysqld_safe --skip-grant-tables &
Reconnect to the MariaDB server with the MariaDB root account:
- mysql -u root
Use the following commands to reset root’s password. Replace
password
with a strong password:- use mysql;
- update user SET PASSWORD=PASSWORD("password") WHERE USER='root';
- flush privileges;
- exit
Then restart MariaDB:
- sudo systemctl start mariadb
However, note that this query shows a large listing of MySQL user information, including user permission information, so as a practical matter you may want to trim down some of the fields to display, something like this:
- mysql> select host, user, password from mysql.user;
The next section provides details and background information about this second query.
How to reduce the amount of ‘user’ information shown
You can get a listing of the fields in the
mysql.user
table by running this MySQL query:- mysql> desc mysql.user;
So for most cases where you want to show MySQL user accounts you'll probably want to limit your MySQL users' query to a few important columns, something like this:
- select host, user, password from mysql.user;
No comments:
Post a Comment