CentOS7安装MySQL

2 篇文章 0 订阅
1 篇文章 0 订阅

1.下载MySQL安装包

官网地址:https://dev.mysql.com/downloads/mysql/
选择Linux-Generic 通用版本即可,
选择x86,64位
在这里插入图片描述

点击DownLoad–》点击No thinks,just start my download 直接下载就可以了。
在这里插入图片描述

2.创建用户、组、路径

2.1.创建home/mysql文件夹

[root@localhost home]# mkdir mysql

2.2.创建mysql组

[root@localhost ~]# groupadd mysql  

2.3.创建mysql用户,并指定组和默认路径

[root@localhost ~]#  useradd -r -d /home/mysql -g mysql mysql

2.4.将mysql的默认用户的组和路径改为mysql

[root@localhost ~]# chown -R mysql:mysql /home/mysql

3.导入安装包

3.1.进入/usr/local路径下

[root@localhost local]# pwd
/usr/local

3.2.通过rz 命令,导入安装包。

4.安装

4.1.解压

[root@localhost local]# tar -xvf mysql-8.0.32-linux-glibc2.12-x86_64.tar.xz

4.2.创建软连接

[root@localhost local]# ln -s mysql-8.0.32-linux-glibc2.12-x86_64 mysql
[root@localhost local]# ls -l
总用量 596012
drwxr-xr-x. 2 root root         6 411 2018 bin
drwxr-xr-x. 2 root root         6 411 2018 etc
drwxr-xr-x. 2 root root         6 411 2018 games
drwxr-xr-x. 2 root root         6 411 2018 include
drwxr-xr-x. 2 root root         6 411 2018 lib
drwxr-xr-x. 2 root root         6 411 2018 lib64
drwxr-xr-x. 2 root root         6 411 2018 libexec
lrwxrwxrwx. 1 root root        35 28 11:06 mysql -> mysql-8.0.32-linux-glibc2.12-x86_64
drwxr-xr-x. 9 root root       129 28 11:03 mysql-8.0.32-linux-glibc2.12-x86_64
-r--------. 1 root root 610315332 27 17:11 mysql-8.0.32-linux-glibc2.12-x86_64.tar.xz
drwxr-xr-x. 2 root root         6 411 2018 sbin
drwxr-xr-x. 5 root root        49 26 17:06 share
drwxr-xr-x. 2 root root         6 411 2018 src

4.3.更改mysql用户和组

root@localhost local]# chown -R mysql:mysql /usr/local/mysql
[root@localhost local]# ls -lF
总用量 596012
drwxr-xr-x. 2 root  root          6 411 2018 bin/
drwxr-xr-x. 2 root  root          6 411 2018 etc/
drwxr-xr-x. 2 root  root          6 411 2018 games/
drwxr-xr-x. 2 root  root          6 411 2018 include/
drwxr-xr-x. 2 root  root          6 411 2018 lib/
drwxr-xr-x. 2 root  root          6 411 2018 lib64/
drwxr-xr-x. 2 root  root          6 411 2018 libexec/
lrwxrwxrwx. 1 mysql mysql        35 28 11:06 mysql -> mysql-8.0.32-linux-glibc2.12-x86_64/
drwxr-xr-x. 9 root  root        129 28 11:03 mysql-8.0.32-linux-glibc2.12-x86_64/
-r--------. 1 root  root  610315332 27 17:11 mysql-8.0.32-linux-glibc2.12-x86_64.tar.xz
drwxr-xr-x. 2 root  root          6 411 2018 sbin/
drwxr-xr-x. 5 root  root         49 26 17:06 share/
drwxr-xr-x. 2 root  root          6 411 2018 src/

4.4.配置MySQL安装环境

[root@localhost local]# vi /etc/profile

在profile最后添加

export PATH=$PATH:/usr/local/mysql/bin

4.5.关闭防火墙

4.5.1.查看防火墙状态

[root@localhost local]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
   Active: active (running) since 三 2023-02-08 11:13:53 CST; 1min 8s ago
     Docs: man:firewalld(1)
 Main PID: 19605 (firewalld)
   CGroup: /system.slice/firewalld.service
           └─19605 /usr/bin/python2 -Es /usr/sbin/firewalld --nofork --nopid

2月 08 11:13:53 localhost.localdomain systemd[1]: Starting firewalld - dynamic firewall daemon...
2月 08 11:13:53 localhost.localdomain systemd[1]: Started firewalld - dynamic firewall daemon.
2月 08 11:13:53 localhost.localdomain firewalld[19605]: WARNING: AllowZoneDrifting is enabled. This is c...ow.
Hint: Some lines were ellipsized, use -l to show in full.

4.5.2.关闭防火墙

[root@localhost local]# systemctl stop firewalld
[root@localhost local]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
   Active: inactive (dead)
     Docs: man:firewalld(1)

2月 07 15:53:48 localhost.localdomain systemd[1]: Starting firewalld - dynamic firewall daemon...
2月 07 15:53:48 localhost.localdomain systemd[1]: Started firewalld - dynamic firewall daemon.
2月 07 15:53:48 localhost.localdomain firewalld[2379]: WARNING: AllowZoneDrifting is enabled. This is co...ow.
2月 08 11:13:26 localhost.localdomain systemd[1]: Stopping firewalld - dynamic firewall daemon...
2月 08 11:13:26 localhost.localdomain systemd[1]: Stopped firewalld - dynamic firewall daemon.
2月 08 11:13:53 localhost.localdomain systemd[1]: Starting firewalld - dynamic firewall daemon...
2月 08 11:13:53 localhost.localdomain systemd[1]: Started firewalld - dynamic firewall daemon.
2月 08 11:13:53 localhost.localdomain firewalld[19605]: WARNING: AllowZoneDrifting is enabled. This is c...ow.
2月 08 11:16:15 localhost.localdomain systemd[1]: Stopping firewalld - dynamic firewall daemon...
2月 08 11:16:15 localhost.localdomain systemd[1]: Stopped firewalld - dynamic firewall daemon.
Hint: Some lines were ellipsized, use -l to show in full.

4.6.关闭SELinux

SELinux 是 2.6 版本的 Linux 内核中提供的强制访问控制 (MAC) 系统。
对于目前可用的 Linux 安全模块来说,SELinux 是功能最全面,而且测试最充分的,它是在 20 年的 MAC 研究基础上建立的。SELinux 在类型强制服务器中合并了多级安全性或一种可选的多类策略,并采用了基于角色的访问控制概念。
由美国国家安全局开发。
学习时可以关闭。

  • enforcing:强制模式,SELinux 正在运行中,已经在限制 domain/type。
  • permissive:宽容模式:SELinux 正在运行中,但仅发出警告信息,并不会实际限制 domain/type
    的存取(permissive模式可以用在测试环境中供调试规则时使用)。
  • disabled:关闭,SELinux 不再运行。

将SELINUX=enforcing改为SELINUX=disable

[root@localhost local]# vi /etc/selinux/config

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=enforcing
# SELINUXTYPE= can take one of three values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected. 
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted 

4.6.1.重启,使之生效

[root@localhost local]# reboot

4.6.2.查看当前selinux的运行模式

[root@localhost local]# getenforce
Enforcing

[root@localhost local]# getenforce
Permissive

4.7.创建mysql目录

cd /usr/local/mysql

4.7.1.创建data目录

[root@localhost mysql]# mkdir /usr/local/mysql/data
[root@localhost mysql]# ls -lF
总用量 292
drwxr-xr-x.  2 7161 31415   4096 1217 00:54 bin/
drwxr-xr-x.  2 root root       6 28 14:59 data/
drwxr-xr-x.  2 7161 31415     56 1217 00:54 docs/
drwxr-xr-x.  3 7161 31415    282 1217 00:54 include/
drwxr-xr-x.  6 7161 31415    201 1217 00:54 lib/
-rw-r--r--.  1 7161 31415 283374 1216 23:34 LICENSE
drwxr-xr-x.  4 7161 31415     30 1217 00:54 man/
-rw-r--r--.  1 7161 31415    666 1216 23:34 README
drwxr-xr-x. 28 7161 31415   4096 1217 00:54 share/
drwxr-xr-x.  2 7161 31415     77 1217 00:54 support-files/
[root@localhost mysql]# 

4.7.2.将data目录的用户和组更改为mysql

[root@localhost mysql]# chown mysql:mysql /usr/local/mysql/data

4.7.3.将data目录的权限设置为750

[root@localhost mysql]# chmod 750 /usr/local/mysql/data
[root@localhost mysql]# ls -lF
总用量 292
drwxr-xr-x.  2  7161 31415   4096 1217 00:54 bin/
drwxr-x---.  2 mysql mysql      6 28 14:59 data/
drwxr-xr-x.  2  7161 31415     56 1217 00:54 docs/
drwxr-xr-x.  3  7161 31415    282 1217 00:54 include/
drwxr-xr-x.  6  7161 31415    201 1217 00:54 lib/
-rw-r--r--.  1  7161 31415 283374 1216 23:34 LICENSE
drwxr-xr-x.  4  7161 31415     30 1217 00:54 man/
-rw-r--r--.  1  7161 31415    666 1216 23:34 README
drwxr-xr-x. 28  7161 31415   4096 1217 00:54 share/
drwxr-xr-x.  2  7161 31415     77 1217 00:54 support-files/

4.7.4.修改配置文件my.cnf(若没有则新建)

[root@localhost mysql]# vi /etc/my.cnf
[mysqld]
port=3306
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
log-error=/usr/local/mysql/data/error.log
socket=/tmp/mysql.sock
pid-file=/usr/local/mysql/data/mysql.pid
character-set-server = utf8
lower_case_table_names=1
innodb_log_file_size=1G
default-storage-engine=INNODB
default_authentication_plugin=mysql_native_password
[client]
port=3306
default-character-set=utf8 

4.8.mysql初始化

[root@localhost mysql]# bin/mysqld --initialize --user=mysql
[root@localhost mysql]# cd data
[root@localhost data]# ll
总用量 78272
-rw-r-----. 1 mysql mysql       56 28 15:12 auto.cnf
-rw-------. 1 mysql mysql     1676 28 15:12 ca-key.pem
-rw-r--r--. 1 mysql mysql     1112 28 15:12 ca.pem
-rw-r--r--. 1 mysql mysql     1112 28 15:12 client-cert.pem
-rw-------. 1 mysql mysql     1676 28 15:12 client-key.pem
-rw-r-----. 1 mysql mysql     1203 28 15:12 error.log
-rw-r-----. 1 mysql mysql   196608 28 15:12 #ib_16384_0.dblwr
-rw-r-----. 1 mysql mysql  8585216 28 15:12 #ib_16384_1.dblwr
-rw-r-----. 1 mysql mysql     5986 28 15:12 ib_buffer_pool
-rw-r-----. 1 mysql mysql 12582912 28 15:12 ibdata1
drwxr-x---. 2 mysql mysql     4096 28 15:12 #innodb_redo
drwxr-x---. 2 mysql mysql        6 28 15:12 #innodb_temp
drwxr-x---. 2 mysql mysql      143 28 15:12 mysql
-rw-r-----. 1 mysql mysql 25165824 28 15:12 mysql.ibd
drwxr-x---. 2 mysql mysql     8192 28 15:12 performance_schema
-rw-------. 1 mysql mysql     1680 28 15:12 private_key.pem
-rw-r--r--. 1 mysql mysql      452 28 15:12 public_key.pem
-rw-r--r--. 1 mysql mysql     1112 28 15:12 server-cert.pem
-rw-------. 1 mysql mysql     1680 28 15:12 server-key.pem
drwxr-x---. 2 mysql mysql       28 28 15:12 sys
-rw-r-----. 1 mysql mysql 16777216 28 15:12 undo_001
-rw-r-----. 1 mysql mysql 16777216 28 15:12 undo_002
[root@localhost data]# cat error.log 

[root@localhost data]# cat error.log 
2023-02-08T07:12:07.056300Z 0 [Warning] [MY-010918] [Server] 'default_authentication_plugin' is deprecated and will be removed in a future release. Please use authentication_policy instead.
2023-02-08T07:12:07.056346Z 0 [System] [MY-013169] [Server] /usr/local/mysql-8.0.32-linux-glibc2.12-x86_64/bin/mysqld (mysqld 8.0.32) initializing of server in progress as process 866
2023-02-08T07:12:07.061478Z 0 [Warning] [MY-013242] [Server] --character-set-server: 'utf8' is currently an alias for the character set UTF8MB3, but will be an alias for UTF8MB4 in a future release. Please consider using UTF8MB4 in order to be unambiguous.
2023-02-08T07:12:07.067456Z 0 [Warning] [MY-013907] [InnoDB] Deprecated configuration parameters innodb_log_file_size and/or innodb_log_files_in_group have been used to compute innodb_redo_log_capacity=2147483648. Please use innodb_redo_log_capacity instead.
2023-02-08T07:12:07.072431Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2023-02-08T07:12:09.084598Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2023-02-08T07:12:12.767236Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: 5%bT3xMH?wRG

