Home  >  Article  >  Operation and Maintenance  >  Detailed introduction to installing mysql 8.0 on Linux (code example)

Detailed introduction to installing mysql 8.0 on Linux (code example)

不言
不言forward
2018-12-15 11:05:216736browse

This article brings you a detailed introduction to the installation method of mysql 8.0 on Linux (code example). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

After some hard work to download the mysql file, we can start the installation of Mysql8.0.

Unzip the file

// 解压文件生成两个xz格式的压缩文件
$ tar -xvf mysql-8.0.13-linux-glibc2.12-x86_64.tar
mysql-router-8.0.13-linux-glibc2.12-x86_64.tar.xz
mysql-test-8.0.13-linux-glibc2.12-x86_64.tar.xz
// 我们需要先删掉/移除原有文件,才可以继续解压,因为解压出来的.tar文件和.tar.xz文件重名
mv mysql-8.0.13-linux-glibc2.12-x86_64.tar ../
xz -d mysql-router-8.0.13-linux-glibc2.12-x86_64.tar.xz
tar -xvf mysql-8.0.13-linux-glibc2.12-x86_64.tar
// 为了方便查找,改个名字
mv mysql-8.0.13-linux-glibc2.12-x86_64.tar mysql8

Environment configuration

We need a dedicated mysql process to start user and permission management:

// 创建mysql系统用户和用户组
useradd -r mysql
// 给予安装目录mysql权限
chown mysql:mysql -R mysql8

Configure your own mysql configuration file. Because I have multiple Mysql libraries, I manually specify many parameters:

[client]
socket=/home/work/lnmp/mysql8/tmp/mysql.sock
default-character-set=utf8

[mysql]
basedir=/home/work/lnmp/mysql8/
datadir=/home/work/lnmp/mysql8/data/
socket=/home/work/lnmp/mysql8/tmp/mysql.sock
port=3306
user=mysql
# 指定日志时间为系统时间
log_timestamps=SYSTEM
log-error=/home/work/lnmp/mysql8/log/mysql.err
# 指定字符集为utf8,因为mysql8.0中的默认字符集为utfmb4,会和其他程序引起兼容性问题
default-character-set=utf8

[mysqld]
basedir=/home/work/lnmp/mysql8/
datadir=/home/work/lnmp/mysql8/data/
socket=/home/work/lnmp/mysql8/tmp/mysql.sock
port=3306
user=mysql
log_timestamps=SYSTEM
collation-server = utf8_unicode_ci
character-set-server = utf8
# 指定默认认证的加密方式,mysql8.0中默认方式为caching_sha2_password,引起老版本兼容性问题
default_authentication_plugin= mysql_native_password

[mysqld_safe]
log-error=/home/work/lnmp/mysql8/log/mysqld_safe.err
pid-file=/home/work/lnmp/mysql8/tmp/mysqld.pid
socket=/home/work/lnmp/mysql8/tmp/mysql.sock

[mysql.server]
basedir=/home/work/lnmp/mysql8
socket=/home/work/lnmp/mysql8/tmp/mysql.sock

[mysqladmin]                                                                                                                                  
socket=/home/work/lnmp/mysql8/tmp/mysql.sock

In this, I specify the path of the error log. In the next operation, if When an error occurs, in addition to checking the error displayed on the terminal, remember to check the error log for detailed information.

Because I specified some files, they need to be created in advance:

