一、清除Mariadb
首先清除CentOS7系统中默认的数据库mariadb,否则不能安装mysql。
代码语言:javascript复制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包:
代码语言:javascript复制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目录,解压缩安装包,命令如下:
代码语言:javascript复制1tar axvf mysql-5.7.36-1.el7.x86_64.rpm-bundle.tar
3. 安装rpm包
切换到下载包目录下(cd 你的下载目录),然后对每个包进行一次安装;
代码语言:javascript复制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配置
代码语言:javascript复制1vim /etc/my.cnf
修改如下配置:
代码语言:javascript复制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
完整的配置文件如下:
代码语言:javascript复制 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数据库
代码语言:javascript复制1mysqld --initialize
2chown mysql:mysql /data/mysql -R
3systemctl start mysqld.service
4systemctl enable mysqld
三、启动MySql服务
1. MySql相关命令
代码语言:javascript复制1# 启动
2systemctl start mysqld.service
3# 停止
4systemctl stop mysqld.service
5# 重启
6systemctl restart mysqld.service
2. 设置MySql开机自启
代码语言:javascript复制1systemctl enable mysqld
3. 查看Mysql随机密码
代码语言:javascript复制1cat /var/log/mysqld.log | grep password
如下输出的K7abt/-C7.S/则为密码
代码语言:javascript复制12022-02-08T09:26:55.733536Z 1 [Note] A temporary password is generated for root@localhost: K7abt/-C7.S/
4. 注意
启用服务时,有个坑,需要注意一下,Mysql启动可能会因为systemctl启动不成功,报如下的错:
代码语言:javascript复制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
代码语言:javascript复制1cat /var/log/mysqld.log
大概能出现如下错误:
代码语言:javascript复制 12022-02-08T09:29:41.944751Z 0 [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.945034Z 0 [Warning] Can't create test file /data/mysql/localhost.lower-test
32022-02-08T09:29:41.945093Z 0 [Note] /usr/sbin/mysqld (mysqld 5.7.36) starting as process 6619 ...
42022-02-08T09:29:41.947580Z 0 [Warning] Can't create test file /data/mysql/localhost.lower-test
52022-02-08T09:29:41.947605Z 0 [Warning] Can't create test file /data/mysql/localhost.lower-test
62022-02-08T09:29:41.951908Z 0 [Note] InnoDB: PUNCH HOLE support available
72022-02-08T09:29:41.951940Z 0 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
82022-02-08T09:29:41.951944Z 0 [Note] InnoDB: Uses event mutexes
92022-02-08T09:29:41.951948Z 0 [Note] InnoDB: GCC builtin __atomic_thread_fence() is used for memory barrier
102022-02-08T09:29:41.951951Z 0 [Note] InnoDB: Compressed tables use zlib 1.2.11
112022-02-08T09:29:41.951954Z 0 [Note] InnoDB: Using Linux native AIO
122022-02-08T09:29:41.952274Z 0 [Note] InnoDB: Number of pools: 1
132022-02-08T09:29:41.952382Z 0 [Note] InnoDB: Using CPU crc32 instructions
142022-02-08T09:29:41.953841Z 0 [Note] InnoDB: Initializing buffer pool, total size = 128M, instances = 1, chunk size = 128M
152022-02-08T09:29:41.960631Z 0 [Note] InnoDB: Completed initialization of buffer pool
162022-02-08T09:29:41.962524Z 0 [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.972692Z 0 [ERROR] InnoDB: Operating system error number 13 in a file operation.
182022-02-08T09:29:41.972733Z 0 [ERROR] InnoDB: The error means mysqld does not have the access rights to the directory.
192022-02-08T09:29:41.972738Z 0 [ERROR] InnoDB: os_file_get_status() failed on './ibdata1'. Can't determine file permissions
202022-02-08T09:29:41.972747Z 0 [ERROR] InnoDB: Plugin initialization aborted with error Generic error
212022-02-08T09:29:42.599175Z 0 [ERROR] Plugin 'InnoDB' init function returned error.
222022-02-08T09:29:42.599237Z 0 [ERROR] Plugin 'InnoDB' registration as a STORAGE ENGINE failed.
232022-02-08T09:29:42.599249Z 0 [ERROR] Failed to initialize builtin plugins.
242022-02-08T09:29:42.599254Z 0 [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
代码语言:javascript复制 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) 2000, 2019, 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命令也会提示如下错误:
代码语言:javascript复制1【mysql】You must reset your password using ALTER USER statement before executing this statement.
原因分析:这个主要是由一个参数控制的 default_password_lifetime,该参数设置的是用户密码的过期时间,根本的原因是刚安装好数据库,Mysql要求你修改随机密码。 知道了原因,那我们就来修改密码
代码语言:javascript复制1# 修改用户密码
2alter user user() identified by "root123456";
3# 刷新权限
4flush privileges;
2. 设置root远程连接
代码语言:javascript复制1# 授权
2grant all privileges on *.* to 'root'@'%' identified by 'root123456';
3# 刷新权限
4flush privileges;
3. 退出Mysql
代码语言:javascript复制1# 通过命令退出 MySQL
2exit;