yum方式安装
- 将MySQL添加到yum的repo库中
 
yum localinstall https://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm
已加载插件:fastestmirror, product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager to register.
mysql57-community-release-el7-8.noarch.rpm                                                                                                                                                                                        | 8.9 kB  00:00:00     
正在检查 /var/tmp/yum-root-b_ONwy/mysql57-community-release-el7-8.noarch.rpm: mysql57-community-release-el7-8.noarch
/var/tmp/yum-root-b_ONwy/mysql57-community-release-el7-8.noarch.rpm 将被安装
正在解决依赖关系
--> 正在检查事务
---> 软件包 mysql57-community-release.noarch.0.el7-8 将被 安装
--> 解决依赖关系完成
依赖关系解决
=========================================================================================================================================================================================================================================================
 Package                                                           架构                                           版本                                             源                                                                               大小
=========================================================================================================================================================================================================================================================
正在安装:
 mysql57-community-release                                         noarch                                         el7-8                                            /mysql57-community-release-el7-8.noarch                                         8.2 k
事务概要
=========================================================================================================================================================================================================================================================
安装  1 软件包
总计:8.2 k
安装大小:8.2 k
Is this ok [y/d/N]: y
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  正在安装    : mysql57-community-release-el7-8.noarch                                                                                                                                                                                               1/1 
  验证中      : mysql57-community-release-el7-8.noarch                                                                                                                                                                                               1/1 
已安装:
  mysql57-community-release.noarch 0:el7-8                                                                                                                                                                                                               
完毕!
- 安装MySQL
 
yum install -y mysql-community-server
已加载插件:fastestmirror, product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager to register.
Loading mirror speeds from cached hostfile
mysql-connectors-community                                                                                                                                                                                                        | 2.6 kB  00:00:00     
mysql-tools-community                                                                                                                                                                                                             | 2.6 kB  00:00:00     
mysql57-community                                                                                                                                                                                                                 | 2.6 kB  00:00:00     
(1/3): mysql-connectors-community/x86_64/primary_db                                                                                                                                                                               |  68 kB  00:00:00     
(2/3): mysql-tools-community/x86_64/primary_db                                                                                                                                                                                    |  83 kB  00:00:00     
(3/3): mysql57-community/x86_64/primary_db                                                                                                                                                                                        | 247 kB  00:00:00     
正在解决依赖关系
--> 正在检查事务
---> 软件包 mysql-community-server.x86_64.0.5.7.32-1.el7 将被 安装
--> 正在处理依赖关系 mysql-community-common(x86-64) = 5.7.32-1.el7,它被软件包 mysql-community-server-5.7.32-1.el7.x86_64 需要
--> 正在处理依赖关系 mysql-community-client(x86-64) >= 5.7.9,它被软件包 mysql-community-server-5.7.32-1.el7.x86_64 需要
--> 正在检查事务
---> 软件包 mysql-community-client.x86_64.0.5.7.32-1.el7 将被 安装
--> 正在处理依赖关系 mysql-community-libs(x86-64) >= 5.7.9,它被软件包 mysql-community-client-5.7.32-1.el7.x86_64 需要
---> 软件包 mysql-community-common.x86_64.0.5.7.32-1.el7 将被 安装
--> 正在检查事务
---> 软件包 mariadb-libs.x86_64.1.5.5.60-1.el7_5 将被 取代
--> 正在处理依赖关系 libmysqlclient.so.18()(64bit),它被软件包 2:postfix-2.10.1-7.el7.x86_64 需要
--> 正在处理依赖关系 libmysqlclient.so.18(libmysqlclient_18)(64bit),它被软件包 2:postfix-2.10.1-7.el7.x86_64 需要
---> 软件包 mysql-community-libs.x86_64.0.5.7.32-1.el7 将被 舍弃
--> 正在检查事务
---> 软件包 mysql-community-libs-compat.x86_64.0.5.7.32-1.el7 将被 舍弃
---> 软件包 postfix.x86_64.2.2.10.1-7.el7 将被 升级
---> 软件包 postfix.x86_64.2.2.10.1-9.el7 将被 更新
--> 解决依赖关系完成
依赖关系解决
=========================================================================================================================================================================================================================================================
 Package                                                                架构                                              版本                                                        源                                                            大小
=========================================================================================================================================================================================================================================================
正在安装:
 mysql-community-libs                                                   x86_64                                            5.7.32-1.el7                                                mysql57-community                                            2.3 M
      替换  mariadb-libs.x86_64 1:5.5.60-1.el7_5
 mysql-community-libs-compat                                            x86_64                                            5.7.32-1.el7                                                mysql57-community                                            1.2 M
      替换  mariadb-libs.x86_64 1:5.5.60-1.el7_5
 mysql-community-server                                                 x86_64                                            5.7.32-1.el7                                                mysql57-community                                            173 M