mkdir log
touch log/mysql.err
touch log/mysqld_safe.err
mkdir tmp
chown mysql:mysql -R ../*

Database initialization

If we do not initialize, use bin/mysqld_safe to start directly An error will be reported because we need to initialize the mysql environment. At this time, refer to the article:

$ bin/mysqld --initialize --user=mysql --basedir=/home/work/lnmp/mysql8/ --datadir=/home/work/lnmp/mysql8/data/
2018-12-13T06:15:03.159123Z 0 [System] [MY-013169] [Server] /home/work/lnmp/mysql8/bin/mysqld (mysqld 8.0.13) initializing of server in progress as process 1190
2018-12-13T06:15:05.255817Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: !/7oy-u%(XfZ
2018-12-13T06:15:06.135143Z 0 [System] [MY-013170] [Server] /home/work/lnmp/mysql8/bin/mysqld (mysqld 8.0.13) initializing of server has completed

The prompt prompts that we have created the root user and a temporary password, and the initialization is successful.

Start the database

At this time, according to the official documentation, we use the mysqld_safe command to start:

$ bin/mysqld_safe
2018-12-13T06:16:58.604154Z mysqld_safe Logging to '/home/work/lnmp/mysql8/log/mysql.err'.
2018-12-13T06:16:58.629249Z mysqld_safe Starting mysqld daemon with databases from /home/work/lnmp/mysql8/data

Open the database

The database process has been started, and we can use the mysql database normally in the new terminal:

$ ps aux | grep mysql 
root      2141  0.0  0.0 815844  5328 pts/0    S+   14:16   0:00 /bin/sh bin/mysqld_safe
mysql     2319  1.0  0.5 1997492 374448 pts/0  Sl+  14:16   0:00 /home/work/lnmp/mysql8/bin/mysqld --basedir=/home/work/lnmp/mysql8/ --datadir=/home/work/lnmp/mysql8/data --plugin-dir=/home/work/lnmp/mysql8//lib/plugin --user=mysql --log-error=/home/work/lnmp/mysql8/log/mysql.err --pid-file=/home/work/lnmp/mysql8/tmp/mysqld.pid --socket=/home/work/lnmp/mysql8/tmp/mysql.sock --port=3306
work     25258  0.0  0.0 105356   824 pts/1    S+   14:18   0:00 grep mysql

But an error is reported when using the mysql command directly:

$ bin/mysql -uroot
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

According to the description of the role of mysql.sock, we Just specify the mysql.sock path:

bin/mysql -S /home/work/lnmp/mysql8/tmp/mysql.sock -h localhost -uroot -p
Enter password:

Change the initial password

After we open the database and use any command, we will be prompted to change the initial temporary password:

mysql> show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

Change password:

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
Query OK, 0 rows affected (0.05 sec)

Reconnect and the new password will take effect.

Link global command

At this time, we can only use the path /home/work/lnmp/mysql8/bin/mysql or relative path to call mysql. The link needs to be Global command:

$ ln -s /home/work/lnmp/mysql8/bin/mysql /usr/bin/
$ ln -s /home/work/lnmp/mysql8/bin/mysql_safe /usr/bin/

Specify socket file

In version 8.0, I tried many methods to modify the default socket path queried by the mysql command in my.cnf. Still unsuccessful, so we had to link the socket file in the default path:

ln -s /home/work/lnmp/mysql8/tmp/mysql.sock /tmp/

Then we will call the mysql command and no error will be reported.

Restart the database

During the process, we may need to restart the database when debugging parameters:

// 杀死进程,相当于停止
cat tmp/mysqld.pid | xargs kill
// 正常启动mysql
bin/mysqld_safe  --defaults-file=/home/work/lnmp/mysql8/my.cnf

Add new users & grant permissions

In mysql8, I created a user and assigned a value, and the result was an error:

mysql > grant all privileges on *.* to root@localhost indentified by '123456';
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'indentified by '123456'' at line 1

According to online information, it is because Mysql8 separates the operations of creating an account and granting permissions:

// 创建账户
create user '用户名'@'访问主机' identified by '密码';
// 赋予权限(修改权限时在后面加with grant option)
grant 权限列表 on 数据库 to '用户名'@'访问主机' ;
// 刷新权限
flush privileges;

Summary

After decompressing the file, create mysql users and user groups, configure the my.cnf configuration file, modify permissions, initialize and start the database, open the database, change the initial password, and connect global commands and specify the socket file path, all installation work has been completed.

The above is the detailed content of Detailed introduction to installing mysql 8.0 on Linux (code example). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete