文章目录
Linux中MySQL安装
CentOS7
MySQL8.0.18
一、rpm安装
1、官网下载
- https://downloads.mysql.com/archives/community/
2、上传
[root@mycentos7 ~]# mkdir mypackage
[root@mycentos7 ~]# cd mypackage
[root@mycentos7 mypackage]# ll
总用量 813336
-rw-r--r--. 1 root root 684851200 9月 3 12:46 mysql-8.0.18-1.el7.x86_64.rpm-bundle.tar
3、检查与卸载数据库
[root@mycentos7 mypackage]# rpm -qa|grep mariadb
mariadb-libs-5.5.68-1.el7.x86_64
[root@mycentos7 mypackage]# rpm -e --nodeps mariadb-libs
[root@mycentos7 mypackage]# rpm -qa|grep mariadb
[root@mycentos7 ~]# rpm -qa|grep mysql
4、解压安装
[root@mycentos7 mypackage]# tar -xvf mysql-8.0.18-1.el7.x86_64.rpm-bundle.tar
mysql-community-libs-8.0.18-1.el7.x86_64.rpm
mysql-community-devel-8.0.18-1.el7.x86_64.rpm
mysql-community-embedded-compat-8.0.18-1.el7.x86_64.rpm
mysql-community-libs-compat-8.0.18-1.el7.x86_64.rpm
mysql-community-common-8.0.18-1.el7.x86_64.rpm
mysql-community-test-8.0.18-1.el7.x86_64.rpm
mysql-community-server-8.0.18-1.el7.x86_64.rpm
mysql-community-client-8.0.18-1.el7.x86_64.rpm
[root@mycentos7 mypackage]# ls
mysql-8.0.18-1.el7.x86_64.rpm-bundle.tar
mysql-community-client-8.0.18-1.el7.x86_64.rpm
mysql-community-common-8.0.18-1.el7.x86_64.rpm
mysql-community-devel-8.0.18-1.el7.x86_64.rpm
mysql-community-embedded-compat-8.0.18-1.el7.x86_64.rpm
mysql-community-libs-8.0.18-1.el7.x86_64.rpm
mysql-community-libs-compat-8.0.18-1.el7.x86_64.rpm
mysql-community-server-8.0.18-1.el7.x86_64.rpm
mysql-community-test-8.0.18-1.el7.x86_64.rpm
[root@mycentos7 mypackage]# rpm -ivh mysql-community-common-8.0.18-1.el7.x86_64.rpm
警告:mysql-community-common-8.0.18-1.el7.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY
准备中... ################################# [100%]
正在升级/安装...
1:mysql-community-common-8.0.18-1.e################################# [100%]
[root@mycentos7 mypackage]# rpm -ivh mysql-community-libs-8.0.18-1.el7.x86_64.rpm
警告:mysql-community-libs-8.0.18-1.el7.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY
准备中... ################################# [100%]
正在升级/安装...
1:mysql-community-libs-8.0.18-1.el7################################# [100%]
[root@mycentos7 mypackage]# rpm -ivh mysql-community-client-8.0.18-1.el7.x86_64.rpm
警告:mysql-community-client-8.0.18-1.el7.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY
准备中... ################################# [100%]
正在升级/安装...
1:mysql-community-client-8.0.18-1.e################################# [100%]
[root@mycentos7 mypackage]# rpm -ivh mysql-community-server-8.0.18-1.el7.x86_64.rpm --force --nodeps
警告:mysql-community-server-8.0.18-1.el7.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY
准备中... ################################# [100%]
正在升级/安装...
1:mysql-community-server-8.0.18-1.e################################# [100%]
5、配置MySQL服务
[root@mycentos7 mypackage]# mysqld --initialize --console
[root@mycentos7 mypackage]# chown -R mysql:mysql /var/lib/mysql
[root@mycentos7 mypackage]# rm -rf /var/lib/mysql // 解决问题
[root@mycentos7 mypackage]# service mysqld restart // 解决问题
Redirecting to /bin/systemctl restart mysqld.service
[root@mycentos7 mypackage]# systemctl start mysqld
6、修改MySQL密码
[root@mycentos7 mypackage]# cat /var/log/mysqld.log|grep localhost
2022-09-03T04:57:57.833601Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: *fnltkp,/8rZ
[root@mycentos7 mypackage]# 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.18
Copyright (c) 2000, 2019, 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> alter user 'root'@'localhost' identified by '111111';
Query OK, 0 rows affected (0.01 sec)
mysql> exit
Bye
7、登录MySQL
[root@mycentos7 mypackage]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.18 MySQL Community Server - GPL
Copyright (c) 2000, 2019, 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;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
mysql>
8、MySQL连接报错
连接MySQL报错:is not allowed to connect to this MySQL server
use mysql;
select host from user where user='root';
update user set host = '%' where user ='root';
flush privileges;
select host from user where user='root';
提示:需要四个文件
二、yum安装(下载最新版本)
// 检查 mariadb
rpm -qa|grep mariadb
// 卸载 mariadb
rpm -e --nodeps mariadb-libs
// 检查 mariadb
rpm -qa|grep mariadb
// 检查 mysql
rpm -qa|grep mysql
// 配置扩展源
rpm -ivh https://dev.mysql.com/get/mysql80-community-release-el7-9.noarch.rpm
// 安装
yum install mysql-community-server -y --nogpgcheck
//启动Mysql
systemctl start mysqld
//并加入开机自启
systemctl enable mysqld
// 查看临时密码
cat /var/log/mysqld.log | grep localhost
// 登录,回车输入密码
mysql -uroot -p
//修改密码
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '数字大小写字母特殊符号的密码';
// 刷新权限
FLUSH PRIVILEGES;
//退出
exit;
// 重新登录
mysql -uroot -p密码
三、wget下载安装包(自定义版本)
// 创建目录
mkdir mypackage
// 进入目录
cd mypackage
// 检查 mariadb
rpm -qa|grep mariadb
// 卸载 mariadb
rpm -e --nodeps mariadb-libs
// 检查 mariadb
rpm -qa|grep mariadb
// 检查 mysql
rpm -qa|grep mysql
// 下载wget
yum install -y wget
// wget下载安装包
wget https://dev.mysql.com/get/mysql-8.0.18-1.el7.x86_64.rpm-bundle.tar
// 解压安装包
tar -xvf mysql-8.0.18-1.el7.x86_64.rpm-bundle.tar
// 按照顺序安装
rpm -ivh mysql-community-common-8.0.18-1.el7.x86_64.rpm
rpm -ivh mysql-community-libs-8.0.18-1.el7.x86_64.rpm
rpm -ivh mysql-community-client-8.0.18-1.el7.x86_64.rpm
rpm -ivh mysql-community-server-8.0.18-1.el7.x86_64.rpm
// 配置mysql服务
mysqld --initialize --console
chown -R mysql:mysql /var/lib/mysql
rm -rf /var/lib/mysql
service mysqld restart
systemctl start mysqld
// 查看密码
cat /var/log/mysqld.log|grep localhost
// 登录
mysql -uroot -p
// 设置密码
alter user 'root'@'localhost' identified by '数字大小写字母特殊符号的密码';
// 切换到mysql数据库
use mysql;
// 查询user表
select user,host from user;
// 设置host
update user set host='%' where host='localhost' and user='root';
// 再次查询user表,发现root的host从localhost变成了%
select user,host from user;
// 刷新权限
FLUSH PRIVILEGES;
// 退出数据库
exit;
// 登录
mysql -uroot -p
// 查询数据库
show databases;