Linux系统安装MySQL的详细步骤

2021-05-08 11:11:28 浏览数 (1)

1 下载

https://dev.mysql.com/downloads/mysql/

确认linux对应的版本:

cat /proc/version

uname -a

lsb_release -a

cat /etc/redhat-release

cat /etc/issue

这个是内核的版本:

代码语言:javascript复制
3.10.0-1062.9.1.el7.x86_64

选择操作系统版本:

选择对应操作系统的mysql版本:

跳转到下载页面:

2.解压mysql的tar格式文件

代码语言:javascript复制
 tar -xvf mysql-8.0.20-1.el7.x86_64.rpm-bundle.tar

运行上述命令,得到如下结果:

直接解压会解压到当前目录下:

也可以创建一个文件夹,在同一个子目录下解压。

3.安装解压的rpm安装包

代码语言:javascript复制
# rpm -ivh mysql-community-common-8.0.20-1.el7.x86_64.rpm

# rpm -ivh mysql-community-libs-8.0.20-1.el7.x86_64.rpm

# rpm -ivh mysql-community-libs-compat-8.0.20-1.el7.x86_64.rpm

# rpm -ivh mysql-community-client-8.0.20-1.el7.x86_64.rpm

# rpm -ivh mysql-community-server-8.0.20-1.el7.x86_64.rpm

执行都第二部的时候报错:

代码语言:javascript复制
[root@VM_0_9_centos local]# rpm -ivh mysql-community-common-8.0.20-1.el7.x86_64.rpm
warning: mysql-community-common-8.0.20-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
  1:mysql-community-common-8.0.20-1.e################################# [100%]
[root@VM_0_9_centos local]# rpm -ivh mysql-community-libs-8.0.20-1.el7.x86_64.rpm
warning: mysql-community-libs-8.0.20-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
error: Failed dependencies:
mariadb-libs is obsoleted by mysql-community-libs-8.0.20-1.el7.x86_64

通过查找得知:

(图片来自:https://www.cnblogs.com/gpdm/p/7170521.html)

所以将如上的命令进行修改如下:

代码语言:javascript复制
# rpm -ivh mysql-community-common-8.0.20-1.el7.x86_64.rpm

# rpm -ivh mysql-community-libs-8.0.20-1.el7.x86_64.rpm --force --nodeps

# rpm -ivh mysql-community-libs-compat-8.0.20-1.el7.x86_64.rpm

# rpm -ivh mysql-community-client-8.0.20-1.el7.x86_64.rpm

# rpm -ivh mysql-community-server-8.0.20-1.el7.x86_64.rpm

修改后,正常安装community-libs,如下图:

继续安装 mysql-community-libs-compact,遇到同样的问题:

代码语言:javascript复制
[root@VM_0_9_centos local]# rpm -ivh mysql-community-libs-compat-8.0.20-1.el7.x86_64.rpm
warning: mysql-community-libs-compat-8.0.20-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
error: Failed dependencies:
mariadb-libs is obsoleted by mysql-community-libs-compat-8.0.20-1.el7.x86_64

同样的问题,自然实用同样的解法:

代码语言:javascript复制
# rpm -ivh mysql-community-common-8.0.20-1.el7.x86_64.rpm

# rpm -ivh mysql-community-libs-8.0.20-1.el7.x86_64.rpm --force --nodeps

# rpm -ivh mysql-community-libs-compat-8.0.20-1.el7.x86_64.rpm --force --nodeps

# rpm -ivh mysql-community-client-8.0.20-1.el7.x86_64.rpm

# rpm -ivh mysql-community-server-8.0.20-1.el7.x86_64.rpm --force --nodeps

所有命令执行完成以后,如下图:

4.修改配置文件

修改配置文件前自然要知道配置文件在哪儿?

执行命令:

代码语言:javascript复制
find / -name my.cnf

打开my.cnf,确认是否式mysql的配置文件:

内容如下:

代码语言:javascript复制
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/8.0/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 the leading "# " to disable binary logging
# Binary logging captures changes between backups and is enabled by
# default. It's default setting is log_bin=binlog
# disable_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
#
# Remove leading # to revert to previous value for default_authentication_plugin,
# this will increase compatibility with older clients. For background, see:
# https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_default_authentication_plugin
# default-authentication-plugin=mysql_native_password

datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

在配置文件中添加如下内容:

代码语言:javascript复制
 [client]
 default-character-set=utf8
 [mysqld]
 default-storage-engine=INNODB
 character-set-server=utf8
 collation-server=utf8_generic_ci

找到 /etc/my.cnf,输入命令

代码语言:javascript复制
vim my.cnf

按下键盘 i 键,使vim从一般模式变更为编辑模式。添加上面待增加的内容,如下:

想要退出的时候提示:

代码语言:javascript复制
E37: No write since last change (add ! to override)

解决方法如下:

(图片来自:https://www.cnblogs.com/liuyuan216/p/3510594.html)

保存后再次打开,便可以看到刚才的新增内容,已经保存了。

4.启动mysql

(ME:其实mysql的具体步骤也挺有意思的,当你找到它的规章的时候,就能够很顺手的使用它,并希望更进一步的认识它)

代码语言:javascript复制
service mysql start

但是你会发现,并没有找到对应的内容,提示如下:

内容如下:

代码语言:javascript复制
[root@VM_0_9_centos etc]# service mysql start
Redirecting to /bin/systemctl start mysql.service
Failed to start mysql.service: Unit not found.

换一种方式:

代码语言:javascript复制
[root@VM_0_9_centos bin]# systemctl restart mysqld.service
Job for mysqld.service failed because the control process exited with error code. See "systemctl status mysqld.service" and "journalctl -xe" for details.

提示如下内容:

回到系统中去可以看到,对应的目录中根本就没有mysqld.service文件:

那么这个文件在什么地方呢?

从这就能看出来对应的路径是不对的,按照windows的配置逻辑,应该在配置文件中,对于路径进行配置。

上面的这个步骤应该有问题,删除掉mysql

4.删除、卸载linux上的mysql

1)查看安装情况

代码语言:javascript复制
rpm -qa | grep -i mysql

2)移除安装

代码语言:javascript复制
# rpm -ev mysql-community-common-8.0.20-1.el7.x86_64.rpm

# rpm -ev mysql-community-libs-8.0.20-1.el7.x86_64.rpm --force --nodeps

# rpm -ev mysql-community-libs-compat-8.0.20-1.el7.x86_64.rpm --force --nodeps

# rpm -ev mysql-community-client-8.0.20-1.el7.x86_64.rpm

# rpm -ev mysql-community-server-8.0.20-1.el7.x86_64.rpm --force --nodeps

发现没有一个正常安装的。那刚才的操作指定是错的。

3)查找mysql的目录并删除文件和库

删除对应目录下的文件:

代码语言:javascript复制
rm -rf /etc/selinux/targeted/active/modules/100/mysql
rm -rf /etc/logrotate.d/mysql
rm -rf /usr/share/mysql
rm -rf /usr/bin/mysql
rm -rf /usr/lib64/mysql
rm -rf /var/lib/mysql

删除后确认:

代码语言:javascript复制
find / -name mysql

4)再次安装以及纠错调试

代码语言:javascript复制
# rpm -ivh mysql-community-common-8.0.20-1.el7.x86_64.rpm
# rpm -ivh mysql-community-libs-8.0.20-1.el7.x86_64.rpm --force --nodeps
# rpm -ivh mysql-community-libs-compat-8.0.20-1.el7.x86_64.rpm --force --nodeps
# rpm -ivh mysql-community-client-8.0.20-1.el7.x86_64.rpm
# rpm -ivh mysql-community-server-8.0.20-1.el7.x86_64.rpm --force --nodeps

mysql-community-common libs libs-compat client server的安装顺序这几个是这种安装顺序么?

找到mysqld的位置:

代码语言:javascript复制
find / -name mysqld

切换到sbin目录下:

运行命令:

代码语言:javascript复制
mysqld --initialize --console

报错:

代码语言:javascript复制
mysqld: error while loading shared libraries: libnuma.so.1: cannot open shared object file: No such file or directory

解决如下:

代码语言:javascript复制
yum -y install numactl

初始化数据库:

代码语言:javascript复制
mysqld --initialize --console

目录授权,否则启动失败:

代码语言:javascript复制
chown -R mysql:mysql /var/lib/mysql/

启动mysql服务:

代码语言:javascript复制
systemctl start mysqld

启动的时候报错:

代码语言:javascript复制
[root@VM_0_9_centos mysql]# service mysqld status
Redirecting to /bin/systemctl status mysqld.service
● mysqld.service - MySQL Server
  Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
  Active: failed (Result: exit-code) since Mon 2020-05-04 23:41:56 CST; 4min 12s ago
    Docs: man:mysqld(8)
          http://dev.mysql.com/doc/refman/en/using-systemd.html
Main PID: 5976 (code=exited, status=1/FAILURE)
  Status: "Server startup in progress"

May 04 23:41:56 VM_0_9_centos systemd[1]: Starting MySQL Server...
May 04 23:41:56 VM_0_9_centos systemd[1]: mysqld.service: main process exited, code=exited, status=1/FAILURE
May 04 23:41:56 VM_0_9_centos systemd[1]: Failed to start MySQL Server.
May 04 23:41:56 VM_0_9_centos systemd[1]: Unit mysqld.service entered failed state.
May 04 23:41:56 VM_0_9_centos systemd[1]: mysqld.service failed.

查看my.cnf中对应的错误日志目录下的mysqld.log文本文件,内容如下:

代码语言:javascript复制
2020-05-04T15:57:53.230522Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.0.20) initializing of server in progress as process 9183
2020-05-04T15:57:53.231829Z 0 [ERROR] [MY-010457] [Server] --initialize specified but the data directory has files in it. Aborting.
2020-05-04T15:57:53.231845Z 0 [ERROR] [MY-013236] [Server] The designated data directory /var/lib/mysql/ is unusable. You can remove all files that the server added to it.
2020-05-04T15:57:53.231888Z 0 [ERROR] [MY-010119] [Server] Aborting
2020-05-04T15:57:53.231985Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.20)  MySQL Community Server - GPL.
2020-05-04T15:57:53.431818Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.20) starting as process 9185
2020-05-04T15:57:53.438939Z 1 [ERROR] [MY-011011] [Server] Failed to find valid data directory.
2020-05-04T15:57:53.439040Z 0 [ERROR] [MY-010020] [Server] Data Dictionary initialization failed.
2020-05-04T15:57:53.439076Z 0 [ERROR] [MY-010119] [Server] Aborting
2020-05-04T15:57:53.439567Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.20)  MySQL Community Server - GPL.

看第一个错误:

代码语言:javascript复制
2020-05-04T15:57:53.231829Z 0 [ERROR] [MY-010457] [Server] --initialize specified but the data directory has files in it. Aborting.

根据提示清空datadir下的文件:

再次执行初始化,并启动:

代码语言:javascript复制
mysqld --initialize --console

可以看到对应的datadir的变化:

再次启动:

代码语言:javascript复制
systemctl start mysqld

报错如下:

代码语言:javascript复制
Job for mysqld.service failed because the control process exited with error code. See "systemctl status mysqld.service" and "journalctl -xe" for details.

再次执行查看日志:

代码语言:javascript复制
2020-05-04T16:29:22.144059Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.0.20) initializing of server in progress as process 15898
2020-05-04T16:29:22.148758Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2020-05-04T16:29:23.736345Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2020-05-04T16:29:25.574139Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: y_k?j7%qsc&B
2020-05-04T16:29:29.423435Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.20) starting as process 15964
2020-05-04T16:29:29.434056Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2020-05-04T16:29:30.064267Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2020-05-04T16:29:30.190111Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Socket: '/var/run/mysqld/mysqlx.sock' bind-address: '::' port: 33060
2020-05-04T16:29:30.391394Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2020-05-04T16:29:30.392561Z 0 [ERROR] [MY-010273] [Server] Could not create unix socket lock file /var/lib/mysql/mysql.sock.lock.
2020-05-04T16:29:30.392720Z 0 [ERROR] [MY-010268] [Server] Unable to setup unix socket lock file.
2020-05-04T16:29:30.393030Z 0 [ERROR] [MY-010119] [Server] Aborting
2020-05-04T16:29:31.985836Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.20)  MySQL Community Server - GPL.
代码语言:javascript复制
[root@VM_0_9_centos log]# systemctl status mysqld.service
● mysqld.service - MySQL Server
  Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
  Active: failed (Result: exit-code) since Tue 2020-05-05 00:29:32 CST; 1min 19s ago
    Docs: man:mysqld(8)
          http://dev.mysql.com/doc/refman/en/using-systemd.html
 Process: 15964 ExecStart=/usr/sbin/mysqld $MYSQLD_OPTS (code=exited, status=1/FAILURE)
 Process: 15871 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
Main PID: 15964 (code=exited, status=1/FAILURE)
  Status: "Server startup in progress"
   Error: 2 (No such file or directory)

May 05 00:29:22 VM_0_9_centos systemd[1]: Starting MySQL Server...
May 05 00:29:32 VM_0_9_centos systemd[1]: mysqld.service: main process exited, code=exited, status=1/FAILURE
May 05 00:29:32 VM_0_9_centos systemd[1]: Failed to start MySQL Server.
May 05 00:29:32 VM_0_9_centos systemd[1]: Unit mysqld.service entered failed state.
May 05 00:29:32 VM_0_9_centos systemd[1]: mysqld.service failed.

随便错误一个就会出问题:

提升data文件夹权限:

代码语言:javascript复制
chmod -R 777 /usr/local/mysql/data

提升权限后再次执行,没有报错:

对应如下问题:

正常启动后,日志中提示如下内容:

代码语言:javascript复制
2020-05-04T16:39:31.860322Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.0.20) initializing of server in progress as process 18122
2020-05-04T16:39:31.864947Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2020-05-04T16:39:33.387572Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2020-05-04T16:39:35.182236Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: d!&_3kCN-Jb*
2020-05-04T16:39:41.563852Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.20) starting as process 18220
2020-05-04T16:39:41.573468Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2020-05-04T16:39:41.573545Z 1 [ERROR] [MY-012271] [InnoDB] The innodb_system data file 'ibdata1' must be writable
2020-05-04T16:39:41.573578Z 1 [ERROR] [MY-012278] [InnoDB] The innodb_system data file 'ibdata1' must be writable
2020-05-04T16:39:41.573615Z 1 [ERROR] [MY-010334] [Server] Failed to initialize DD Storage Engine
2020-05-04T16:39:41.573705Z 0 [ERROR] [MY-010020] [Server] Data Dictionary initialization failed.
2020-05-04T16:39:41.573764Z 0 [ERROR] [MY-010119] [Server] Aborting
2020-05-04T16:39:41.574234Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.20)  MySQL Community Server - GPL.

到这终于看到正常启动了:

根据上面的临时密码登录mysql:

但是其中出现如下问题:

代码语言:javascript复制
-bash: mysql: command not found

这是由于没有正确安装mysql的客户端所引起的问题,解决

代码语言:javascript复制
# rpm -ivh mysql-community-common-8.0.20-1.el7.x86_64.rpm
# rpm -ivh mysql-community-libs-8.0.20-1.el7.x86_64.rpm --force --nodeps
# rpm -ivh mysql-community-libs-compat-8.0.20-1.el7.x86_64.rpm --force --nodeps
# rpm -ivh mysql-community-client-8.0.20-1.el7.x86_64.rpm --force --nodeps
# rpm -ivh mysql-community-server-8.0.20-1.el7.x86_64.rpm --force --nodeps

也就是客户端的安装加上对应的--force --nodeps,安装完成后才弹出对应的输入密码的提示。

输入密码后再次提示错误:

代码语言:javascript复制
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)

配置完client的配置文件后,正常开启mysql:

进入后修改默认密码:

代码语言:javascript复制
alter USER 'root'@'localhost' IDENTIFIED BY '新密码(必须包含:数字大小写字母特殊字符)';
代码语言:javascript复制
show databases;
use mysql;
select host,user,authentication_string,plugin from user;

执行后结果如下图:

host为localhost,表示只能本地连接mysql服务。

改为可以远程登录:

代码语言:javascript复制
update user set host = "%" where user='root';

刷新权限:

代码语言:javascript复制
flush privileges;

报错:

代码语言:javascript复制
1251-Client does not support autentication protocol requested by server;consider upgrading MySQL client

使用新密码进入修改'root'@'%'对应的原生密码:

语句如下:

代码语言:javascript复制
show databases;
use mysql;
alter USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '***********';
flush privileges;

完成后,去navicat上验证得到结果:

微信:haodeliumang

0 人点赞