Linux(CentOS)下载安装mysql

481 阅读6分钟

Linux(CentOS)下载安装mysql

下载

  • 创建/opt/install/mysql目录
  • 下载最新版本的mysql到该目录,一定要下载服务器系统版本对应的,比如我的linux的CentOS7,所以我下的是Red Hat Enterprise Linux 7 / Oracle Linux 7 (x86, 64-bit), RPM Bundle
mkdir -p /opt/install/mysql
wget -O /opt/install/mysql/mysql-8.0.22-1.el7.x86_64.rpm-bundle.tar https://downloads.mysql.com/archives/get/p/23/file/mysql-8.0.22-1.el7.x86_64.rpm-bundle.tar

PS:官方下载页面downloads.mysql.com/archives/co…

解压

使用tar -xvf [指定的压缩档案包] -C [指定的解压目录]解压包。

mkdir -p /opt/install/mysql/mysql-8.0.22-1.el7.x86_64.rpm-bundle
tar -xvf /opt/install/mysql/mysql-8.0.22-1.el7.x86_64.rpm-bundle.tar -C /opt/install/mysql/mysql-8.0.22-1.el7.x86_64.rpm-bundle

检查是否已经安装了其他版本的mysql

使用rpm -qa | grep -i mysql 检查是否已经安装了其他版本的mysql

删除mysql

使用yum remove mysql-libs删除mysql相关的套件

如果原先没有安装过mysql但centos7会自带mariadb,mariadb是mysql的一个分支,所以不管怎么样,在安装之前最好删除一下mysql套件,这样可以把相关的套件都清理掉,避免新版本的mysql安装失败

[root@VM-0-8-centos ~]# yum remove mysql-libs
Loaded plugins: fastestmirror, langpacks
Resolving Dependencies
--> Running transaction check
---> Package mariadb-libs.x86_64 1:5.5.65-1.el7 will be erased
--> Processing Dependency: libmysqlclient.so.18()(64bit) for package: 2:postfix-2.10.1-9.el7.x86_64
--> Processing Dependency: libmysqlclient.so.18(libmysqlclient_18)(64bit) for package: 2:postfix-2.10.1-9.el7.x86_64
--> Running transaction check
---> Package postfix.x86_64 2:2.10.1-9.el7 will be erased
--> Finished Dependency Resolution
epel/7/x86_64                                            | 4.7 kB     00:00   
epel/7/x86_64/group_gz                                   |  95 kB     00:00   
epel/7/x86_64/updateinfo                                 | 1.0 MB     00:00   
epel/7/x86_64/primary_db                                 | 6.9 MB     00:00   
extras/7/x86_64                                          | 2.9 kB     00:00   
os/7/x86_64                                              | 3.6 kB     00:00   
updates/7/x86_64                                         | 2.9 kB     00:00   
updates/7/x86_64/primary_db                              | 5.7 MB     00:00   
 
Dependencies Resolved
 
================================================================================
 Package              Arch           Version                  Repository   Size
================================================================================
Removing:
 mariadb-libs         x86_64         1:5.5.65-1.el7           @os         4.4 M
Removing for dependencies:
 postfix              x86_64         2:2.10.1-9.el7           @os          12 M
 
Transaction Summary
================================================================================
Remove  1 Package (+1 Dependent package)
 
Installed size: 17 M
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-9.el7.x86_64                                1/2
  Erasing    : 1:mariadb-libs-5.5.65-1.el7.x86_64                           2/2
  Verifying  : 1:mariadb-libs-5.5.65-1.el7.x86_64                           1/2
  Verifying  : 2:postfix-2.10.1-9.el7.x86_64                                2/2
 
Removed:
  mariadb-libs.x86_64 1:5.5.65-1.el7                                          
 
Dependency Removed:
  postfix.x86_64 2:2.10.1-9.el7                                               
 
Complete!

安装mysql服务端

使用rpm -ivh [套件安装包]依次安装common、client-plugins、libs、client、server

[root@VM-0-8-centos ~]# rpm -ivh /opt/install/mysql/mysql-8.0.22-1.el7.x86_64.rpm-bundle/mysql-community-common-8.0.22-1.el7.x86_64.rpm
warning: /opt/install/mysql/mysql-8.0.22-1.el7.x86_64.rpm-bundle/mysql-community-common-8.0.22-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
   1:mysql-community-common-8.0.22-1.e################################# [100%]
[root@VM-0-8-centos ~]# rpm -ivh /opt/install/mysql/mysql-8.0.22-1.el7.x86_64.rpm-bundle/mysql-community-client-plugins-8.0.22-1.el7.x86_64.rpm
warning: /opt/install/mysql/mysql-8.0.22-1.el7.x86_64.rpm-bundle/mysql-community-client-plugins-8.0.22-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
   1:mysql-community-client-plugins-8.################################# [100%]
[root@VM-0-8-centos ~]# rpm -ivh /opt/install/mysql/mysql-8.0.22-1.el7.x86_64.rpm-bundle/mysql-community-libs-8.0.22-1.el7.x86_64.rpm
warning: /opt/install/mysql/mysql-8.0.22-1.el7.x86_64.rpm-bundle/mysql-community-libs-8.0.22-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
   1:mysql-community-libs-8.0.22-1.el7################################# [100%]
