Install with YUM

1. Installation failure causes and solutions

CentOS 7.6 has an internal integration with Mariadb. If you install Mysql on CentOS 7.6, it will conflict with mariadb files, so you need to uninstall mariadb first. The following command is used to check whether Mariadb is installed:

shell> rpm -qa | grep mariadb
Copy the code

The uninstallation command is as follows:

Shell > RPM -e --nodeps mariadb-libs-5.5.60-1.el7_5.x86_64Copy the code

And check whether it is clean as shown below:

2. CentOS 7 Cause of mysql installation failure

Starting with CentOS 7, MariaDB is the default database installation package in the Yum source. MariaDB (a branch of MySQL) is installed by default with yum on CentOS 7 and above. If you want to install the official MySQL version, you need to use the Yum source provided by MySQL.

3. Add MySQL Yum Repository

Download the MySQL source

The official address: dev.mysql.com/downloads/r…

Shell > cat /etc/redhat-release CentOS Linux release 7.6.1810 (Core)

Viewing the system version:

shell> cat /etc/redhat-release
Copy the code

The diagram below:

Select the corresponding version to download, for example, CentOS 7.

Dev.mysql.com/get/mysql80…

The command is as follows:

shell> wget https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
Copy the code

Install the MySQL source

The command is as follows:

shell> sudo rpm -Uvh platform-and-version-specific-package-name.rpm
Copy the code

The installation package named platform-and-version-specific-package-name. RPM is installed.

For example, if CentOS7 is installed as the latest MySQL source, run the following command:

shell> sudo rpm -Uvh mysql80-community-release-el7-3.noarch.rpm
Copy the code

Check whether the installation is successful

Repo and mysql-community-source.repo are generated in the /etc/yom.repos. d/ directory

You can also see mysql related resources through yum Repolist

Run the following command:

shell> yum repolist enabled | grep "mysql.*-community.*"
Copy the code

As shown below:

4. Select the MySQL version

If you want to install MySQL from the MySQL source, you can skip this step. If you want to install MySQL from the MySQL source, you can skip this step. For example, if I want to install MySQL5.7, I need to “switch the version” :

View all MySQL versions in the current MySQL Yum Repository (each version in a different subrepository)

shell> yum repolist all | grep mysql
Copy the code

The diagram below:

Switch version

shell> sudo yum-config-manager --disable mysql80-community

shell> sudo yum-config-manager --enable mysql57-community
Copy the code

In addition to using yum-config-manager, you can directly edit the /etc/yom.repos. d/ mysql-community-repo file

For example, enabled=0 Disables the function

The mysql-community-repo file contains the following contents:

[mysql80-community] name=MySQL 8.0 community Server baseurl=http://repo.mysql.com/yum/mysql-8.0-community/el/7/$basearch/ enabled = 0 gpgcheck = 1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysqlCopy the code

Enable enabled = 1

# Enable to use MySQL 5.7 [mysql57-community] name=MySQL 5.7 community Server baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/$basearch/ enabled = 1 gpgcheck = 1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysqlCopy the code

Check the MySQL repository currently enabled

shell> yum repolist enabled | grep mysql
Copy the code

The diagram below:

If multiple repositories are enabled at the same time, the latest version will be selected for installation

5. Installation of MySQL

shell> sudo yum install mysql-community-server
Copy the code

This command installs the MySQL server (mysql-community-server) and its dependencies and related components. Including mysql-community-client, mysql-community-common, and mysql-community-libs

If the bandwidth is insufficient, this step takes a long time. Please wait patiently for ~

– Fixed slow MySql download from official MySql source via Yum

To back up the Yum source file of the system, run the following command:

shell> mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
Copy the code

To download aliyun’s Yum configuration file, run the following command:

shell> wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
Copy the code

Run yum makecache to generate the cache:

shell> yum makecache
Copy the code

Now you can quickly download mysql with the following command:

shell> sudo yum install mysql-community-server
Copy the code

6. Start the MySQL

  • Start the

