MySQL Tutorial

MySQL Installation and Configuration

MySQL Database Operations

Database Design

MySQL Data Types

MySQL Storage Engines

MySQL Basic Operations of Tables

MySQL Constraints

MySQL Operators

MySQL Function

MySQL Manipulate Table Data

MySQL View

MySQL Indexes

MySQL Stored Procedure

MySQL Trigger

MySQL Transactions

MySQL Character Set

MySQL User Management

MySQL Database Backup and Recovery

MySQL Log

MySQL Performance Optimization

Install And Config MySQL in Linux

MySQL recommends using RPM package for installation on Linux platform, because the installation and uninstallation of RPM package are very convenient, and can be achieved through simple commands. This section mainly introduces how to install and configure MySQL using RPM package under Linux.

The installation environment in this section is CentOS 6.5, and the installation package of el6 is selected. Readers should choose the corresponding installation package according to their own system, for example: CentOS 7 should choose the el7 installation package. If the system version corresponding to the installation package is incorrect, a dependency error about glibc will appear during installation.

Next, install it through the RPM package. The specific operation steps are as follows:

Step 1): Go to the official download page (http://dev.mysql.com/downloads/mysql) and select the package to be downloaded.
Step 2): After the download is complete, switch to the root user. Install the rpm packages in sequence according to the dependencies, and the dependencies are in the order of common→libs→client→server. Install using the command rpm -ivh {-file-name}.
rpm -ivh mysql-community-common-5.7.29-1.el6.x86_64.rpm
rpm -ivh mysql-community-libs-5.7.29-1.el6.x86_64.rpm
rpm -ivh mysql-community-client-5.7.29-1.el6.x86_64.rpm
rpm -ivh mysql-community-server-5.7.29-1.el6.x86_64.rpm
In ivh, the i-install parameter indicates one or more RPM packages after installation; the v-verbose parameter indicates that detailed information is displayed during the installation process; the h-hash parameter indicates that "#" is used to display the installation progress.

When installing MySQL under the Linux operating system, be sure to pay attention to permissions. When installing RPM packages, you need to use root privileges, otherwise you will be prompted that the privileges are insufficient. And after the installation is complete, you also need to use root privileges to start and stop the MySQL service.

Step 3): The MySQL database can be started by the following command, but you must use root privileges.
service mysql start
Tip: Starting from MySQL 5.0, the service name of MySQL has been changed to mysql instead of mysqld of 4.*.

The operation command of the MySQL service is:
service mysql start | stop | restart | status
The meanings of the above parameters are as follows:
  • start: start the MySQL service
  • stop: stop the MySQL service
  • restart: restart the MySQL service
  • status: View MySQL service status

Step 4): After the service starts, look for the initial random password of root (if there is no initial password, just enter the user name root to log in)
cat /var/log/mysqld.log | grep 'temporary password is generated'

Step 5): After the installation is successful, use the following command to log in to MySQL.
mysql -uroot -p

If you see the following welcome message, the login is successful, and you can operate the MySQL database.
[root@localhost ~]# mysql -uroot -p
Enter password: ****
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.7.29 MySQL Community Server (GPL)

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
The above descriptive statement is described as follows:
  • Commands end with; or\g: Indicates that the command under the mysql command line ends with a semicolon (;) or "\g", and the command is executed when this end character is encountered.
  • Your MySQL connection id is 1: id indicates the number of connections to the MySQL database, here is 1, indicating that it is the first login.
  • Server version: 5. 7.29-log MySQL Community Server (GPL): Server version is followed by the version of the database, which is 5.7.29. Community Indicates that the release is a community edition.
  • Type 'help;' or '\h' for help: Enter "help;" or "\h" to see help information.
  • Type '\c' to clear the current input statement: means to clear the previous command when encountering "\c".
Step 6): You can use the following command to modify the password
set password='testroot';

When installing using an RPM package, the system will not prompt you in which folder the various files are installed. The following describes the directory where each main file is located. The MySQL server directory and subdirectories are shown in the following table:

Linux platform MySQL installation directory
folder folder contents
/usr/bin Clients and scripts (commands such as mysqladmin, mysqldump, etc.)
/usr/sbin mysqld server
/var/lib/mysql log files, socket files and databases
/usr/share/info Manual of Information Format
/usr/share/man UNIX help pages
/usr/include/mysql head File
/usr/lib/mysql library
/usr/share/mysql Error messages, character sets, installation and configuration files, etc.
/etc/rc.d/init.d/ The mysql directory for startup script files that can be used to start and stop the MySQL service 

Step 7): Configure the MySQL service, copy a file with the suffix cnf in the /usr/share/mysql/ or /usr/share/ folder to the /etc/ folder, and rename it to my.cnf. Use the vi editor to edit my.cnf (we introduced the specific meaning of each parameter in the my.cnf configuration file in the section " My.cnf Configuration File Details". The command is as follows:
cp /usr/share/mysql/my-large.cnf /etc/my.cnf
vi /etc/my.cnf
The first line of commands can complete the work of copying and renaming, and the second line of commands can edit my.cnf.

Note: After entering the my.cnf file with vi, press the i or a key to edit it. Press the Esc key to exit the editing state and enter the command state. If you want to save the modified data, :wyou can enter it. If you don't want to save and exit directly, :q!you can enter it.

After editing and saving the my.cnf file, the MySQL service must be restarted for the configuration in my.cnf to take effect.
  1. Setting Up MySQL Server on Linux:

    • Description: Setting up MySQL involves installing the MySQL server package, starting the service, and configuring basic settings.
    • Code: This may vary based on the distribution:
      • Ubuntu:
        sudo apt update
        sudo apt install mysql-server
        sudo systemctl start mysql
        
      • CentOS:
        sudo yum install mysql-server
        sudo systemctl start mysqld
        
  2. Configure MySQL on Ubuntu/Linux:

    • Description: Use the mysql_secure_installation command to configure MySQL settings, set the root password, and secure the installation.
    • Code:
      sudo mysql_secure_installation
      
  3. MySQL Installation and Configuration in CentOS:

    • Description: For CentOS, install MySQL and start the service. Use mysql_secure_installation for configuration.
    • Code:
      sudo yum install mysql-server
      sudo systemctl start mysqld
      sudo mysql_secure_installation
      
  4. Securing MySQL Installation on Linux:

    • Description: Run mysql_secure_installation to secure the MySQL installation by setting a strong root password, removing anonymous users, and disabling remote root login.
    • Code:
      sudo mysql_secure_installation
      
  5. Basic MySQL Setup on Debian:

    • Description: On Debian, install MySQL and configure with mysql_secure_installation.
    • Code:
      sudo apt update
      sudo apt install mysql-server
      sudo mysql_secure_installation