您的位置 首页 >  博文

CentOS7离线安装MySql5.7(亲测好用)

一、清除Mariadb

首先清除CentOS7系统中默认的数据库mariadb,否则不能安装mysql。

1rpm -qa |grep mariadb |xargs yum remove -y

二、安装MySql

1. 下载MySql的相关rpm包

在https://dev.mysql.com/downloads/mysql/选择Archives选项卡,产品版本选择5.7.36,操作系统选择Red Hat Enterprise Linux 7/Oracle Linux 7,把OS的版本选择为all,直接下载mysql-5.7.36-1.el7.x86_64.rpm-bundle.tar,所有的rpm包都在里面。可以通过下面的命令来下载rpm包:

1wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.36-1.el7.x86_64.rpm-bundle.tar

2. 解压mysql-5.7.36-1.el7.x86_64.rpm-bundle.tar

将下载的mysql-5.7.36-1.el7.x86_64.rpm-bundle.tar放到/usr/local/mysql目录,解压缩安装包,命令如下:

1tar axvf mysql-5.7.36-1.el7.x86_64.rpm-bundle.tar

3. 安装rpm包

切换到下载包目录下(cd 你的下载目录),然后对每个包进行一次安装;

1rpm -ivh mysql-community-common-5.7.36-1.el7.x86_64.rpm
2rpm -ivh mysql-community-libs-5.7.36-1.el7.x86_64.rpm
3rpm -ivh mysql-community-libs-compat-5.7.36-1.el7.x86_64.rpm
4rpm -ivh mysql-community-embedded-5.7.36-1.el7.x86_64.rpm
5rpm -ivh mysql-community-embedded-compat-5.7.36-1.el7.x86_64.rpm
6rpm -ivh mysql-community-embedded-devel-5.7.36-1.el7.x86_64.rpm
7rpm -ivh mysql-community-devel-5.7.36-1.el7.x86_64.rpm
8rpm -ivh mysql-community-client-5.7.36-1.el7.x86_64.rpm
9rpm -ivh mysql-community-server-5.7.36-1.el7.x86_64.rpm

4. 修改MySql配置

1vim /etc/my.cnf

修改如下配置:

1# datadir=/var/lib/mysql
2datadir=/data/mysql
3socket=/var/lib/mysql/mysql.sock
4log-error=/var/log/mysqld.log
5pid-file=/var/run/mysqld/mysqld.pid

完整的配置文件如下:

 1# For advice on how to change settings please see
2# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
3
4[mysqld]

5#
6# Remove leading # and set to the amount of RAM for the most important data
7# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
8# innodb_buffer_pool_size = 128M
9#
10# Remove leading # to turn on a very important data integrity option: logging
11# changes to the binary log between backups.
12# log_bin
13#
14# Remove leading # to set options mainly useful for reporting servers.
15# The server defaults are faster for transactions and fast SELECTs.
16# Adjust sizes as needed, experiment to find the optimal values.
17# join_buffer_size = 128M
18# sort_buffer_size = 2M
19# read_rnd_buffer_size = 2M
20datadir=/data/mysql
21socket=/var/lib/mysql/mysql.sock
22
23# Disabling symbolic-links is recommended to prevent assorted security risks
24symbolic-links=0
25
26log-error=/var/log/mysqld.log
27pid-file=/var/run/mysqld/mysqld.pid

5. 初始化Mysql数据库

1mysqld --initialize
2chown mysql:mysql /data/mysql -R
3systemctl start mysqld.service
4systemctl enable mysqld

三、启动MySql服务

1. MySql相关命令

1# 启动
2systemctl start mysqld.service
3# 停止
4systemctl stop mysqld.service
5# 重启
6systemctl restart mysqld.service

2. 设置MySql开机自启

1systemctl enable mysqld

3. 查看Mysql随机密码

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

如下输出的K7abt/-C7.S/则为密码

12022-02-08T09:26:55.7335361 [Note] A temporary password is generated for root@localhost: K7abt/-C7.S/

4. 注意

启用服务时,有个坑,需要注意一下,Mysql启动可能会因为systemctl启动不成功,报如下的错:

1[root@localhost ~]# systemctl start mysqld
2Job for mysqld.service failed because the control process exited with error code. See "systemctl status mysqld.service" and "journalctl -xe" for details.

如出现上面情况,则需要看一下Mysql的日志,在/var/log/mysqld.log

1cat /var/log/mysqld.log

大概能出现如下错误:

 12022-02-08T09:29:41.9447510 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