为依赖而安装:
 mysql-community-client                                                 x86_64                                            5.7.32-1.el7                                                mysql57-community                                             25 M
 mysql-community-common                                                 x86_64                                            5.7.32-1.el7                                                mysql57-community                                            308 k
为依赖而更新:
 postfix                                                                x86_64                                            2:2.10.1-9.el7                                              base                                                         2.4 M
事务概要
=========================================================================================================================================================================================================================================================
安装  3 软件包 (+2 依赖软件包)
升级           ( 1 依赖软件包)
总下载量:205 M
Downloading packages:
No Presto metadata available for base
警告:/var/cache/yum/x86_64/7/mysql57-community/packages/mysql-community-common-5.7.32-1.el7.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY                                                              ]  0.0 B/s | 116 kB  --:--:-- ETA 
mysql-community-common-5.7.32-1.el7.x86_64.rpm 的公钥尚未安装
(1/6): mysql-community-common-5.7.32-1.el7.x86_64.rpm                                                                                                                                                                             | 308 kB  00:00:00     
(2/6): mysql-community-libs-5.7.32-1.el7.x86_64.rpm                                                                                                                                                                               | 2.3 MB  00:00:00     
(3/6): mysql-community-libs-compat-5.7.32-1.el7.x86_64.rpm                                                                                                                                                                        | 1.2 MB  00:00:00     
(4/6): postfix-2.10.1-9.el7.x86_64.rpm                                                                                                                                                                                            | 2.4 MB  00:00:00     
(5/6): mysql-community-client-5.7.32-1.el7.x86_64.rpm                                                                                                                                                                             |  25 MB  00:00:06     
(6/6): mysql-community-server-5.7.32-1.el7.x86_64.rpm                                                                                                                                                                             | 173 MB  00:00:33     
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
总计                                                                                                                                                                                                                     5.8 MB/s | 205 MB  00:00:35     
从 file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql 检索密钥
导入 GPG key 0x5072E1F5:
 用户ID     : "MySQL Release Engineering <mysql-build@oss.oracle.com>"
 指纹       : a4a9 4068 76fc bd3c 4567 70c8 8c71 8d3b 5072 e1f5
 软件包     : mysql57-community-release-el7-8.noarch (installed)
 来自       : /etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  正在安装    : mysql-community-common-5.7.32-1.el7.x86_64                                                                                                                                                                                           1/8 
  正在安装    : mysql-community-libs-5.7.32-1.el7.x86_64                                                                                                                                                                                             2/8 
  正在安装    : mysql-community-client-5.7.32-1.el7.x86_64                                                                                                                                                                                           3/8 
  正在安装    : mysql-community-libs-compat-5.7.32-1.el7.x86_64                                                                                                                                                                                      4/8 
  正在更新    : 2:postfix-2.10.1-9.el7.x86_64                                                                                                                                                                                                        5/8 
  正在安装    : mysql-community-server-5.7.32-1.el7.x86_64                                                                                                                                                                                           6/8 
warning: /etc/my.cnf created as /etc/my.cnf.rpmnew
  清理        : 2:postfix-2.10.1-7.el7.x86_64                                                                                                                                                                                                        7/8 
  正在删除    : 1:mariadb-libs-5.5.60-1.el7_5.x86_64                                                                                                                                                                                                 8/8 
warning: file /etc/ld.so.conf.d/mariadb-x86_64.conf: remove failed: No such file or directory
  验证中      : 2:postfix-2.10.1-9.el7.x86_64                                                                                                                                                                                                        1/8 
  验证中      : mysql-community-client-5.7.32-1.el7.x86_64                                                                                                                                                                                           2/8 
  验证中      : mysql-community-libs-compat-5.7.32-1.el7.x86_64                                                                                                                                                                                      3/8 
  验证中      : mysql-community-libs-5.7.32-1.el7.x86_64                                                                                                                                                                                             4/8 
  验证中      : mysql-community-server-5.7.32-1.el7.x86_64                                                                                                                                                                                           5/8 
  验证中      : mysql-community-common-5.7.32-1.el7.x86_64                                                                                                                                                                                           6/8 
  验证中      : 2:postfix-2.10.1-7.el7.x86_64                                                                                                                                                                                                        7/8 
  验证中      : 1:mariadb-libs-5.5.60-1.el7_5.x86_64                                                                                                                                                                                                 8/8 
已安装:
  mysql-community-libs.x86_64 0:5.7.32-1.el7                                     mysql-community-libs-compat.x86_64 0:5.7.32-1.el7                                     mysql-community-server.x86_64 0:5.7.32-1.el7                                    
作为依赖被安装:
  mysql-community-client.x86_64 0:5.7.32-1.el7                                                                                mysql-community-common.x86_64 0:5.7.32-1.el7                                                                               
