您的位置:首页 > 运维架构 > Linux

Linux实战---CentOS7下yum方式安装MySQL5.7

2020-01-13 17:55 597 查看

CentOS7安装MySQL5.7

环境说明
Centos版本: CentOS Linux release 7.7.1908 (Core)
Linux连接工具:SecureCRT
MySQL 下载地址:https://dev.mysql.com/downloads/mysql/5.5.html
Select Version: 5.7.28
Select Operating System: Red Hat Enterprise Linux / Oracle Linux
Select OS Version: ALL
Download Packages:mysql-5.7.28-1.el7.x86_64.rpm-bundle.tar
MySQL安装完成默认文件路径
配置文件:/etc/my.cnf 
日志文件:/var/log/var/log/mysqld.log 
服务启动脚本:/usr/lib/systemd/system/mysqld.service 
socket文件:/var/run/mysqld/mysqld.pid
数据库文件:/var/lib/mysql

第一步:查看是否安装了MySQL

# 查看是否安装了MySQL
[root@localhost mysql-5.7.28]# rpm -qa | grep -i mysql
#查看是否安装了mariadb(centos7默认系统自带的)
[root@localhost mysql-5.7.28]# rpm -qa | grep -i mariadb
mariadb-libs-5.5.64-1.el7.x86_64
# 已经安装了mariadb是mysql的一个分支,需要把它卸载掉
[root@localhost mysql-5.7.28]# rpm -e mariadb-libs-5.5.64-1.el7.x86_64
error: Failed dependencies:
libmysqlclient.so.18()(64bit) is needed by (installed) postfix-2:2.10.1-7.el7.x86_64
libmysqlclient.so.18(libmysqlclient_18)(64bit) is needed by (installed) postfix-2:2.10.1-7.el7.x86_64
# 用rpm删除会失败,提示有依赖关系的软件需要安装 所以采用yum remove +包的形式删除
[root@localhost mysql-5.7.28]# yum remove mariadb-libs-5.5.64-1.el7.x86_64
Loaded plugins: fastestmirror
Installed size: 17 M
#这个地方直接输入y就可以
Is this ok [y/N]: y
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Erasing    : 2:postfix-2.10.1-7.el7.x86_64 1/2
Erasing    : 1:mariadb-libs-5.5.64-1.el7.x86_64  2/2
Verifying  : 1:mariadb-libs-5.5.64-1.el7.x86_64  1/2
Verifying  : 2:postfix-2.10.1-7.el7.x86_64    2/2
Removed:
mariadb-libs.x86_64 1:5.5.64-1.el7
Dependency Removed:
postfix.x86_64 2:2.10.1-7.el7
#提示删除成功
Complete!

第二步:开始安装MySQL