4.9.启动mysql

cd /usr/local/mysql

4.9.1.启动命令

[root@localhost mysql]# support-files/mysql.server start
Starting MySQL.... SUCCESS! 

4.9.2. 查看mysql进程

[root@localhost mysql]# ps -ef|grep mysql
root      6791     1  0 15:18 pts/0    00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/usr/local/mysql/data --pid-file=/usr/local/mysql/data/mysql.pid
mysql     7018  6791 18 15:18 pts/0    00:00:03 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/usr/local/mysql/data/error.log --pid-file=/usr/local/mysql/data/mysql.pid --socket=/tmp/mysql.sock --port=3306
root      7340  1424  0 15:18 pts/0    00:00:00 grep --color=auto mysql

4.10.登录mysql

4.10.1.密码通过查看/usr/local/mysql/data/error.log 获取

2023-02-08T07:12:12.767236Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: 5%bT3xMH?wRG

4.10.2.通过mysql -u root -p’密码’登录

[root@localhost mysql]# mysql -u root -p'5%bT3xMH?wRG'
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 8
Server version: 8.0.32

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

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> 

4.10.3.修改root用户密码

mysql> alter user 'root'@'localhost' identified by '123456';
Query OK, 0 rows affected (0.00 sec)

4.10.4.赋权

mysql> grant all on *.* to 'root'@'%';
ERROR 1410 (42000): You are not allowed to create a user with GRANT

报错原因:默认安装的mysql的root用户只支持本地连接。Host为localhsot
解决方法:
执行命令

mysql>  update user set host='%' where user='root';
Query OK, 0 rows affected (0.00 sec)
Rows matched: 1  Changed: 0  Warnings: 0

刷新权限

mysql> flush privileges; 
Query OK, 0 rows affected (0.02 sec)

再次赋权成功

mysql> GRANT ALL ON *.* TO 'root'@'%';
Query OK, 0 rows affected (0.01 sec)

4.11.使用DBeaver连接MySQL数据库

选择新建数据库连接–》选择MySQL–》选择下一步
在这里插入图片描述

服务器地址填写IP地址
数据库填写mysql
点击测试连接,提示已连接。点击确定。
在这里插入图片描述

4.11.1.问题:

数据库名称通过后台查询,发现有mysql和sys两个。输入其它会报错。

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

4.12.配置mysql开机启动

4.12.1.将mysql.server 复制到init.d 下

[root@localhost mysql]# cp support-files/mysql.server  /etc/init.d/mysqld

4.12.2.通过chkconfig命令将mysql服务添加到开机列表

[root@localhost mysql]# chkconfig --add mysqld

4.12.3.查看开机列表中是否已经存在mysql服务。

[root@localhost mysql]# chkconfig --list

注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。 

      要列出 systemd 服务,请执行 'systemctl list-unit-files'。
      查看在具体 target 启用的服务请执行
      'systemctl list-dependencies [target]'。

mysqld          0:关    1:关    2:开    3:开    4:开    5:开    6:关
netconsole      0:关    1:关    2:关    3:关    4:关    5:关    6:关
network         0:关    1:关    2:开    3:开    4:开    5:开    6:关

5.如果使用rz sz命令,需要安装

[root@localhost local]# yum -y install lrzsz
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.huaweicloud.com
 * extras: mirrors.huaweicloud.com
 * updates: mirrors.ustc.edu.cn
base                                                                                    | 3.6 kB  00:00:00     
extras                                                                                  | 2.9 kB  00:00:00     
updates                                                                                 | 2.9 kB  00:00:00     
正在解决依赖关系
--> 正在检查事务
---> 软件包 lrzsz.x86_64.0.0.12.20-36.el7 将被 安装
--> 解决依赖关系完成

依赖关系解决

===============================================================================================================
 Package                架构                    版本                               源                     大小
===============================================================================================================
正在安装:
 lrzsz                  x86_64                  0.12.20-36.el7                     base                   78 k

事务概要
===============================================================================================================
安装  1 软件包

总下载量:78 k
安装大小:181 k
Downloading packages:
lrzsz-0.12.20-36.el7.x86_64.rpm                                                         |  78 kB  00:00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  正在安装    : lrzsz-0.12.20-36.el7.x86_64                                                                1/1 
  验证中      : lrzsz-0.12.20-36.el7.x86_64                                                                1/1 

已安装:
  lrzsz.x86_64 0:0.12.20-36.el7                                                                                

完毕!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值