22022-02-08T09:29:41.9450340 [Warning] Can't create test file /data/mysql/localhost.lower-test
32022-02-08T09:29:41.9450930 [Note] /usr/sbin/mysqld (mysqld 5.7.36) starting as process 6619 ...
42022-02-08T09:29:41.9475800 [Warning] Can't create test file /data/mysql/localhost.lower-test
52022-02-08T09:29:41.9476050 [Warning] Can't create test file /data/mysql/localhost.lower-test
62022-02-08T09:29:41.9519080 [Note] InnoDB: PUNCH HOLE support available
72022-02-08T09:29:41.9519400 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
82022-02-08T09:29:41.9519440 [Note] InnoDB: Uses event mutexes
92022-02-08T09:29:41.9519480 [Note] InnoDB: GCC builtin __atomic_thread_fence() is used for memory barrier
102022-02-08T09:29:41.9519510 [Note] InnoDB: Compressed tables use zlib 1.2.11
112022-02-08T09:29:41.9519540 [Note] InnoDB: Using Linux native AIO
122022-02-08T09:29:41.9522740 [Note] InnoDB: Number of pools: 1
132022-02-08T09:29:41.9523820 [Note] InnoDB: Using CPU crc32 instructions
142022-02-08T09:29:41.9538410 [Note] InnoDB: Initializing buffer pool, total size = 128M, instances = 1, chunk size = 128M
152022-02-08T09:29:41.9606310 [Note] InnoDB: Completed initialization of buffer pool
162022-02-08T09:29:41.9625240 [Note] InnoDB: If the mysqld execution user is authorized, page cleaner thread priority can be changed. See the man page of setpriority().
172022-02-08T09:29:41.9726920 [ERROR] InnoDB: Operating system error number 13 in a file operation.
182022-02-08T09:29:41.9727330 [ERROR] InnoDB: The error means mysqld does not have the access rights to the directory.
192022-02-08T09:29:41.9727380 [ERROR] InnoDB: os_file_get_status() failed on './ibdata1'. Can't determine file permissions
202022-02-08T09:29:41.9727470 [ERROR] InnoDB: Plugin initialization aborted with error Generic error
212022-02-08T09:29:42.5991750 [ERROR] Plugin 'InnoDB' init function returned error.
222022-02-08T09:29:42.5992370 [ERROR] Plugin 'InnoDB' registration as a STORAGE ENGINE failed.
232022-02-08T09:29:42.5992490 [ERROR] Failed to initialize builtin plugins.
242022-02-08T09:29:42.5992540 [ERROR] Aborting
25# 解释
26[错误] InnoDB:文件操作中的操作系统错误号13
27[错误] InnoDB:该错误意味着mysqld没有访问该目录的权限。
28[错误] InnoDB: os_file_get_status()在上失败。/ibdata1。无法确定文件权限
29[错误] InnoDB:插件初始化中止,出现错误一般错误
30[错误]插件“InnoDB”初始化函数返回错误。
31[错误]插件“InnoDB”注册为存储引擎失败。
32[错误]初始化内置插件失败。
33[错误]中止

出现上述错误需要关闭selinux,以下是操作方法。

5. 关闭selinux

  • 临时关闭selinux

1[root@localhost ~]# getenforce    //查看selinux状态
2Enforcing
3[root@localhost ~]# setenforce 0    //临时关闭selinux,重启后失效
4[root@localhost ~]# getenforce
5Permissive
6[root@localhost ~]# systemctl start mysqld     //启动mysql服务成功
  • 永久关闭selinux

 1# 备份配置文件
2cp -r /etc/selinux/config /etc/selinux/config.bak
3# 修改配置为disabled
4sed -i 's/SELINUX=enforcing/\SELINUX=disabled/' /etc/selinux/config
5# 查看配置
6cat /etc/selinux/config
7
8
9# This file controls the state of SELinux on the system.
10# SELINUX= can take one of these three values:
11#     enforcing - SELinux security policy is enforced.
12#     permissive - SELinux prints warnings instead of enforcing.
13#     disabled - No SELinux policy is loaded.
14SELINUX=disabled
15# SELINUXTYPE= can take one of three values:
16#     targeted - Targeted processes are protected,
17#     minimum - Modification of targeted policy. Only selected processes are protected. 
18#     mls - Multi Level Security protection.
19SELINUXTYPE=targeted 

改完后即可成功启动Mysql,如果不行则重启一下服务器。

四、测试MySql

1. 登录Mysql

 1# 以root用户登录MySql,执行命令
2mysql -u root -p
3# 输入以上命令回车进入,出现输入密码提示,输入刚才查看到的临时密码,复制粘贴即可,Mysql密码是不显示的。
4
5
6Welcome to the MySQL monitor.  Commands end with ; or g.
7Your MySQL connection id is 11
8Server version: 8.0.15
9Copyright (c) 20002019, Oracle and/or its affiliates. All rights reserved.
10Oracle is a registered trademark of Oracle Corporation and/or its
11affiliates. Other names may be trademarks of their respective
12owners.
13Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.
14mysql>

出现上面的提示,则说明安装并启动成功,但是目前什么都操作不了,执行Mysql命令也会提示如下错误:

1【mysql】You must reset your password using ALTER USER statement before executing this statement.

原因分析:这个主要是由一个参数控制的 default_password_lifetime,该参数设置的是用户密码的过期时间,根本的原因是刚安装好数据库,Mysql要求你修改随机密码。
知道了原因,那我们就来修改密码

1# 修改用户密码
2alter user user() identified by "root123456";
3# 刷新权限
4flush privileges;

2. 设置root远程连接

1# 授权
2grant all privileges on *.* to 'root'@'%' identified by 'root123456';
3# 刷新权限
4flush privileges;

3. 退出Mysql

1# 通过命令退出 MySQL
2exit;



1605940369531071296.jpg


I Tech You, 我教你!(www.itechyou.cn)

一个专注于技术分享、免费教程、学习资源的博客!爱好网页技术、网络安全、技术分享、经验分享、IT资讯;最全网络技术学习资源,云服务器折扣活动分享。

欢迎关注,一起学习,共成长!

关于作者: 王俊南(Jonas)

昨夜寒蛩不住鸣。惊回千里梦,已三更。起来独自绕阶行。人悄悄,帘外月胧明。 白首为功名。旧山松竹老,阻归程。欲将心事付瑶琴。知音少,弦断有谁听。

热门文章