1、MySQL 8.0.26 下载
官方网站下载 MySQL 8.0.26 安装包,下载地址:mysql8.0.26
本案例采用Linux 64位操作系统进行讲解,通过wget命令下载安装包。
使用df -lh命令查看,磁盘大小,尽量安装在比较大的磁盘下,防止空间不够使用。
[root@VM-0-4-centos home]# df -lh
Filesystem Size Used Avail Use% Mounted on
devtmpfs 3.9G 0 3.9G 0% /dev
tmpfs 3.9G 24K 3.9G 1% /dev/shm
tmpfs 3.9G 604K 3.9G 1% /run
tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup
/dev/vda1 50G 3.8G 44G 9% /
tmpfs 783M 0 783M 0% /run/user/0
通过上述命令可以看出根目录空间比较充足,那么就把安装包下载到home目录了,执行wget命令。
[root@VM-0-4-centos home]# wget https://downloads.mysql.com/archives/get/p/23/file/mysql-8.0.26-linux-glibc2.12-x86_64.tar.xz
2、解压缩文件
解压 mysql-8.0.26-linux-glibc2.12-x86_64.tar.xz 文件,使用tar -xvf mysql-8.0.26-linux-glibc2.12-x86_64.tar.xz 命令。
[root@VM-0-4-centos home]# tar -xvf mysql-8.0.26-linux-glibc2.12-x86_64.tar.xz
mysql-8.0.26-linux-glibc2.12-x86_64/bin/
mysql-8.0.26-linux-glibc2.12-x86_64/bin/myisam_ftdump
mysql-8.0.26-linux-glibc2.12-x86_64/bin/myisamchk
mysql-8.0.26-linux-glibc2.12-x86_64/bin/myisamlog
mysql-8.0.26-linux-glibc2.12-x86_64/bin/myisampack
mysql-8.0.26-linux-glibc2.12-x86_64/bin/mysql
....
mysql-8.0.26-linux-glibc2.12-x86_64/share/
mysql-8.0.26-linux-glibc2.12-x86_64/share/install_rewriter.sql
mysql-8.0.26-linux-glibc2.12-x86_64/share/uninstall_rewriter.sql
3、移动文件
将压缩包移动到usr/local目录下,并重命名文件为mysql,使用mv /home/mysql-8.0.26-linux-glibc2.12-x86_64 /usr/local/mysql命令。
[root@VM-0-4-centos home]# mv mysql-8.0.26-linux-glibc2.12-x86_64 /usr/local/mysql
[root@VM-0-4-centos home]# cd /usr/local/mysql/
[root@VM-0-4-centos mysql]# ls
bin docs include lib LICENSE man README share support-files
[root@VM-0-4-centos mysql]# cd ..
[root@VM-0-4-centos local]# ls
bin etc games include lib lib64 libexec mysql qcloud sbin share src yd.socket.server
[root@VM-0-4-centos local]#
4、创建数据存放目录
在mysql根目录下新建一个目录data,主要用于存放数据库数据文件,使用mkdir data命令。
[root@VM-0-4-centos local]# cd mysql/
[root@VM-0-4-centos mysql]# mkdir data
[root@VM-0-4-centos mysql]# ls
bin data docs include lib LICENSE man README share support-files
[root@VM-0-4-centos mysql]#
5、创建用户组和用户
创建mysql用户组和mysql用户,使用groupadd mysql和useradd -g mysql mysql命令。
[root@VM-0-4-centos mysql]# groupadd mysql
[root@VM-0-4-centos mysql]# useradd -g mysql mysql
6、改变mysql目录权限
修改mysql目录权限,可以使用chown -R mysql.mysql /usr/local/mysql/命令。
修改mysql目录权限也可以通过chown -R mysql .和chgrp -R mysql .两个命令。注意:这两个命令都需要执行的,还有那个点不要忽视掉。
7、数据库初始化
数据库初始化./bin/mysqld --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --initialize命令,得到临时密码。
[root@VM-0-2-centos mysql]# ./bin/mysqld --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --initialize
2022-01-16T07:32:18.729960Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is deprecated and will be removed in a future release.
2022-01-16T07:32:18.729960Z 0 [System] [MY-013169] [Server] /usr/local/mysql/bin/mysqld (mysqld 8.0.26) initializing of server in progress as process 7691
2022-01-16T07:32:18.740975Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2022-01-16T07:32:19.800287Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2022-01-16T07:32:21.721672Z 0 [Warning] [MY-013746] [Server] A deprecated TLS version TLSv1 is enabled for channel mysql_main
2022-01-16T07:32:21.722106Z 0 [Warning] [MY-013746] [Server] A deprecated TLS version TLSv1.1 is enabled for channel mysql_main
2022-01-16T07:32:21.787669Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: :e0l?pC>Z%R.
需要注意:记录一下mysql数据库的临时密码:e0l?pC>Z%R.,后面安装步骤是需要使用的,否则需要重新安装数据库或其他方式获取密码,此处问题省略。
8、修改my.cnf文件
修改my.cnf文件,使用vim /etc/my.cnf命令。
[mysqld]
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
socket = /usr/local/mysql/mysql.sock
character-set-server=utf8
port = 3306
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
[client]
socket = /usr/local/mysql/mysql.sock
default-character-set=utf8
#[mysqld]
#datadir=/var/lib/mysql
#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
直接将上述配置内容复制到my.cnf文件中,或者自行修改,然后执行:wq命令,保存并退出。
9、创建mysql服务
1)将mysql.server启动文件复制到/etc/init.d目录,使用cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld命令。
2)赋予权限,使用chmod +x /etc/rc.d/init.d/mysqld命令;
3)使用chkconfig --add mysqld创建mysql服务。
[root@VM-0-4-centos mysql]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@VM-0-4-centos mysql]# chmod +x /etc/rc.d/init.d/mysqld
[root@VM-0-4-centos mysql]# chkconfig --add mysqld
检查mysql服务是否生效,使用chkconfig --list mysqld命令。
[root@VM-0-4-centos mysql]# 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
10、配置全局环境变量
编辑/etc/profile文件,使用vim /etc/profile命令,在profile文件中添加如下两行配置,使用:wq命令保存后退出。
export PATH=$PATH:/usr/local/mysql/bin:/usr/local/mysql/lib
export PATH
设置环境变量立即生效使用source /etc/profile命令。
[root@VM-0-4-centos ~]# source /etc/profile
[root@VM-0-4-centos ~]#
11、启动mysql服务
启动mysql服务,使用service mysql start命令;使用service mysql status命令,查看是否启动成功。
[root@VM-0-4-centos ~]# service mysql start
Redirecting to /bin/systemctl start mysql.service
[root@VM-0-4-centos ~]# service mysql status
Redirecting to /bin/systemctl status mysql.service
● mysqld.service - LSB: start and stop MySQL
Loaded: loaded (/etc/rc.d/init.d/mysqld; bad; vendor preset: disabled)
Active: active (running) since Sun 2022-01-16 17:17:55 CST; 8s ago
Docs: man:systemd-sysv-generator(8)
Process: 27231 ExecStart=/etc/rc.d/init.d/mysqld start (code=exited, status=0/SUCCESS)
CGroup: /system.slice/mysqld.service
├─27242 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/usr/local/mysql/data --pid-file=/usr/local/mysql/data/VM-0-4-centos.pid
└─27408 /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-0-4-cent...
Jan 16 17:17:54 VM-0-4-centos systemd[1]: Starting LSB: start and stop MySQL...
Jan 16 17:17:54 VM-0-4-centos mysqld[27231]: Starting MySQL.Logging to '/usr/local/mysql/data/VM-0-4-centos.err'.
Jan 16 17:17:55 VM-0-4-centos mysqld[27231]: SUCCESS!
Jan 16 17:17:55 VM-0-4-centos systemd[1]: Started LSB: start and stop MySQL.
12、登录mysql修改密码
登录mysql数据库,使用mysql -uroot -p密码命令,临时密码是:e0l?pC>Z%R.
[root@VM-0-4-centos ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 13
Server version: 8.0.26
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
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>
修改mysql临时密码,也就是将第七步数据库初始化生成的临时密码修改成自己需要设置的密码。
修改mysql数据库密码,使用ALTER USER ‘root’@‘localhost’ IDENTIFIED WITH mysql_native_password BY ‘123456’;命令。
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456';
Query OK, 0 rows affected (0.01 sec)
mysql>
注意:此处123456修改为自己的需要密码即可。
13、设置mysql远程登录
1)切换数据库,使用use mysql;命令。
2)修改mysql库中host值,使用update user set host=‘%’ where user=‘root’ limit 1;命令。
3)刷新mysql权限,使用flush privileges;命令。
mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> update user set host='%' where user='root' limit 1;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql>
14、mysql客户端连接数据库
客户端连接mysql数据库,连接名(自定义名称)、主机(IP)、端口号及用户名和密码,点击测试连接按钮,显示连接成功即可。