# 把.tar文件放到/opt文件中
[root@localhost /]# cd /opt/
# 通过rz命令把.tar上传到opt目录
[root@localhost opt]# rz
# 解压到mysql-5.7.28 目录
[root@localhost opt]# tar -xvf mysql-5.7.28-1.el7.x86_64.rpm-bundle.tar -C mysql-5.7.28
mysql-community-embedded-5.7.28-1.el7.x86_64.rpm
mysql-community-libs-compat-5.7.28-1.el7.x86_64.rpm
mysql-community-devel-5.7.28-1.el7.x86_64.rpm
mysql-community-embedded-compat-5.7.28-1.el7.x86_64.rpm
mysql-community-libs-5.7.28-1.el7.x86_64.rpm
mysql-community-test-5.7.28-1.el7.x86_64.rpm
mysql-community-common-5.7.28-1.el7.x86_64.rpm
mysql-community-embedded-devel-5.7.28-1.el7.x86_64.rpm
mysql-community-client-5.7.28-1.el7.x86_64.rpm
mysql-community-server-5.7.28-1.el7.x86_64.rpm
# 安装mysql
[root@localhost /]# cd /opt/mysql-5.7.28
[root@localhost mysql-5.7.28]# yum install mysql-community-{server,client,common,libs}-*
Dependency Installed:
perl.x86_64 4:5.16.3-294.el7_6          perl-Carp.noarch 0:1.26-244.el7         perl-Encode.x86_64 0:2.51-7.el7                perl-Exporter.noarch 0:5.68-3.el7     perl-File-Path.noarch 0:2.09-2.el7         perl-File-Temp.noarch 0:0.23.01-3.el7
perl-Filter.x86_64 0:1.49-3.el7         perl-Getopt-Long.noarch 0:2.40-3.el7    perl-HTTP-Tiny.noarch 0:0.033-3.el7            perl-PathTools.x86_64 0:3.40-5.el7    perl-Pod-Escapes.noarch 1:1.04-294.el7_6   perl-Pod-Perldoc.noarch 0:3.20-4.el7
perl-Pod-Simple.noarch 1:3.28-4.el7     perl-Pod-Usage.noarch 0:1.63-3.el7      perl-Scalar-List-Utils.x86_64 0:1.27-248.el7   perl-Socket.x86_64 0:2.010-4.el7      perl-Storable.x86_64 0:2.45-3.el7          perl-Text-ParseWords.noarch 0:3.29-4.el7
perl-Time-HiRes.x86_64 4:1.9725-3.el7   perl-Time-Local.noarch 0:1.2300-2.el7   perl-constant.noarch 0:1.27-2.el7              perl-libs.x86_64 4:5.16.3-294.el7_6   perl-macros.x86_64 4:5.16.3-294.el7_6      perl-parent.noarch 1:0.225-244.el7
perl-podlators.noarch 0:2.5.1-3.el7     perl-threads.x86_64 0:1.87-4.el7        perl-threads-shared.x86_64 0:1.43-6.el7
Complete!
#查看MySQL进程
[root@localhost mysql-5.7.28]# ps -ef|grep -i mysql
mysql     3132     1  1 19:19 ?        00:00:00 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
root      3175  2506  0 19:20 pts/0    00:00:00 grep --color=auto -i mysql
#查看MySQL是否安装成功
[root@localhost mysql-5.7.28]# rpm -qa | grep -i mysql
mysql-community-client-5.7.28-1.el7.x86_64
mysql-community-server-5.7.28-1.el7.x86_64
mysql-community-libs-5.7.28-1.el7.x86_64
mysql-community-libs-compat-5.7.28-1.el7.x86_64
mysql-community-common-5.7.28-1.el7.x86_64

第三步:查看MySQL安装目录

#查看mysql安装目录
[root@localhost ~]# whereis mysql
mysql:
/usr/bin/mysql
/usr/lib64/mysql
/usr/share/mysql
/usr/share/man/man1/mysql.1.gz

第四步:初始化MySQL密码

  • 通过临时密码修改
#重启MySQL服务
[root@localhost mysql]# service mysqld restart
Redirecting to /bin/systemctl restart mysqld.service
#重置密码,此时密码为随机密码
[root@localhost mysql]# grep "password" /var/log/mysqld.log
2019-12-25T11:19:37.299148Z 1 [Note] A temporary password is generated for root@localhost: gkA6xL_2--<l
#密码是gkA6xL_2--<l 这里有一个快捷输入密码的方式,可以选中后,点击右键(主要是为了分不清大消息)
[root@localhost mysql]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.7.28
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>
#登录成功,首次登录,必须修改密码才能使用
mysql> show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
#重置密码,5.7.2之后,为了安全密码必须包含:大写字母、字母、数字、符号
mysql> alter user 'root'@'localhost' identified by 'root';
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
mysql> alter user 'root'@'localhost' identified by 'root2020';
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
mysql>  alter user 'root'@'localhost' identified by 'root2020.';
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
mysql>  alter user 'root'@'localhost' identified by 'Root2020.';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
  • 通过修改配置文件重置密码
#在配置文件末尾添加:skip-grant-tables 用来跳过密码验证过程
[root@localhost mysql-5.7.28]# vi /etc/my.cnf
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html

[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
#添加这一行,跳过密码验证
skip-grant-tables

#保存退出后重启MySQL
[root@localhost mysql]# service mysqld restart
[root@localhost mysql-5.7.28]# mysql -u root -p
#此处不需要输入直接回车就可以
Enter password:
#一定要先输入
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
#修改密码
mysql> alter user 'root'@'localhost' identified by 'Root2020.';
#退出MySQL
mysql> exit;
#删除配置文件里面配置
[root@localhost mysql-5.7.28]# vi /etc/my.cnf
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html

[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
#删除这一行:skip-grant-tables
#保存退出后重启MySQL
[root@localhost mysql]# service mysqld restart

第五步:MySQL默认文件路径

配置文件:/etc/my.cnf 
日志文件:/var/log/var/log/mysqld.log 
服务启动脚本:/usr/lib/systemd/system/mysqld.service 
socket文件:/var/run/mysqld/mysqld.pid
数据库文件:/var/lib/mysql

  • 点赞
  • 收藏
  • 分享
  • 文章举报
程序猿壹号 发布了8 篇原创文章 · 获赞 0 · 访问量 152 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: