Debug School

rakesh kumar
rakesh kumar

Posted on • Updated on

How to Operate MySQL on Linux

How to Install MySQL on CentOS 7
Harden(secure) MySQL Server
Create a New MySQL User and Database
refer here
refer here
refer here
Reset the MySQL Root Password
refer here
refer here


Before You Begin.

Ensure that you have followed the Getting Started and Securing Your Server guides, and the Linode’s hostname is set.

To check your hostname run:

2.hostname -f
The first command should show your short hostname, and the second should show your fully qualified domain name (FQDN).

Update your system:

1.sudo yum update
You will need wget to complete this guide. It can be installed as follows:
1.yum install wget

Harden(secure) MySQL Server

Run the mysql_secure_installation script to address several security concerns in a default MySQL installation.

1.sudo mysql_secure_installation
Enter fullscreen mode Exit fullscreen mode

Using MySQL

The standard tool for interacting with MySQL is the mysql client which installs with the mysql-server package. The MySQL client is used through a terminal.

Root Login
1.To log in to MySQL as the root user:

mysql -u root -p
Enter fullscreen mode Exit fullscreen mode

2.When prompted, enter the root password you assigned when the mysql_secure_installation script was run.
You'll then be presented with a welcome header and the MySQL prompt as shown below:

Enter fullscreen mode Exit fullscreen mode

3.To generate a list of commands for the MySQL prompt, enter \h. You’ll then see:

List of all MySQL commands:
Note that all text commands must be first on line and end with ';'

  1. ? (\?) Synonym for `help'.
  2. clear (\c) Clear command.
  3. connect (\r) Reconnect to the server. Optional arguments are db and host.
  4. delimiter (\d) Set statement delimiter. NOTE: Takes the rest of the line as new delimiter.
  5. edit (\e) Edit command with $EDITOR.
  6. ego (\G) Send command to mysql server, display result vertically.
  7. exit (\q) Exit mysql. Same as quit.
  8. go (\g) Send command to mysql server.
  9. help (\h) Display this help.
  10. nopager (\n) Disable pager, print to stdout.
  11. notee (\t) Don't write into outfile.
  12. pager (\P) Set PAGER [to_pager]. Print the query results via PAGER.
  13. print (\p) Print current command.
  14. prompt (\R) Change your mysql prompt.
  15. quit (\q) Quit mysql.
  16. rehash (#) Rebuild completion hash.
  17. source (.) Execute an SQL script file. Takes a file name as an argument.
  18. status (\s) Get status information from the server.
  19. system (!) Execute a system shell command.
  20. tee (\T) Set outfile [to_outfile]. Append everything into given outfile.
  21. use (\u) Use another database. Takes database name as argument.
  22. charset (\C) Switch to another charset. Might be needed for processing binlog with multi-byte charsets.
  23. warnings (\W) Show warnings after every statement.
  24. nowarning (\w) Don't show warnings after every statement.
  25. For server side help, type 'help contents'
  26. mysql>

Create a New MySQL User and Database

1.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';

2.Then exit MySQL.

  • exit

Create a Sample Table

1.Log back in as testuser.

mysql -u testuser -p

Create a sample table called customers. This creates a table with a customer ID field of the type INT for integer (auto-incremented for new records, used as the primary key), as well as two fields for storing the customer’s name.

1.use testdb;
2.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 Password

If you forget your root MySQL password, it can be reset.

Stop the current MySQL server instance, then restart it with an option to not ask for a password.

1.sudo systemctl stop mysqld
2.sudo mysqld_safe --skip-grant-tables &
Reconnect to the MySQL server with the MySQL root account.
1.mysql -u root
Use the following commands to reset root’s password. Replace password with a strong password.

1.use mysql;
2.update user SET PASSWORD=PASSWORD("password") WHERE USER='root';
3.flush privileges;
Then restart MySQL.
1.sudo systemctl start mysqld


Image description
Image description
Image description
Image description

Top comments (0)