YouTip LogoYouTip

Mysql Install

# MySQL Installation The download address for MySQL on all platforms is: (https://dev.mysql.com/downloads/mysql/). Choose the _MySQL Community Server_ version and platform you need. > **Note:** During the installation process, we need to enable administrator privileges to install, otherwise, the installation may fail due to insufficient permissions. * * * ## Installing MySQL on Linux/UNIX On the Linux platform, it is recommended to install MySQL using RPM packages. MySQL AB provides the following RPM package download addresses: * **MySQL** - MySQL server. You need this option unless you only want to connect to a MySQL server running on another machine. * **MySQL-client** - MySQL client programs for connecting to and operating the MySQL server. * **MySQL-devel** - Libraries and header files. You need to install this RPM package if you want to compile other MySQL clients, such as Perl modules. * **MySQL-shared** - This package contains shared libraries (libmysqlclient.so*) needed for dynamic loading by certain languages and applications that use MySQL. * **MySQL-bench** - Benchmark and performance testing tools for the MySQL database server. Before installation, we can check if the system already has MySQL installed: ```bash rpm -qa | grep mysql If it is installed on your system, you can choose to uninstall it: ```bash rpm -e mysql # Normal removal mode rpm -e --nodeps mysql # Forceful removal mode. Use this command if the above command fails due to dependent files. **Installing MySQL:** Next, we will use the `yum` command to install MySQL on a CentOS 7 system. Note that in CentOS 7, MySQL has been removed from the default program list. Therefore, before installation, we need to download the Yum repository package from the official website. The download address is: [https://dev.mysql.com/downloads/repo/yum/](https://dev.mysql.com/downloads/repo/yum/) !(#) ```bash wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm rpm -ivh mysql-community-release-el7-5.noarch.rpm yum update yum install mysql-server Permission settings: ```bash chown -R mysql:mysql /var/lib/mysql/ Initialize MySQL: ```bash mysqld --initialize Start MySQL: ```bash systemctl start mysqld Check MySQL running status: ```bash systemctl status mysqld **Note:** If this is the first time starting the MySQL service, the MySQL server will first perform an initialization configuration. > Additionally, you can use MariaDB as a replacement. MariaDB is a fork of the MySQL database management system, mainly maintained by the open-source community under the GPL license. One reason for developing this fork was the potential risk of Oracle closing the source code after acquiring MySQL, so the community adopted a fork to avoid this risk. > > > MariaDB aims to be fully compatible with MySQL, including APIs and command lines, making it an easy drop-in replacement for MySQL. > > ```bash > yum install mariadb-server mariadb > ``` > Relevant commands for the MariaDB database are: > > ```bash > systemctl start mariadb # Start MariaDB > systemctl stop mariadb # Stop MariaDB > systemctl restart mariadb # Restart MariaDB > systemctl enable mariadb # Set to start on boot > ``` * * * ## Verifying MySQL Installation After successfully installing MySQL, some basic tables will be initialized. After the server starts, you can perform a simple test to verify if MySQL is working properly. Use the `mysqladmin` utility to get the server status: Use the `mysqladmin` command to check the server version. On Linux, this binary file is located in the `/usr/bin` directory. On Windows, it is located in `C:mysqlbin`. ```bash [root@host]# mysqladmin --version On Linux, this command will output the following result, which is based on your system information: mysqladmin Ver 8.23 Distrib 5.0.9-0, for redhat-linux-gnu on i386 If the above command does not output any information, it indicates that your MySQL was not installed successfully. * * * ## Using MySQL Client to Execute Simple SQL Commands You can use the `mysql` command in the MySQL Client to connect to the MySQL server. By default, the MySQL server login password is empty, so this example does not require a password. The command is as follows: ```bash [root@host]# mysql After executing the above command, it will output the `mysql>` prompt, indicating that you have successfully connected to the MySQL server. You can execute SQL commands at the `mysql>` prompt: mysql> SHOW DATABASES; +----------+ | Database | +----------+ | mysql | | test | +----------+ 2 rows in set (0.13 sec) * * * ## Post-Installation Steps for MySQL After MySQL is successfully installed, the default root user password is empty. You can use the following command to create a password for the root user: ```bash [root@host]# mysqladmin -u root password "new_password"; Now you can connect to the MySQL server using the following command: ```bash [root@host]# mysql -u root -p Enter password:******* **Note:** When entering the password, it will not be displayed. Just enter it correctly. * * * ## Installing MySQL on Windows Installing MySQL on Windows is relatively simple. The latest version can be found in (https://dev.mysql.com/downloads/mysql/) (**For more detailed installation: (#)**). !(#) !(#) Click the **Download** button to enter the download page. Click **No thanks, just start my download.** in the image below to start the download immediately: !(#) After downloading, we unzip the zip package to the corresponding directory. Here, I place the unzipped folder under **C:webmysql-8.0.11**. **Next, we need to configure the MySQL configuration file** Open the just-unzipped folder **C:webmysql-8.0.11**, create a **my.ini** configuration file in this folder, and edit **my.ini** to configure the following basic information: ```ini # Set default character set for MySQL client default-character-set=utf8 # Set port 3306 port=3306 # Set MySQL installation directory basedir=C:webmysql-8.0.11 # Set MySQL database data storage directory. MySQL 8+ does not require this configuration; the system generates it automatically, otherwise errors may occur. # datadir=C:websqldata # Allow maximum connections max_connections=20 # Character set used by the server, default is 8-bit latin1 character-set-server=utf8 # Default storage engine for new tables default-storage-engine=INNODB **Next, let's start the MySQL database:** Open the cmd command-line tool as an administrator and switch directories: ```bash cd C:webmysql-8.0.11bin Initialize the database: ```bash mysqld --initialize --console After execution is complete, it will output the initial default password for the root user, for example: ...2018-04-20T02:35:05.464644Z 5 A temporary password is generated for root@localhost: APWCY5ws&hjQ ... `APWCY5ws&hjQ` is the initial password, which is required for subsequent logins. You can also change the password after logging in. Enter the following installation command: ```bash mysqld install To start, enter the following command: ```bash net start mysql > Note: In version 5.7, you need to initialize the data directory: > > ```bash > cd C:webmysql-8.0.11bin > mysqld --initialize-insecure > ``` > After initialization, run `net start mysql` to start MySQL. * * * ## Logging into MySQL When the MySQL service is already running, we can log in to the MySQL database using the built-in client tool. First, open the command prompt and enter the following command format: ```bash mysql -h hostname -u username -p Parameter description: * **-h**: Specifies the MySQL hostname the client wants to log into. This parameter can be omitted when logging into the local machine (localhost or 127.0.0.1). * **-u**: The username to log in with. * **-p**: Tells the server that a password will be used for login. This option can be ignored if the username and password for login are empty. If we want to log into the local MySQL database, we only need to enter the following command: ```bash mysql -u root -p Press Enter to confirm. If the installation is correct and MySQL is running, you will get the following response: Enter password: If a password exists, enter it to log in. If not, press Enter directly to log in. After a successful login, you will see the prompt "Welcome to the MySQL monitor...". Then the command prompt will remain at `mysql>` with a blinking cursor, waiting for command input. Enter **exit** or **quit** to log out.
← Mysql AdministrationMysql Tutorial β†’