1.下载mysql-8.0.18-linux-glibc2.12-x86_64.tar.xz的安装包
云盘下载链接:https://pan.baidu.com/s/1f8Eebiu2hP2-rqxKYUq2wA
提取码:rcna
2.使用rz -be(或者xftp工具) 上传mysql安装包到/usr/local 目录
[root@localhost local]# cd /usr/local
[root@localhost local]# pwd
/usr/local
[root@localhost local]# rz -be
-bash: rz: 未找到命令
-bash: rz: 未找到命令 ,未安装lrzsz ,安装lrzsz
[root@localhost local]# yum -y install lrzsz
上传出现乱码时断开xshell重新连接,再上传。
2.解压mysql-8.0.18-linux-glibc2.12-x86_64.tar.xz(注意解压.xz压缩包是 -xvf参数非-zxvf)
[root@localhost local]# tar -xvf mysql-8.0.18-linux-glibc2.12-x86_64.tar.xz
3.将解压的文件重命名为mysql
[root@localhost local]# mv mysql-8.0.18-linux-glibc2.12-x86_64 mysql
4.进入到/usr/local目录下,创建用户和用户组并授权
[root@localhost local]# pwd
/usr/local
[root@localhost local]# groupadd mysql
[root@localhost local]# useradd -r -g mysql mysql
[root@localhost local]# chown -R mysql:mysql ./ #授权
[root@localhost local]#
5.在/usr/local/mysql目录下,创建data文件夹
[root@localhost mysql]# mkdir data
6.初始化数据库,并会自动生成随机密码,记下等下登陆要用
[root@localhost mysql]# bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
初始化时出现了报错:
安装 libaio ,安装成功后,继续运行数据库的初始化命令。
[root@localhost mysql]# rpm -qa|grep libaio
[root@localhost mysql]# yum -y install libaio-devel.x86_64
[root@localhost mysql]# chown -R root:root ./
[root@localhost mysql]# chown -R mysql:mysql data
8.创建my.cnf文件
[root@localhost mysql]# cd support-files/
[root@localhost support-files]# touch my-default.cnf
[root@localhost support-files]# chmod 777 ./my-default.cnf
[root@localhost support-files]# ll
总用量 20
-rwxrwxrwx. 1 root root 0 1月 21 18:46 my-default.cnf
-rwxr-xr-x. 1 root root 1061 9月 20 20:08 mysqld_multi.server
-rwxr-xr-x. 1 root root 2048 9月 20 20:08 mysql-log-rotate
-rwxr-xr-x. 1 root root 10576 9月 20 20:08 mysql.server
[root@localhost support-files]# cp support-files/my-default.cnf /etc/my.cnf
cp: 无法获取"support-files/my-default.cnf" 的文件状态(stat): 没有那个文件或目录
[root@localhost support-files]# cp my-default.cnf /etc/my.cnf
cp:是否覆盖"/etc/my.cnf"? yes
9.配置my.cnf
[root@localhost support-files]# vim /etc/my.cnf
配置为以下查看内容
[root@localhost support-files]# cat /etc/my.cnf
[client]
port = 3306
socket = /tmp/mysql.sock
[mysqld]
init-connect='SET NAMES utf8'
basedir=/usr/local/mysql #根据自己的安装目录填写
datadir=/usr/local/mysql/data #根据自己的mysql数据目录填写
socket=/tmp/mysql.sock
max_connections=200 # 允许最大连接数
character-set-server=utf8 # 服务端使用的字符集默认为8比特编码的latin1字符集
default-storage-engine=INNODB # 创建新表时将使用的默认存储引擎
10.开机自启,进入/usr/local/mysql/support-files进行设置
[root@localhost support-files]# cp mysql.server /etc/init.d/mysql
[root@localhost support-files]# chmod +x /etc/init.d/mysql
11.注册服务
[root@localhost support-files]# chkconfig --add mysql
[root@localhost support-files]# chkconfig --list mysql
注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。
要列出 systemd 服务,请执行 'systemctl list-unit-files'。
查看在具体 target 启用的服务请执行
'systemctl list-dependencies [target]'。
mysql 0:关 1:关 2:开 3:开 4:开 5:开 6:关
[root@localhost support-files]#
12.etc/ld.so.conf要配置路径,不然报错
[root@localhost support-files]# vim /etc/ld.so.conf
添加如下内容:
/usr/local/mysql/lib
13.配置环境变量
[root@localhost support-files]# vim /etc/profile
添加如下内容:
#MYSQL ENVIRONMENT
export PATH=$PATH:/usr/local/mysql/bin:/usr/local/mysql/lib
14.更新环境变量
[root@localhost support-files]# source /etc/profile
15.启动mysql服务
[root@localhost ~]# service mysql start
16.登录,密码为上面第6步随机生成得密码(输入时不显示)
修改 'root'@'localhost' 的密码
mysql> alter user 'root'@'localhost' identified by 'root123';
Query OK, 0 rows affected (0.02 sec)
17.开启允许远程登录
mysql> use mysql; #如果报以下该错误
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
mysql> ALTER USER USER() IDENTIFIED BY 'root'; #解决方式
Query OK, 0 rows affected (0.01 sec)
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> select host, user, authentication_string, plugin from user;#查看用户信息
+-----------+------------------+------------------------------------------------------------------------+-----------------------+
| host | user | authentication_string | plugin |
+-----------+------------------+------------------------------------------------------------------------+-----------------------+
| localhost | mysql.infoschema | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password |
| localhost | mysql.session | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password |
| localhost | mysql.sys | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password |
| localhost | root | $A$005$ CREATE USER 'root'@'%' IDENTIFIED BY 'root'; #创建任意ip连接root用户
Query OK, 0 rows affected (0.01 sec)
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION; #授权root用户可以远程登陆
Query OK, 0 rows affected (0.01 sec)
mysql> flush privileges;#立即生效
Query OK, 0 rows affected (0.04 sec)
mysql> ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'root123';#修改'root'@'%'用户密码
Query OK, 0 rows affected (0.00 sec)
mysql> FLUSH PRIVILEGES;#立即生效
Query OK, 0 rows affected (0.02 sec)
mysql> exit;#退出
Bye
[root@localhost ~]# service mysql restart #重启mysql服务
Shutting down MySQL.. SUCCESS!
Starting MySQL.. SUCCESS!
[root@localhost ~]#
防火墙开放3306端口
[root@localhost ~]# firewall-cmd --zone=public --add-port=3306/tcp --permanent #开放3306端口
success
[root@localhost ~]# systemctl restart firewalld.service #重启防火墙
[root@localhost ~]# firewall-cmd --list-ports #查看是否开放成功
3306/tcp
至此全部配置完成,远程工具即可连接。
参考自:https://blog.csdn.net/weixin_42109071/article/details/89375748