写点什么

Linux 安装 mysql

作者:犟马骝
  • 2021 年 11 月 28 日
  • 本文字数:2111 字

    阅读完需:约 7 分钟

1       安装

1.1    下载 mysql 离线安装包并上传至服务器


mysql-5.7.35-1.el7.x86_64.rpm-bundle.tar

下载地址:https://downloads.mysql.com/archives/community/

1.2    删除原有的 mariadb

先查看一下是否已经安装了,命令:rpm -qa|grep mariadb


[root@node03 ~]# rpm -qa|grep mariadbmariadb-libs-5.5.60-1.el7_5.x86_64
复制代码


删除 mariadb,命令:rpm -e --nodeps mariadb-libs

1.3    解压缩 mysql 离线安装包

cd /opt

tar -xvf mysql-5.7.35-1.el7.x86_64.rpm-bundle.tar

[root@node03 mysql]# tar -xvf mysql-5.7.35-1.el7.x86_64.rpm-bundle.tar mysql-community-client-5.7.35-1.el7.x86_64.rpmmysql-community-common-5.7.35-1.el7.x86_64.rpmmysql-community-devel-5.7.35-1.el7.x86_64.rpmmysql-community-embedded-5.7.35-1.el7.x86_64.rpmmysql-community-embedded-compat-5.7.35-1.el7.x86_64.rpmmysql-community-embedded-devel-5.7.35-1.el7.x86_64.rpmmysql-community-libs-5.7.35-1.el7.x86_64.rpmmysql-community-libs-compat-5.7.35-1.el7.x86_64.rpmmysql-community-server-5.7.35-1.el7.x86_64.rpmmysql-community-test-5.7.35-1.el7.x86_64.rpm
复制代码


1.4    安装 rpm 包

逐个安装,命令如下:

 

【必须安装】

rpm -ivh mysql-community-common-5.7.35-1.el7.x86_64.rpm

rpm -ivh mysql-community-libs-5.7.35-1.el7.x86_64.rpm

rpm -ivh mysql-community-client-5.7.35-1.el7.x86_64.rpm

rpm -ivh mysql-community-server-5.7.35-1.el7.x86_64.rpm

【非必须安装】

rpm -ivh mysql-community-devel-5.7.35-1.el7.x86_64.rpm

rpm -ivh mysql-community-embedded-compat-5.7.35-1.el7.x86_64.rpm

rpm -ivh mysql-community-libs-compat-5.7.35-1.el7.x86_64.rpm

rpm -ivh mysql-community-test-5.7.35-1.el7.x86_64.rpm

2       服务启停

2.1    查看服务状态

命令:systemctl status mysqld

[root@node03 mysql]# systemctl status mysqld● mysqld.service - MySQL Server   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)   Active: inactive (dead)     Docs: man:mysqld(8)           http://dev.mysql.com/doc/refman/en/using-systemd.html
复制代码

2.2    停止服务

2.3    初始化数据库

mysqld --initialize --console

2.4    目录授权

chown -R mysql:mysql /var/lib/mysql/

2.5    启动 mysql 服务

systemctl start mysqld

systemctl status mysqld

[root@node03 mysql]# systemctl status mysqld● mysqld.service - MySQL Server   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)   Active: active (running) since Sun 2021-11-28 04:52:39 EST; 5s ago     Docs: man:mysqld(8)           http://dev.mysql.com/doc/refman/en/using-systemd.html  Process: 8019 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)  Process: 8002 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS) Main PID: 8022 (mysqld)   CGroup: /system.slice/mysqld.service           └─8022 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
Nov 28 04:52:38 node03 systemd[1]: Starting MySQL Server...Nov 28 04:52:39 node03 systemd[1]: Started MySQL Server.
复制代码

3       数据库操作

3.1    查看临时密码

cat /var/log/mysqld.log|grep password

[root@node03 mysql]# cat /var/log/mysqld.log|grep password2021-11-28T09:52:23.709534Z 1 [Note] A temporary password is generated for root@localhost: %NuH-foVC7u=
复制代码

3.2    用临时密码登录数据库

mysql -u root -p 回车键

然后输入临时密码(输入时不会显示出来,输入完直接回车)

[root@node03 mysql]# mysql -u root -pEnter password: Welcome to the MySQL monitor.  Commands end with ; or \g.Your MySQL connection id is 2Server version: 5.7.35
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
复制代码

3.3    修改 mysql 密码

alter USER 'root'@'localhost' IDENTIFIED BY '123456';

3.4    授权远程连接

use mysql;

update user set host = "%" where user='root';

flush privileges;

 

尝试使用 SQLyog 远程连接,会出现如下错误:错误号码 1251 Client does not support authentication protocol requested by server;consider upgrading MySQL client

解决办法:把 mysql 用户登录密码加密规则还原成 mysql_native_password。

use mysql;

alter USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456';

flush privileges;

再次使用 SQLyog 连接 mysql,测试成功。


用户头像

犟马骝

关注

把学习的过程记录下来,踏实走好每一步! 2020.10.11 加入

专注于大数据技术、机器学习方面的内容研究,着重理论与实践相结合的学习方式,对新技术有很强的好奇心。坚持学习,加油!

评论

发布
暂无评论
Linux安装mysql