centos7服务器安装mysql8
前言
CentOS 7 版本将MySQL数据库软件从默认的程序列表中移除,用mariadb代替了。
本文记录了在线安装mysql及离线安装mysql的方法和常见问题。
如果需要继续使用MySQL,可参考本文操作。
在线安装
获取yum源
- 在MySQL官网中下载YUM源rpm安装包,复制下载链接使用wget下载。
wget https://dev.mysql.com/get/mysql80-community-release-el7-1.noarch.rpm
- 安装yum源
yum localinstall mysql80-community-release-el7-1.noarch.rpm
- 检查yum源
yum repolist enabled | grep "mysql.*-community.*"
可能遇到的问题:
-
安装了较旧的yum源,先删除旧版本
-
查询yum源
rpm -qa |grep -i repo-name
-
删除该yum源
rpm -e repo-name
(参考博客地址)
-
安装mysql
-
可以修改/etc/yum.repos.d/mysql-community.repo源,改变默认安装的mysql版本,enabled=0禁用,enabled=1启用。
-
安装默认版本
※ 此处会耗费很长时间,可以先忙别的。
yum install -y mysql-community-server
执行完毕即安装完成。受服务器网速影响(预估耗时30h+),在线安装到此截止。改用离线安装。↓
离线安装
-
官网下载RPM Bundle包
-
上传Bundle包至服务器,rz -be上传、或xftp上传。
-
解压Bundle包至
/usr/local/mysql
目录下。 -
依次安装common、libs、client、server
[root@VM-0-4-centos mysql]# tar -xvf mysql-8.0.21-1.el7.x86_64.rpm-bundle.tar
mysql-community-common-8.0.21-1.el7.x86_64.rpm
mysql-community-embedded-compat-8.0.21-1.el7.x86_64.rpm
mysql-community-libs-8.0.21-1.el7.x86_64.rpm
mysql-community-devel-8.0.21-1.el7.x86_64.rpm
mysql-community-server-8.0.21-1.el7.x86_64.rpm
mysql-community-client-8.0.21-1.el7.x86_64.rpm
mysql-community-libs-compat-8.0.21-1.el7.x86_64.rpm
mysql-community-test-8.0.21-1.el7.x86_64.rpm
[root@VM-0-4-centos mysql]# rpm -qa | grep mariadb
[root@VM-0-4-centos mysql]# rpm -ivh mysql-community-common-8.0.21-1.el7.x86_64.rpm
Preparing... ################################# [100%]
Updating / installing...
1:mysql-community-common-8.0.21-1.e################################# [100%]
[root@VM-0-4-centos mysql]# rpm -ivh mysql-community-libs-8.0.21-1.el7.x86_64.rpm
Preparing... ################################# [100%]
Updating / installing...
1:mysql-community-libs-8.0.21-1.el7################################# [100%]
[root@VM-0-4-centos mysql]# rpm -ivh mysql-community-client-8.0.21-1.el7.x86_64.rpm
Preparing... ################################# [100%]
Updating / installing...
1:mysql-community-client-8.0.21-1.e################################# [100%]
[root@VM-0-4-centos mysql]# rpm -ivh mysql-community-server-8.0.21-1.el7.x86_64.rpm
Preparing... ################################# [100%]
Updating / installing...
1:mysql-community-server-8.0.21-1.e################################# [100%]
至此,mysql已安装完毕。
服务启停
-
查看服务状态
systemctl status mysqld.service
-
启动服务
service mysqld start
-
停止服务
service mysqld stop
# 启动服务
[root@VM-0-4-centos etc]# service mysqld start
Redirecting to /bin/systemctl start mysqld.service
# 查看服务
[root@VM-0-4-centos etc]# systemctl status mysqld.service
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since Tue 2020-09-01 00:08:51 CST; 37s ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 21087 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
Main PID: 21110 (mysqld)
Status: "Server is operational"
CGroup: /system.slice/mysqld.service
└─21110 /usr/sbin/mysqld
# 关闭服务
[root@VM-0-4-centos etc]# service mysqld stop
Redirecting to /bin/systemctl stop mysqld.service
[root@VM-0-4-centos etc]# systemctl status mysqld.service
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: inactive (dead) since Tue 2020-09-01 00:09:49 CST; 11s ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 21110 ExecStart=/usr/sbin/mysqld $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
Process: 21087 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
Main PID: 21110 (code=exited, status=0/SUCCESS)
Status: "Server shutdown complete"
-
遇到问题
[ERROR] [MY-012271] [InnoDB] The innodb_system data file 'ibdata1' must be writable
查看日志发现是ibdata1无写权限,修改
/var/lib/mysql
权限为777chmod -R 777 mysql
数据库设置
-
数据库初始化
使用指令1对数据库进行初始化
mysqld --initialize;
,并得到临时密码。
[root@VM-0-4-centos etc]# mysqld --initialize;
2020-08-31T16:13:01.986611Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.0.21) initializing of server in progress as process 21288
2020-08-31T16:13:01.995195Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2020-08-31T16:13:04.085829Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2020-08-31T16:13:07.225825Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: fu-k1yjtnllY 《==============临时密码
或查看日志获取临时密码(我安装了两次所以两种方式显示密码不一致,请忽略)
[root@VM-0-4-centos mysql]# cat /var/log/mysqld.log |grep pass
2020-08-31T17:45:15.371100Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: gX:Uey-fg2JV 《==============临时密码
-
登录mysql
使用临时密码登录mysql
[root@VM-0-4-centos mysql]# 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.21
Copyright (c) 2000, 2020, 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>
-
修改root密码
使用
alter USER 'root'@'localhost' IDENTIFIED BY 'root';
修改root密码。
mysql> alter USER 'root'@'localhost' IDENTIFIED BY 'root';
Query OK, 0 rows affected (0.05 sec)
mysql> quit
Bye
[root@VM-0-4-centos mysql]# mysql -uroot -p
Enter password: <===========================输入新密码root
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 8.0.21 MySQL Community Server - GPL
Copyright (c) 2000, 2020, 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>
- 授权远程连接
# 切换数据库
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
# 修改root host为%
mysql> update user set host = "%" where user='root';
Query OK, 1 row affected (0.02 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
# 修改远程登录密码为root
mysql> alter USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'root';
Query OK, 0 rows affected (0.01 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
- 使用Navicat测试连接,可在
/etc/my.cnf
中配置端口号,默认为3306。
END
mysql的安装出现了挺多error,不过还好大部分都能在网上找到答案,总结起来大概包含以下内容
-
rpm安装需要先移除旧版本。
rpm -qa|grep XXX
rpm -e XXX
-
移除旧版本时报错缺少依赖。
rpm -ev XXX --force --nodeps
-
mysql安装完成后初始化失败。
清空
/var/lib/mysql
目录。有些版本是data目录。 -
mysql服务启动失败。
查看
/var/log/mysqld.log
错误日志定位问题,缺路径就建文件夹,少权限就授权。 -
登录mysql失败,需要密码。
初始化后查看日志获取临时密码
cat /var/log/mysqld.log |grep pass
另外说句题外话:
服务器上安装任何软件都尽量修改其访问端口,使用默认端口很危险,在学习redis、mysql、filebeat等软件时,都曾因使用默认端口被残忍挖矿,血泪教训。o(╥﹏╥)o