1、下载解压安装包
A.下载解压
[root@iZuligp6e1dyzfZ local]# cd mysql/
[root@iZuligp6e1dyzfZ mysql]# mkdir mysql8.0
[root@iZuligp6e1dyzfZ mysql]# cd mysql8.0/
[root@iZuligp6e1dyzfZ mysql8.0]# wget https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.11-linux-glibc2.12-x86_64.tar.gz
[root@iZuligp6e1dyzfZ mysql8.0]# ll
[root@iZuligp6e1dyzfZ mysql8.0]# tar -zxvf mysql-8.0.11-linux-glibc2.12-x86_64.tar.gz
B.创建数据目录(个人习惯放在bin目录同目录)
[root@iZuligp6e1dyzfZ mysql8.0]# cd mysql-8.0.11-linux-glibc2.12-x86_64
[root@iZuligp6e1dyzfZ mysql-8.0.11-linux-glibc2.12-x86_64]# mkdir data
[root@iZuligp6e1dyzfZ mysql-8.0.11-linux-glibc2.12-x86_64]# ll
2、创建MySQL用户及用户组进行授权
[root@iZuligp6e1dyzfZ mysql-8.0.11-linux-glibc2.12-x86_64]# groupadd mysql
[root@iZuligp6e1dyzfZ mysql-8.0.11-linux-glibc2.12-x86_64]# useradd -g mysql mysql
[root@iZuligp6e1dyzfZ mysql-8.0.11-linux-glibc2.12-x86_64]# chown -R mysql.mysql /local/mysql/mysql8.0/mysql-8.0.11-linux-glibc2.12-x86_64
[root@iZuligp6e1dyzfZ mysql-8.0.11-linux-glibc2.12-x86_64]#
3、初始化MySQL数据目录
A.定位当前在MySQL的bin目录下
[root@iZuligp6e1dyzfZ mysql-8.0.11-linux-glibc2.12-x86_64]# ll
total 332
drwxr-xr-x 2 mysql mysql 4096 Jul 20 22:41 bin
drwxr-xr-x 2 mysql mysql 4096 Jul 20 22:41 data
drwxr-xr-x 2 mysql mysql 4096 Jul 20 22:41 docs
drwxr-xr-x 3 mysql mysql 4096 Jul 20 22:41 include
drwxr-xr-x 5 mysql mysql 4096 Jul 20 22:41 lib
-rw-r--r-- 1 mysql mysql 301518 Apr 8 2018 LICENSE
drwxr-xr-x 4 mysql mysql 4096 Jul 20 22:41 man
-rw-r--r-- 1 mysql mysql 687 Apr 8 2018 README
drwxr-xr-x 28 mysql mysql 4096 Jul 20 22:41 share
drwxr-xr-x 2 mysql mysql 4096 Jul 20 22:41 support-files
B.执行初始化数据命令(遇到缺少库)
[root@iZuligp6e1dyzfZ mysql-8.0.11-linux-glibc2.12-x86_64]# ./bin/mysqld --user=mysql --basedir=/local/mysql/mysql8.0/mysql-8.0.11-linux-glibc2.12-x86_64/ --datadir=/local/mysql/mysql8.0/mysql-8.0.11-linux-glibc2.12-x86_64/data/ --initializ
e
./bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory
[root@iZuligp6e1dyzfZ mysql-8.0.11-linux-glibc2.12-x86_64]# bin/mysqld --user=mysql --basedir=/local/mysql/mysql8.0/mysql-8.0.11-linux-glibc2.12-x86_64/ --datadir=/local/mysql/mysql8.0/mysql-8.0.11-linux-glibc2.12-x86_64/data/ --initialize
bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory
C.通过yum安装libaio库
[root@iZuligp6e1dyzfZ mysql-8.0.11-linux-glibc2.12-x86_64]# yum install -y libaio
D.再次执行初始化MySQL命令成功执行(生成临时初始密码)
[root@iZuligp6e1dyzfZ mysql-8.0.11-linux-glibc2.12-x86_64]# bin/mysqld --user=mysql --basedir=/local/mysql/mysql8.0/mysql-8.0.11-linux-glibc2.12-x86_64/ --datadir=/local/mysql/mysql8.0/mysql-8.0.11-linux-glibc2.12-x86_64/data/ --initialize
4、MySQL配置文件
编辑MySQL配置文件/etc/my.cnf
[root@iZuligp6e1dyzfZ mysql-8.0.11-linux-glibc2.12-x86_64]# vim /etc/my.cnf
/etc/my.cnf配置如下内容
[client]
port=3306
# mysql socket 文件存放地址
socket=/var/lib/mysql/mysql.sock
# 默认字符集
default-character-set=utf8mb4
[mysqld]
port=3306
character_set_server=utf8mb4
basedir=/local/mysql/mysql8.0/mysql-8.0.11-linux-glibc2.12-x86_64/
datadir=/local/mysql/mysql8.0/mysql-8.0.11-linux-glibc2.12-x86_64/data/
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
[mysqld_safe]
# log-error=/var/log/mariadb/mariadb.log
# pid-file=/var/run/mariadb/mariadb.pid
#
# include all files from the config directory
#
# !includedir /etc/my.cnf.d
5、建立MySQL服务
A.当前操作应在bin目录的根目录(当前目录有support-files文件夹)
[root@iZuligp6e1dyzfZ mysql-8.0.11-linux-glibc2.12-x86_64]# ll
total 332
drwxr-xr-x 2 mysql mysql 4096 Jul 20 22:41 bin
drwxr-xr-x 5 mysql mysql 4096 Jul 20 22:57 data
drwxr-xr-x 2 mysql mysql 4096 Jul 20 22:41 docs
drwxr-xr-x 3 mysql mysql 4096 Jul 20 22:41 include
drwxr-xr-x 5 mysql mysql 4096 Jul 20 22:41 lib
-rw-r--r-- 1 mysql mysql 301518 Apr 8 2018 LICENSE
drwxr-xr-x 4 mysql mysql 4096 Jul 20 22:41 man
-rw-r--r-- 1 mysql mysql 687 Apr 8 2018 README
drwxr-xr-x 28 mysql mysql 4096 Jul 20 22:41 share
drwxr-xr-x 2 mysql mysql 4096 Jul 20 22:41 support-files
B.添加MySQL到系统服务
[root@iZuligp6e1dyzfZ mysql-8.0.11-linux-glibc2.12-x86_64]# cp -a ./support-files/mysql.server /etc/init.d/mysqld
[root@iZuligp6e1dyzfZ mysql-8.0.11-linux-glibc2.12-x86_64]# cd /etc/init.d
[root@iZuligp6e1dyzfZ init.d]# chmod +x /etc/init.d/mysqld
[root@iZuligp6e1dyzfZ init.d]# chkconfig --add mysqld
[root@iZuligp6e1dyzfZ init.d]# chkconfig --list mysqld
Note: This output shows SysV services only and does not include native
systemd services. SysV configuration data might be overridden by native
systemd configuration.
If you want to list systemd services use 'systemctl list-unit-files'.
To see services enabled on particular target use
'systemctl list-dependencies [target]'.
mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off
C.启动服务报错
[root@iZuligp6e1dyzfZ bin]# service mysql start
[root@iZuligp6e1dyzfZ bin]# systemctl status -l mysqld.service
● mysqld.service - LSB: start and stop MySQL
Loaded: loaded (/etc/rc.d/init.d/mysqld; bad; vendor preset: disabled)
Active: failed (Result: exit-code) since Tue 2020-07-21 09:16:33 CST; 1h 16min ago
Docs: man:systemd-sysv-generator(8)
Process: 23849 ExecStart=/etc/rc.d/init.d/mysqld start (code=exited, status=1/FAILURE)
Jul 21 09:16:32 iZuligp6e1dyzfZ systemd[1]: Starting LSB: start and stop MySQL...
Jul 21 09:16:32 iZuligp6e1dyzfZ mysqld[23849]: Starting MySQL.Logging to '/local/mysql/mysql8.0/mysql-8.0.11-linux-glibc2.12-x86_64/data/iZuligp6e1dyzfZ.err'.
Jul 21 09:16:32 iZuligp6e1dyzfZ mysqld[23849]: 2020-07-21T01:16:32.992054Z mysqld_safe Directory '/var/lib/mysql' for UNIX socket file don't exists.
Jul 21 09:16:33 iZuligp6e1dyzfZ mysqld[23849]: The server quit without updating PID file (/local/mysql/mysql8.0/mysql-8.0.11-linux-glibc2.12-x86_64/data//iZuligp6e1dyzfZ.pid).[FAILED]
Jul 21 09:16:33 iZuligp6e1dyzfZ systemd[1]: mysqld.service: control process exited, code=exited status=1
Jul 21 09:16:33 iZuligp6e1dyzfZ systemd[1]: Failed to start LSB: start and stop MySQL.
Jul 21 09:16:33 iZuligp6e1dyzfZ systemd[1]: Unit mysqld.service entered failed state.
Jul 21 09:16:33 iZuligp6e1dyzfZ systemd[1]: mysqld.service failed.
①根据启动错误日志可以关联到/etc/my.cnf文件的socket=/var/lib/mysql/mysql.sock配置
②创建目录进行mysql用户操作赋权
[root@iZuligp6e1dyzfZ lib]# mkdir mysql
[root@iZuligp6e1dyzfZ lib]# chmod 777 /var/lib/mysql/
D.MySQL服务启动成功
[root@iZuligp6e1dyzfZ bin]# service mysql start
Redirecting to /bin/systemctl start mysql.service
E.客户端连接成功(操作前会提示重置密码)
①将mysql链接至alias
[root@iZuligp6e1dyzfZ bin 14:26]# alias mysql=/local/dev/mysql/mysql-8.0.11-linux-glibc2.12-x86_64/bin/mysql
②连接登录MySQL
[root@iZuligp6e1dyzfZ bin]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.11
Copyright (c) 2000, 2018, 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.
mysql> show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '000000';
Query OK, 0 rows affected (0.01 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
mysql>
6、设置远程访问
A.在/etc/my.cnf配置远程访问的权限插件
[root@iZuligp6e1dyzfZ bin]# vim /etc/my.cnf
[client]
port=3306
# mysql socket 文件存放地址
socket=/var/lib/mysql/mysql.sock
# 默认字符集
default-character-set=utf8mb4
[mysqld]
port=3306
character_set_server=utf8mb4
basedir=/local/mysql/mysql8.0/mysql-8.0.11-linux-glibc2.12-x86_64/
datadir=/local/mysql/mysql8.0/mysql-8.0.11-linux-glibc2.12-x86_64/data/
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
default_authentication_plugin=mysql_native_password
[mysqld_safe]
# log-error=/var/log/mariadb/mariadb.log
# pid-file=/var/run/mariadb/mariadb.pid
#
# include all files from the config directory
#
# !includedir /etc/my.cnf.d
~
B.设置远程主机访问以及新的密码加密规则并刷新权限
[root@iZuligp6e1dyzfZ bin]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 8.0.11 MySQL Community Server - GPL
Copyright (c) 2000, 2018, 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.
mysql> CREATE USER 'root'@'%' IDENTIFIED BY 'root';
Query OK, 0 rows affected (0.05 sec)
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%'WITH GRANT OPTION;
Query OK, 0 rows affected (0.07 sec)
mysql> ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '000000';
Query OK, 0 rows affected (0.09 sec)
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
mysql>