作为依赖被升级:
  postfix.x86_64 2:2.10.1-9.el7                                                                                                                                                                                                                          
替代:
  mariadb-libs.x86_64 1:5.5.60-1.el7_5                                                                                                                                                                                                                   
完毕!
- 启动MySQL
 
systemctl start mysqld.service
- 查看MySQL运行状态
 
[root@iZbp1e21ya1elf3wyej5uaZ ~]# systemctl status mysqld.service
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since 一 2020-12-14 09:56:10 CST; 6s ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 6640 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
  Process: 5970 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
 Main PID: 6644 (mysqld)
    Tasks: 27
   Memory: 328.8M
   CGroup: /system.slice/mysqld.service
           └─6644 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
12月 14 09:55:57 iZbp1e21ya1elf3wyej5uaZ systemd[1]: Starting MySQL Server...
12月 14 09:56:10 iZbp1e21ya1elf3wyej5uaZ systemd[1]: Started MySQL Server.
由于MySQL5.7之后首次登陆MySQL都是需要使用密码的,这个密码是随机生成的,但是往往我们需要重新设置密码,一般有两种方式
- 方式一:通过下面的命令查看生成的临时密码
 
cat /var/log/mysqld.log|grep 'A temporary password'
有的时候上面的命令不能得到密码,则需要方式二
- 方式二:修改MySQL配置文件my.cnf,在 [mysqld] 小节下添加一行:skip-grant-tables=1,这一行配置让 mysqld 启动时不对密码进行验证
 
vim /etc/my.cnf
[client]
port = 3306
socket = /tmp/mysql.sock
[mysql]
prompt="MySQL [\d]> "
no-auto-rehash
[mysqld]
skip-grant-tables=1   
port = 3306
socket = /tmp/mysql.sock
basedir = /usr/local/mysql
datadir = /data/mysql
pid-file = /data/mysql/mysql.pid
user = mysql
bind-address = 0.0.0.0
server-id = 1
init-connect = 'SET NAMES utf8mb4'
character-set-server = utf8mb4
skip-name-resolve
#skip-networking
back_log = 300
max_connections = 1263
max_connect_errors = 6000
open_files_limit = 65535
table_open_cache = 1024
max_allowed_packet = 500M
binlog_cache_size = 1M
max_heap_table_size = 8M
tmp_table_size = 128M
read_buffer_size = 2M
read_rnd_buffer_size = 8M
sort_buffer_size = 8M
join_buffer_size = 8M
key_buffer_size = 256M
thread_cache_size = 64
query_cache_type = 1
query_cache_size = 64M
query_cache_limit = 2M
ft_min_word_len = 4
log_bin = mysql-bin
binlog_format = mixed
expire_logs_days = 7
log_error = /data/mysql/mysql-error.log
slow_query_log = 1
long_query_time = 1
slow_query_log_file = /data/mysql/mysql-slow.log
performance_schema = 0
explicit_defaults_for_timestamp
#lower_case_table_names = 1
skip-external-locking
default_storage_engine = InnoDB
#default-storage-engine = MyISAM
innodb_file_per_table = 1
innodb_open_files = 500
innodb_buffer_pool_size = 1024M
innodb_write_io_threads = 4
innodb_read_io_threads = 4
innodb_thread_concurrency = 0
innodb_purge_threads = 1
innodb_flush_log_at_trx_commit = 2
innodb_log_buffer_size = 2M
innodb_log_file_size = 32M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 90
innodb_lock_wait_timeout = 120
bulk_insert_buffer_size = 8M
myisam_sort_buffer_size = 64M
myisam_max_sort_file_size = 10G
myisam_repair_threads = 1
interactive_timeout = 28800
wait_timeout = 28800
[mysqldump]
quick
max_allowed_packet = 500M
[myisamchk]
key_buffer_size = 256M
sort_buffer_size = 8M
read_buffer = 4M
write_buffer = 4M
- 重启MySQL
 
systemctl restart mysqld
systemctl status mysqld
- 登录MySQL,不用输入密码,直接按Enter键就可以了
 
mysql -uroot -p
- 切换到mysql数据库
 
use mysql;
- 设置数据库登录密码和开启远程访问
 
update user set authentication_string = password('你要设置的密码'),password_expired = 'N',password_last_changed = now() where user = 'root';  # 设置密码
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '你要设置的密码' WITH GRANT OPTION; # 开启允许所有地址以root身份登录访问
flush privileges;
注意:如果MySQL是线上的服务器,不是本地电脑上的,登录密码一定、一定、一定要设置的复杂一点,不然会出现下面的情况,你懂我意思吧!!!

image.png
- 重启MySQL
 
systemctl restart mysqld
systemctl status mysqld