[root@VM-0-8-centos ~]# rpm -ivh /opt/install/mysql/mysql-8.0.22-1.el7.x86_64.rpm-bundle/mysql-community-client-8.0.22-1.el7.x86_64.rpm
warning: /opt/install/mysql/mysql-8.0.22-1.el7.x86_64.rpm-bundle/mysql-community-client-8.0.22-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
   1:mysql-community-client-8.0.22-1.e################################# [100%]
[root@VM-0-8-centos ~]# rpm -ivh /opt/install/mysql/mysql-8.0.22-1.el7.x86_64.rpm-bundle/mysql-community-server-8.0.22-1.el7.x86_64.rpm
warning: /opt/install/mysql/mysql-8.0.22-1.el7.x86_64.rpm-bundle/mysql-community-server-8.0.22-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
   1:mysql-community-server-8.0.22-1.e################################# [100%]

mysql安装成功后会自动创建mysql的用户和用户组

[root@VM-0-8-centos ~]# cat /etc/passwd | grep mysql
mysql:x:27:27:MySQL Server:/var/lib/mysql:/bin/false
[root@VM-0-8-centos ~]# cat /etc/group | grep mysql
mysql:x:27:

mysqladmin --version命令也可以验证是否安装成功

启动mysql服务

service mysqld start

[root@VM-0-8-centos ~]# service mysqld start
Redirecting to /bin/systemctl start mysqld.service

查看mysql状态

service mysqld status

[root@VM-0-8-centos ~]# 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: active (running) since Wed 2021-03-10 14:48:08 CST; 44min ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 29927 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
Main PID: 30025 (mysqld)
   Status: "Server is operational"
   CGroup: /system.slice/mysqld.service
           └─30025 /usr/sbin/mysqld
 
Mar 10 14:47:56 VM-0-8-centos systemd[1]: Starting MySQL Server...
Mar 10 14:48:08 VM-0-8-centos systemd[1]: Started MySQL Server.

查看root初始密码

cat /var/log/mysqld.log

如下,root初始密码为v8gKhwuZQy+_

[root@VM-0-8-centos ~]# cat /var/log/mysqld.log
2021-03-10T06:47:56.874640Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.0.22) initializing of server in progress as process 29954
2021-03-10T06:47:56.902166Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2021-03-10T06:47:58.641454Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2021-03-10T06:48:01.364931Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: v8gKhwuZQy+_
2021-03-10T06:48:07.060401Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.22) starting as process 30025
2021-03-10T06:48:07.081514Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2021-03-10T06:48:07.774453Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2021-03-10T06:48:07.927543Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Bind-address: '::' port: 33060, socket: /var/run/mysqld/mysqlx.sock
2021-03-10T06:48:08.175560Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2021-03-10T06:48:08.175772Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
2021-03-10T06:48:08.208388Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.22'  socket: '/var/lib/mysql/mysql.sock'  port: 3306  MySQL Community Server - GPL.

修改密码

mysqladmin -u [用户] -p password [密码]

默认密码策略为8位以上的包括大写字母、小写字母、数字和特殊字符的密码

[root@VM-0-8-centos ~]# mysqladmin -u root -p password Root@123456
Enter password: 【此处输入初始密码】
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.

登陆mysql

mysql -u[用户名] -p[密码]

[root@VM-0-8-centos ~]# mysql -uroot -pRoot@123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 15
Server version: 8.0.22 MySQL Community Server - GPL
 
Copyright (c) 2000, 2020, 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>

常用命令

service mysqld status        #查看mysql状态
service mysqld stop          #关闭
service mysqld start         #启动
service mysqld restart       #重启

开启Navicat远程连接

mysql> use mysql; # 进入到mysql库
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
 
Database changed
mysql> update user set host = '%' where user ='root'; # 设置所有主机都能访问root用户
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0
 
mysql> flush privileges; # 立即生效
Query OK, 0 rows affected (0.00 sec)
 
mysql> alter user 'root'@'%' identified with mysql_native_password by 'Root@123456'; # 修改加密规则并指定密码,即在navicat上使用用户名‘root’和密码‘Root@123456’登陆数据库
Query OK, 0 rows affected (0.01 sec)

如果已经开启Navicat远程连接但仍然连接不了可能是防火墙的问题,可以选择关闭防火墙或开放指定端口

 
# 关闭防火墙
systemctl stop firewalld.service
 
# 禁止firewall开机启动
systemctl disable firewalld.service
 
# 启动防火墙
systemctl start firewalld.service
 
# 开放指定端口
firewall-cmd --zone=public --add-port=3306/tcp --permanent
 
# 关闭指定端口
firewall-cmd --zone=public --remove-port=3306/tcp --permanent
 
# 重启防火墙,在修改指定端口后使用
firewall-cmd --reload
 
# 查看防火墙状态
firewall-cmd --state
 

使用rpm安装mysql的默认目录

目录说明
/var/lib/mysql数据库目录
/usr/share/mysql配置文件(mysql.server命令及配置文件)
/usr/bin相关命令(mysqladmin mysqldump等命令)
/etc/rc.d/init.d启动脚本(启动脚本文件mysql的目录)