centos7服务器重新安装mysql_centos7服务器安装mysql8

本文介绍了如何在CentOS 7上安装MySQL8.0,包括在线和离线两种方法。在线安装涉及下载YUM源、安装及可能遇到的问题。离线安装则涉及下载RPM Bundle包,逐个安装组件。最后,文章还涵盖了启动、停止MySQL服务,以及设置和授权远程连接的步骤。
摘要由CSDN通过智能技术生成

前言

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权限为777

chmod -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

LINUX重启MYSQL的命令 . 分类: Linux 2010-06-25 10:21 6367人阅读 评论(0) 收藏 举报 如何启动/停止/重启MySQL 一、启动方式 1、使用 service 启动:service mysqld start 2、使用 mysqld 脚本启动:/etc/inint.d/mysqld start 3、使用 safe_mysqld 启动:safe_mysqld& 二、停止 1、使用 service 启动:service mysqld stop 2、使用 mysqld 脚本启动:/etc/inint.d/mysqld stop 3、 mysqladmin shutdown 三、重启 1、使用 service 启动:service mysqld restart 2、使用 mysqld 脚本启动:/etc/inint.d/mysqld restart 提问 编辑摘要 如何启动/停止/重启MySQL 一、启动方式 1、使用 service 启动:service mysqld start 2、使用 mysqld 脚本启动:/etc/inint.d/mysqld start 3、使用 safe_mysqld 启动:safe_mysqld& 二、停止 1、使用 service 启动:service mysqld stop 2、使用 mysqld 脚本启动:/etc/inint.d/mysqld stop 3、 mysqladmin shutdown 三、重启 1、使用 service 启动:service mysqld restart 2、使用 mysqld 脚本启动:/etc/inint.d/mysqld restart 刚开始学 mysql时都是用redhat自带的。启动是什么 /rc.d/init.d/ start 这很简单,但是后来越学越多,系统自带的 mysql,有的是版本太低,有的是与 自己想要装的web服务需要的低版本的mysql 后来自己学着以tar的方式安装 mysql,我的mysql装在/usr/local/mysql目录下启 动碰到过很多问题。最常见的是: ERROR 2002: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (111) 解决办法: [root@test mysql]# /usr/local/mysql/bin/mysqladmin -u root / > -S /var/lib/mysql/mysql.sock password 'your.passwd' 或者做个连接 ln -s /var/lib/mysql/mysql.sock /tmp 其实提示找不到 /tmp/mysql.sock有时也并不是/tmp目录下没这个文件,是启动 命令不对,我碰到过 常见的几种启动方式,自己也不是记得很清楚,如果你确定tmp下有mysql.sock这 个文件不妨试试另外的几个命令 /usr/local/mysql/bin/mysql -u root -p /usr/local/mysql/bin/mysqld --user=mysql& /usr/local/mysql/bin/mysqld --user=root& /usr/local/mysql/bin/mysqld_safe --user=root& /usr/local/mysql/bin/mysqld_safe --user=mysql& /usr/local/mysql/bin/safe_mysqld--uer=root&(注意 safe_mysqld与mysqld_safe是不同的,&表示mysql在后台运行)我的就会报错了 STOPPING server from pid file /usr/local/mysql/data/localhost.localdomain.pid 060304 11:46:21 mysqld ended 这是权限问题,我的mysql目录属于root用户,也属于root群组,改用 mysqld_safe启动就没问题了, 大家只要注意这几个 mysql,safe_mysqld,mysqld_safe,mysqld,mysqladmin.多试 几次 其实有时mysql已经正常启动了,查看mysql是否启动命令 ps -aux | grep mysqld 会看到如下类似内容 mysql 6394 0.0 1.5 10528 992 pts/3 S 16:16 0:00 /usr/local/mysql/ mysql 6395 0.0 1.5 10528 992 pts/3 S 16:16 0:00 /usr/local/mysql/ mysql 6396 0.0 1.5 10528 992 pts/3 S 16:16 0:00 /usr/local/mysql/ root 6422 0.0 1.1 2408 732 pts/3 S 16:20 0:00 grep mysql 查看mysql是否在监听端口命令 netstat -tl | grep mysql 会看到如下类似内容 tcp 0 0 *:mysql *:* LISTEN
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值