Centos7 yum 安装Mysql 8

本文详细记录了MySQL8的安装步骤,包括添加官方源、安装服务、启动及密码设置。面对启动失败的问题,通过查看日志定位到数据目录错误,并成功解决了问题,最终实现MySQL服务正常运行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.加入mysql8官方源

rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
yum install mysql-serveryum clean all
yum makecache

2.安装mysql8

yum install mysql-community-server

3.启动mysql

systemctl start mysqld.service

3.1获取临时密码

[root@localhost mysql]# grep "A temporary password" /var/log/mysqld.log
2018-12-15T16:08:17.317445Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: -te-.s9yVqW;

3.2修改root密码,必须包含大小写特殊字母

[root@localhost mysql]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.13

Copyright (c) 2000, 2018, 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> alter user 'root'@'localhost' identified by 'Luca321@';
Query OK, 0 rows affected (0.06 sec)

4.启动解决失败

[root@localhost lib]# systemctl start 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.

4.1查看服务状态,没指出问题。

[root@localhost lib]# 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 Sat 2018-12-15 10:59:58 EST; 2min 37s ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 18496 ExecStart=/usr/sbin/mysqld $MYSQLD_OPTS (code=exited, status=1/FAILURE)
  Process: 18472 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
 Main PID: 18496 (code=exited, status=1/FAILURE)
   Status: "SERVER_BOOTING"
    Error: 2 (No such file or directory)

Dec 15 10:59:58 localhost.localdomain systemd[1]: Starting MySQL Server...
Dec 15 10:59:58 localhost.localdomain systemd[1]: mysqld.service: main process exited, code=exited, status=1/FAILURE
Dec 15 10:59:58 localhost.localdomain systemd[1]: Failed to start MySQL Server.
Dec 15 10:59:58 localhost.localdomain systemd[1]: Unit mysqld.service entered failed state.
Dec 15 10:59:58 localhost.localdomain systemd[1]: mysqld.service failed.

● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: failed (Result: exit-code) since Sat 2018-12-15 10:59:58 EST; 2min 37s ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 18496 ExecStart=/usr/sbin/mysqld $MYSQLD_OPTS (code=exited, status=1/FAILURE)
  Process: 18472 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
 Main PID: 18496 (code=exited, status=1/FAILURE)
   Status: "SERVER_BOOTING"
    Error: 2 (No such file or directory)

Dec 15 10:59:58 localhost.localdomain systemd[1]: Starting MySQL Server...
Dec 15 10:59:58 localhost.localdomain systemd[1]: mysqld.service: main process exited, code=exited, status=1/FAILURE
Dec 15 10:59:58 localhost.localdomain systemd[1]: Failed to start MySQL Server.
Dec 15 10:59:58 localhost.localdomain systemd[1]: Unit mysqld.service entered failed state.
Dec 15 10:59:58 localhost.localdomain systemd[1]: mysqld.service failed.

[root@localhost lib]# journalctl -xe
Dec 15 10:59:58 localhost.localdomain polkitd[751]: Registered Authentication Agent for unix-process:18466:834528 (system bus name :1.29 [/usr/bin/pkttyagent --notify-fd 5 --fallback], object path /org/freede
Dec 15 10:59:58 localhost.localdomain systemd[1]: Starting MySQL Server...
-- Subject: Unit mysqld.service has begun start-up
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
-- 
-- Unit mysqld.service has begun starting up.
Dec 15 10:59:58 localhost.localdomain systemd[1]: mysqld.service: main process exited, code=exited, status=1/FAILURE
Dec 15 10:59:58 localhost.localdomain systemd[1]: Failed to start MySQL Server.
-- Subject: Unit mysqld.service has failed
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
-- 
-- Unit mysqld.service has failed.
-- 
-- The result is failed.
Dec 15 10:59:58 localhost.localdomain systemd[1]: Unit mysqld.service entered failed state.
Dec 15 10:59:58 localhost.localdomain systemd[1]: mysqld.service failed.
Dec 15 10:59:58 localhost.localdomain polkitd[751]: Unregistered Authentication Agent for unix-process:18466:834528 (system bus name :1.29, object path /org/freedesktop/PolicyKit1/AuthenticationAgent, locale 
Dec 15 11:01:01 localhost.localdomain systemd[1]: Started Session 4 of user root.
-- Subject: Unit session-4.scope has finished start-up
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
-- 
-- Unit session-4.scope has finished starting up.
-- 
-- The start-up result is done.
Dec 15 11:01:01 localhost.localdomain systemd[1]: Starting Session 4 of user root.
-- Subject: Unit session-4.scope has begun start-up
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
-- 
-- Unit session-4.scope has begun starting up.
Dec 15 11:01:01 localhost.localdomain CROND[18503]: (root) CMD (run-parts /etc/cron.hourly)
Dec 15 11:01:01 localhost.localdomain run-parts(/etc/cron.hourly)[18506]: starting 0anacron
Dec 15 11:01:01 localhost.localdomain run-parts(/etc/cron.hourly)[18512]: finished 0anacron
Dec 15 11:02:22 localhost.localdomain dbus-daemon[745]: dbus[745]: avc:  received setenforce notice (enforcing=0)
Dec 15 11:02:22 localhost.localdomain dbus[745]: avc:  received setenforce notice (enforcing=0)
Dec 15 11:02:39 localhost.localdomain polkitd[751]: Registered Authentication Agent for unix-process:18518:850668 (system bus name :1.32 [/usr/bin/pkttyagent --notify-fd 5 --fallback], object path /org/freede
Dec 15 11:02:39 localhost.localdomain systemd[1]: Starting MySQL Server...
-- Subject: Unit mysqld.service has begun start-up
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
-- 
-- Unit mysqld.service has begun starting up.
Dec 15 11:02:40 localhost.localdomain systemd[1]: mysqld.service: main process exited, code=exited, status=1/FAILURE
Dec 15 11:02:40 localhost.localdomain systemd[1]: Failed to start MySQL Server.
-- Subject: Unit mysqld.service has failed
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
-- 
-- Unit mysqld.service has failed.
-- 
-- The result is failed.
Dec 15 11:02:40 localhost.localdomain systemd[1]: Unit mysqld.service entered failed state.
Dec 15 11:02:40 localhost.localdomain systemd[1]: mysqld.service failed.
Dec 15 11:02:40 localhost.localdomain polkitd[751]: Unregistered Authentication Agent for unix-process:18518:850668 (system bus name :1.32, object path /org/freedesktop/PolicyKit1/AuthenticationAgent, locale 
 

4.2只能查看mysql日志,这里很明显告诉我, /var/lib/mysql/有问题,

[root@localhost lib]# tail -f /var/log/mysqld.log 
2018-12-15T16:05:45.384297Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.0.13) initializing of server in progress as process 18584
2018-12-15T16:05:45.386729Z 0 [ERROR] [MY-010457] [Server] --initialize specified but the data directory has files in it. Aborting.
2018-12-15T16:05:45.386788Z 0 [ERROR] [MY-013236] [Server] Newly created data directory /var/lib/mysql/ is unusable. You can safely remove it.
2018-12-15T16:05:45.386797Z 0 [ERROR] [MY-010119] [Server] Aborting
2018-12-15T16:05:45.387015Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.13)  MySQL Community Server - GPL.
2018-12-15T16:05:45.645437Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.13) starting as process 18586
2018-12-15T16:05:45.656019Z 1 [ERROR] [MY-011011] [Server] Failed to find valid data directory.
2018-12-15T16:05:45.656216Z 0 [ERROR] [MY-010020] [Server] Data Dictionary initialization failed.
2018-12-15T16:05:45.656258Z 0 [ERROR] [MY-010119] [Server] Aborting
2018-12-15T16:05:45.656981Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.13)  MySQL Community Server - GPL.

4.3 进入目录删除数据

[root@localhost lib]# cd /var/lib/mysql
[root@localhost mysql]# ll
total 48
-rw-r-----. 1 mysql mysql     0 Dec 15 09:27 binlog.index
-rw-------. 1 mysql mysql  1675 Dec 15 09:27 ca-key.pem
-rw-r--r--. 1 mysql mysql  1107 Dec 15 09:27 ca.pem
-rw-r--r--. 1 mysql mysql  1107 Dec 15 09:27 client-cert.pem
-rw-------. 1 mysql mysql  1679 Dec 15 09:27 client-key.pem
drwx------. 2 mysql mysql 16384 Dec 15 08:36 lost+found
-rw-------. 1 mysql mysql  1679 Dec 15 09:27 private_key.pem
-rw-r--r--. 1 mysql mysql   451 Dec 15 09:27 public_key.pem
-rw-r--r--. 1 mysql mysql  1107 Dec 15 09:27 server-cert.pem
-rw-------. 1 mysql mysql  1679 Dec 15 09:27 server-key.pem
[root@localhost mysql]# rm -rf *

4.5尝试启动,解决问题

[root@localhost ~]# systemctl start mysqld.service  
[root@localhost ~]# netstat -tanlp
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1363/sshd           
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      18310/master        
tcp        0     96 10.205.127.71:22        10.205.57.106:50295     ESTABLISHED 10519/sshd: root@pt 
tcp6       0      0 :::3306                 :::*                    LISTEN      18676/mysqld        
tcp6       0      0 :::22                   :::*                    LISTEN      1363/sshd           
tcp6       0      0 ::1:25                  :::*                    LISTEN      18310/master        
tcp6       0      0 :::33060                :::*                    LISTEN      18676/mysqld        

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值