The command is as follows:

shell> sudo systemctl start mysqld.service
Copy the code

CentOS 6:

shell> sudo service mysqld start
Copy the code

Check the status

shell> sudo systemctl status mysqld.service
Copy the code

CentOS 6:

shell> sudo service mysqld status
Copy the code

stop

shell> sudo systemctl stop mysqld.service
Copy the code

CentOS 6:

shell> sudo service mysqld stop
Copy the code

restart

shell> sudo systemctl restart mysqld.service
Copy the code

CentOS 6:

shell> sudo service mysqld restart
Copy the code

7. Change the initial password

After the MySQL is started for the first time, the super administrator account root@localhost is created. The initial password is stored in a log file.

shell> sudo grep 'temporary password' /var/log/mysqld.log
Copy the code

Changing the Default Password

Note: Make sure the server is started before changing the password

shell> mysql -uroot -p

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
Copy the code

Error as follows:

ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
Copy the code

The above prompt appears because the password is too simple, the solution is as follows:

  1. For complex passwords, MySQL’s default password policy is to contain numbers, letters, and special characters.

  2. If you don’t want to use a complex password, you can modify the default policy of validATE_password_policy (and parameters such as VALIDATE_password_length) to support simple passwords.

  3. CNF file, add validate_password=OFF, save and restart MySQL

  4. Run the following command again

    mysql> ALTER USER ‘root’@’localhost’ IDENTIFIED BY ‘123456’;

The following results appear:

Query OK, 0 rows affected (0.00 sec)
Copy the code

8. Allow remote access by root

For local login configuration, set host of roo account to wildcard % as follows:

mysql> use mysql;

mysql> update user set host = '%' where user = 'root';
Copy the code

The command can be queried as follows:

mysql> select host,user from user;
Copy the code

To refresh, run the following command:

mysql> flush privileges;
Copy the code

9. Set the encoding to UTF8

  • Check the code

  1. The query command is displayed as follows:

    mysql> SHOW VARIABLES LIKE ‘character%’;

The diagram below:

Set the coding

  1. When creating a table using Navicat, select UTF8 for character set and UTF8 for table fields.

  2. Modify the encoding mode as follows

    mysql> set character_set_client=utf8; Query OK, 0 rows affected (0.00 sec)

    mysql> set character_set_results=utf8; Query OK, 0 rows affected (0.00 sec)

    mysql> set character_set_connection=utf8; Query OK, 0 rows affected (0.00 sec)

validation

mysql> select * from user; + - + -- -- -- -- -- -- -- -- -- -- -- + | | id name | + - + -- -- -- -- -- -- -- -- -- -- -- + | 1 | zhang SAN | | 2 | liLei | + - + -- -- -- -- -- -- -- -- -- -- - + 2 rows in the set (0.00 SEC)Copy the code

10. Set startup

shell> systemctl enable mysqld

shell> systemctl daemon-reload
Copy the code

11. View processes and ports

ps   -C   mysqld                                                
netstat  -utnlp  | grep  mysqld 
Copy the code

12. Search for the initialized password

grep password /var/log/mysqld.log 
Copy the code

13. Log in to the local service

Mysql -hlocalhost -uroot -p' initialize password 'Copy the code

14. View service information

[root@192 ~]# uname -r 3.10.0-957.el7.x86_64 [root@192 ~]# cat /etc/redhat-release CentOS Linux release 7.6.1810 (Core) [root@192 ~]# cat /proc/version Linux version 3.10.0-957.el7.x86_64 ([email protected]) (GCC version 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC)) #1 SMP Thu Nov 8 23:39:32 UTC 2018Copy the code

15. Uninstall the installed mysql

Stop the mysql service

systemctl stop  mysqld.service        #centOS7
service   mysqld  stop                #centOS6 
Copy the code

Delete or back up your mysql database

(I chose delete, if the data is important remember to back up)

rm -rf /etc/my.cnf
rm -rf /var/lib/mysql 
Copy the code

Then uninstall the RPM package of mysql that has been installed

Yum list installed | grep mysql RPM - qa | grep -i mysql # lookup service installed mysql - community - client. X86_64 5.7.17-1. El7 installed X86_64 5.7.17-1.el7 installed mysql-community-devel.x86_64 5.7.17-1.el7 installed X86_64 5.7.17-1.el7 installed mysql-community-embedded-compat. X86_64 5.7.17-1.el7 installed X86_64 5.7.17-1.el7 installed mysql-community-libs.x86_64 5.7.17-1.el7 installed X86_64 5.7.17-1.el7 installed mysql-community-minimal debuginfo.x86_64 5.7.17-1.el7 X86_64 5.7.17-1.el7 installed mysql-community-test.x86_64 5.7.17-1.el7 installed mysql57-community-release.noarch el7-8 installedCopy the code

Uninstalling the service (there are some problems when uninstalling the service)

yum remove mysql*  
Copy the code

It is recommended to use this uninstall

RPM -ev Indicates the full package nameCopy the code

Some mysql packages are installed in RPM mode, which can be difficult to uninstall

Yum Remove yum removeCopy the code

2. Decompress the package and install tar.gz

1. Download the compressed package

Download address:

Dev.mysql.com/downloads/m…

My system is Centos7.6, select the following figure:

The command is as follows:

Shell > wget HTTP: / / https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-5.8.0.23-linux-glibc2.12-x86_64.tar.gzCopy the code

2. Installation & Configuration:

Rely on

MySQL depends on the libaio library. If you have not installed it first, run the following command:

shell> yum install libaio
Copy the code
  • Create mysql user

A system account that does not require login and is used to start the MySQL service

shell> groupadd mysql

shell> useradd -r -g mysql -s /bin/false mysql
Copy the code

Unzip and create links

Shell > CD /usr/local shell> tar ZXVF /path/to/mysql-5.8.0.23-linux-glibc2.12-x86_64.tar.gz shell> ln -s Mysql - 5.8.0.23 - Linux - glibc2.12 x86_64 / mysqlCopy the code
  • Create mysql-files directory

This step is not necessary. You can set the value of secure_file_priv to point to this directory (used to restrict data import and export operations).

shell> cd mysql

shell> mkdir mysql-files

shell> chown mysql:mysql mysql-files

shell> chmod 750 mysql-files
Copy the code
  • Initialize the

    shell> bin/mysqld –initialize –user=mysql

If the initialization error is as follows:

error while loading shared libraries: libnuma.so.1: cannot open shared object file: No such file or directory
Copy the code

Because libnuma is not installed (or 32-bit is installed by default), we need 64-bit here:

shell> yum install numactl.x86_64
Copy the code

If the initialization is successful, a line containing the initial password is returned. This line is required for the first login:

A temporary password is generated for root@localhost: 8M0ary878s*U
Copy the code
  • Enable SSL (optional)

    shell> bin/mysql_ssl_rsa_setup

  • Start the

    shell> bin/mysqld_safe –user=mysql &

To view the process, you can see some default parameters. You can modify these parameters in the configuration file by running the following command:

shell> ps -ef | grep mysql
Copy the code

The display is as follows:

root     14604 12719  0 00:03 pts/0    00:00:00 /bin/sh bin/mysqld_safe --user=mysql
mysql    14674 14604  0 00:03 pts/0    00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=VM_2_24_centos.err --pid-file=VM_2_24_centos.pid
Copy the code
  • Setting environment Variables

To avoid adding a path every time you run a mysql command, add the following to /etc/profile:

export PATH=$PATH:/usr/local/mysql/bin
Copy the code
  • Set as service

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

    shell> service mysqld start|stop|restart|status

  • Powered up

    shell> chkconfig –add mysqld

    shell> chkconfig –list mysqld

The display is as follows:

Mysqld 0: off 1: off 2: on 3: on 4: on 5: on 6: offCopy the code

Other configurations are the same as yum and will not be described here