Two common ways to install MySQL on Linux

1. Installation

 

Method 1: Install MySQL via Docker

 

1. search

docker search mysql

2. pull

docker pull mysql

3. images

docker images

4. Install mapped port rename

docker run -p 3306:3306 --name mysql -e MYSQL_ROOT_PASSWORD=root -d mysql --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci

5. View the running container

docker ps

At this point, you're done! Use database software to connect


Chinese garbled characters appear when Docker installs mysql, please click here!

 

Method 2: Install MySQL through mirroring

 

The first step: download

Method 1: Click here to download , as shown below:

Method 2: Execute the following statement in the directory where you want to place the installation package

wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz

Step 2: Unzip and move

Note: Moving is not necessary, but there are specified file directories in the following configuration, and the installation of mysql on the Mac will also be placed in /usr/local by default, so it is best to follow the steps below.

Unzip:

tar -zxvf mysql-5.7.29-linux-glibc2.12-x86_64.tar.gz

mobile:

mv mysql-5.7.29-linux-glibc2.12-x86_64 /usr/mysql

Rename (rename and soft link, choose one of the two)

① Rename:

mv mysql-5.7.29-linux-glibc2.12-x86_64/ mysql-5.7.29

② Soft connection command:

ln -s mysql-5.7.29-linux-glibc2.12-x86_64/ mysql-5.7.29

Step 3: Create users and assign permissions to the data directory

Create the mysql group and user:

groupadd mysql 

useradd -r -g mysql mysql

Create the mysq data directory (back to the root directory):

cd / 

mkdir -p data 

cd data/ 

mkdir -p mysql

Grant permissions:

chown mysql:mysql -R /data/mysql

Step 4: Configure parameters

carried out:

vim /etc/my.cnf

Then press i to enter the editing mode and copy the following content into it:

[mysqld]
bind-address=0.0.0.0
port=3306
user=mysql
basedir=/usr/mysql/mysql-5.7.29/
datadir=/data/mysql
socket=/tmp/mysql.sock
log-error=/data/mysql/mysql.err
pid-file=/data/mysql/mysql.pid
# character config
character_set_server=utf8mb4
symbolic-links=0

After editing, press esc and hold down shift+: input wq to save and exit.

Step 5: Initialize mysql

cd /usr/mysql/mysql-5.7.29/bin/
./mysqld --defaults-file=/etc/my.cnf --basedir=/usr/mysql/mysql-5.7.29/ --datadir=/data/mysql/ --user=mysql --initialize

The following problems may occur (please ignore if not):

Solution: This is but less numactl. At this time, if it is Centos, execute yum -y install numactl to solve this problem. Run sudo apt-get install numactl in ubuntu. as follows:

Then execute the above statement and find that the execution is successful:

Check the initial password and copy it out:

cat /data/mysql/mysql.err

Step 6: Start mysql and change the root password

First put mysql.server in /etc/init.d/mysql:

cp /usr/mysql/mysql-5.7.29/support-files/mysql.server /etc/init.d/mysql

start up:

service mysqld start

The following problems may occur (please ignore if not):

Reason: Because there is no path and no permissions, this path is created and authorized to the mysql user, as follows:

solve!

Check whether to start:

ps -ef|grep mysql

change the password:

cd /usr/mysql/mysql-5.7.29/bin 

./mysql -u root -p

Prompt for the password, paste the password you just copied

Then perform the modification and refresh

SET PASSWORD = PASSWORD('**********'); 

ALTER USER 'root'@'localhost' PASSWORD EXPIRE NEVER; 

FLUSH PRIVILEGES;

This is the end, if you want to close mysql, execute service mysqld stop

 

If you use a remote connection at this time, you will find that you cannot connect...

The following three commands are mainly executed here (log into the database first):

use mysql                                            # 访问mysql库

update user set host = '%' where user = 'root';      # 使root能再任何host访问

FLUSH PRIVILEGES;                                    # 刷新

solve:

 

Precautions:

If the above steps have permission problems, you can execute the following commands and turn on the autostart:

cp ./support-files/mysql.server /etc/init.d/mysqld

chown 777 /etc/my.cnf  

chmod +x /etc/init.d/mysqld

Also, if your system has Mariadb before installation, uninstall it.

The first command is used to view, if there is, use the following command to uninstall

rpm -qa|grep mariadb

rpm -e --nodeps mariadb-libs-5.5.44-2.el7.centos.x86_64

In addition, if something that appears during the 5.7 installation is not resolved, it may be that your system was not clean before installation and you need to uninstall the residual mysql files.

 

Set MySQL to start at boot

1. Copy the service file to init.d and rename it to mysqld

cp /usr/mysql/mysql-5.7.29/support-files/mysql.server /etc/init.d/mysqld

2. Grant executable permissions

chmod +x /etc/init.d/mysqld

3. Add service

chkconfig --add mysqld

4. Display service list

chkconfig --list

If you see the mysql service, and 3, 4, and 5 are all on, then it succeeds, if it is off, type

chkconfig --level 345 mysqld on

5. Restart to take effect

reboot

 

Two, uninstall

1. Check the current installation of mysql

rpm -qa | grep -i mysql

2. Stop the mysql service and delete the previously installed mysql

Delete command:rpm -ev 包名

If it prompts a dependency package error, use the following command to try:rpm -ev 包名 –-nodeps

If you get an error:, error: %preun(xxxxxx) scriptlet failed, exit status 1try the following command:rpm -e --noscripts 包名

3. Find the directory of the previous old version of mysql, and delete the files and libraries of the old version of mysql

find / -name mysql

Use the following command to delete the specified directory:rm -rf 路径

Note: /etc/my.cnf will not be deleted after uninstallation, it needs to be deleted manually:

rm -rf /etc/my.cnf

4. Find again whether the machine has mysql installed

rpm -qa | grep -i mysql

If there is no result, mysql has been completely uninstalled.

 

If there is a problem outside the article, you can privately write to the author.

Guess you like

Origin blog.csdn.net/m0_38023584/article/details/105484072