MySQL下载
https://dev.mysql.com/downloads/mysql/
MySQL官网
https://www.mysql.com/
Centos下MySQL解压版离线安装教程
1.准备工作:
-
载系统自带的 Mariadb、MySQL
#卸载系统自带的 Mariadb
[root@localhost opt]# rpm -qa|grep mariadb
mariadb-libs-5.5.68-1.el7.x86_64
[root@localhost opt]# rpm -e --nodeps mariadb-libs-5.5.68-1.el7.x86_64
#卸载etc目录下的my.cnf 文件
[root@localhost opt]# rm -rf /etc/my.cnf
#检查MySQL是否存在
root@localhost opt]# rpm -qa | grep mysql
2.检查用户组
-
检查 MySQL组和用户是否存在,如果没有则创建
#查看MySQL用户组
[root@localhost opt]# cat /etc/group | grep mysql
#创建MySQL用户组
[root@localhost opt]# groupadd mysql
#创建MySQL用户,并加入MySQL用户组
[root@localhost opt]# useradd -g mysql mysql
3.下载或者上传MySQL
#进入目录上传或者下载MySQL安装包
#下载
wget -c https://downloads.mysql.com/archives/get/p/23/file/mysql-8.0.25-el7-x86_64.tar.gz
#如果下载不下来,手动下载后放到/usr/local/mysql目录下,并解压
[root@localhost opt]# tar -zxvf mysql-8.0.25-el7-x86_64.tar.gz
#注:xz解压
[root@localhost opt]# xz -d mysql-8.0.31-linux-glibc2.12-x86_64.tar.xz
[root@localhost opt]# tar -xf mysql-8.0.31-linux-glibc2.12-x86_64.tar
#重命名解压包
[root@localhost opt]# mv mysql-8.0.31-linux-glibc2.12-x86_64/ mysql-8.0.31
-
更改所属的组和用户
[root@localhost opt]# chown -R mysql mysql-8.0.31/
[root@localhost opt]# chgrp -R mysql mysql-8.0.31/
4.创建data与log文件夹
-
进入mysql文件夹,创建一个data文件夹存放数据、log文件夹存放日志
[root@localhost mysql-8.0.31]# mkdir data
[root@localhost mysql-8.0.31]# mkdir log
[root@localhost mysql-8.0.31]# chown -R mysql:mysql data
[root@localhost mysql-8.0.31]# chown -R mysql:mysql log
5.新建配置文件 my.cnf
[root@localhost mysql-8.0.31]# vim /etc/my.cnf
[root@localhost mysql-8.0.31]# chown 777 /etc/my.cnf
-
修改配置文件 my.cnf
[mysql]
#设置mysql客户端默认字符集
default-character-set=utf8
[mysqld]
#禁用dns解析
skip-name-resolve
#设置9001端口
port = 9001
#设置mysql的安装目录
basedir=/data/mysql-8.0.31
#设置mysql数据库的数据的存放目录
datadir=/data/mysql-8.0.31/data
#允许最大连接数
max_connections=200
#允许连接失败的次数
max_connect_errors=10
#服务端使用的字符集默认为8比特编码的latin1字符集
character-set-server=utf8
#创建新表时将使用的默认存储引擎
default-storage-engine=INNODB
#默认使用“mysql_native_password”插件认证
default_authentication_plugin=mysql_native_password
#设置服务器接收数据包大小
max_allowed_packet=64M
#配置错误日志
log_error=/data/mysql-8.0.31/log/mysql-error.log
#配置进程文件
pid-file=/data/mysql-8.0.31/mysql.pid
[client]
#设置mysql客户端连接服务端时默认使用的端口
port=9001
default-character-set=utf8
#设置客户端发送数据包大小
max_allowed_packet=64M
6.初始化MySQL
-
初始化MySQL,添加mysql服务,并设置开机自启
#初始化MySQL
[root@localhost mysql-8.0.31]# ./bin/mysqld --user=mysql --basedir=/opt/mysql-8.0.31/ --datadir=/opt/mysql-8.0.31/data/ --initialize
#添加mysql服务,并设置开机自启
#将MySQL的服务脚本放到系统服务中
[root@localhost mysql-8.0.31]# cp -a /opt/mysql-8.0.31/support-files/mysql.server /etc/init.d/mysql
#授予可执行权限
[root@localhost mysql-8.0.31]# chmod +x /etc/rc.d/init.d/mysql
#添加mysql服务
[root@localhost mysql-8.0.31]# chkconfig --add mysql
#使MySQL开机自启
[root@localhost mysql-8.0.31]# chkconfig --level 345 mysql on
#创建mysql下bin目录的同步链接,设置后无需进入mysql的bin目录就可以执行mysql命令
[root@localhost mysql-8.0.31]# ln -s /opt/mysql-8.0.31/bin/mysql /usr/bin
7.启动MySQL服务
#启动MySQL服务
[root@localhost mysql-8.0.31]# service mysql start
Starting MySQL.... SUCCESS!
#查看状态
[root@localhost mysql-8.0.31]# service mysql status
SUCCESS! MySQL running (6472)
#停止MySQL服务
service mysql stop
#查看状态
service mysql status
#重启MySQL命令
service mysql restart
8.登录MySQL并配置
-
查看临时密码,并登陆MySQL
[root@localhost ~]# cat /opt/mysql-8.0.31/log/mysql-error.log
2022-11-21T03:11:58.379381Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: ;&kPlxCXF9Hi
-
登录,修改root密码并设置远程访问
#登录
[root@localhost ~]# mysql -P5306 -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 8.0.31
Copyright (c) 2000, 2022, 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> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'Ys_20221107';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
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> update user set host = '%' where user = 'root';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> select host, user from user;
+-----------+------------------+
| host | user |
+-----------+------------------+
| % | root |
| localhost | mysql.infoschema |
| localhost | mysql.session |
| localhost | mysql.sys |
+-----------+------------------+
4 rows in set (0.00 sec)
mysql>quit
版权声明:本文为weixin_44249248原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。