centos7安装mysql5.7
mysql安装包:
链接:https://pan.baidu.com/s/1FrndAHB_9hPTvZvjgs96Ww
提取码:rycs
1.卸载CentOS7系统自带的mariadb
查看是否存在mariadb
[root@demo software]# rpm -qa | grep mariadb
mariadb-libs-5.5.52-1.el7.x86_64
删除mariadb
[root@demo software]# rpm -e --nodeps mariadb-libs-5.5.52-1.el7.x86_64
[root@demo software]# rpm -qa | grep mariadb
[root@demo software]#
2.解压mysql-5.7.28-1.el7.x86_64.rpm-bundle.tar
[root@demo mysql_package]# tar xvf mysql-5.7.28-1.el7.x86_64.rpm-bundle.tar
mysql-community-embedded-5.7.28-1.el7.x86_64.rpm
mysql-community-libs-compat-5.7.28-1.el7.x86_64.rpm
mysql-community-devel-5.7.28-1.el7.x86_64.rpm
mysql-community-embedded-compat-5.7.28-1.el7.x86_64.rpm
mysql-community-libs-5.7.28-1.el7.x86_64.rpm
mysql-community-test-5.7.28-1.el7.x86_64.rpm
mysql-community-common-5.7.28-1.el7.x86_64.rpm
mysql-community-embedded-devel-5.7.28-1.el7.x86_64.rpm
mysql-community-client-5.7.28-1.el7.x86_64.rpm
mysql-community-server-5.7.28-1.el7.x86_64.rpm
3.依次解压以下rpm包
[root@demo mysql_package]# rpm -ivh mysql-community-common-5.7.28-1.el7.x86_64.rpm
warning: mysql-community-common-5.7.28-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing... ################################# [100%]
Updating / installing...
1:mysql-community-common-5.7.28-1.e################################# [100%]
[root@demo mysql_package]# rpm -ivh mysql-community-libs-5.7.28-1.el7.x86_64.rpm
warning: mysql-community-libs-5.7.28-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing... ################################# [100%]
Updating / installing...
1:mysql-community-libs-5.7.28-1.el7################################# [100%]
[root@demo mysql_package]# rpm -ivh mysql-community-client-5.7.28-1.el7.x86_64.rpm
warning: mysql-community-client-5.7.28-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing... ################################# [100%]
Updating / installing...
1:mysql-community-client-5.7.28-1.e################################# [100%]
[root@demo mysql_package]# rpm -ivh mysql-community-devel-5.7.28-1.el7.x86_64.rpm
warning: mysql-community-devel-5.7.28-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing... ################################# [100%]
Updating / installing...
1:mysql-community-devel-5.7.28-1.el################################# [100%]
[root@demo mysql_package]# rpm -ivh mysql-community-server-5.7.28-1.el7.x86_64.rpm
warning: mysql-community-server-5.7.28-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing... ################################# [100%]
Updating / installing...
1:mysql-community-server-5.7.28-1.e################################# [100%]
注意:安装过程可能遇到以下问题【没问题则跳过这一步】
1).缺少libaio > # rpm -ivh mysql-community-server-5.7.17-1.el7.x86_64.rpm > > 警告:mysql-community-server-5.7.17-1.el7.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY > 错误:依赖检测失败: > libaio.so.1()(64bit) 被 mysql-community-server-5.7.17-1.el7.x86_64 需要 > libaio.so.1(LIBAIO_0.1)(64bit) 被 mysql-community-server-5.7.17-1.el7.x86_64 需要 > libaio.so.1(LIBAIO_0.4)(64bit) 被 mysql-community-server-5.7.17-1.el7.x86_64 需要 > net-tools 被 mysql-community-server-5.7.17-1.el7.x86_64 需要 > 解决办法: > yum install libaio 2)缺少net-tools > rpm -ivh mysql-community-server-5.7.17-1.el7.x86_64.rpm > 警告:mysql-community-server-5.7.17-1.el7.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY > 错误:依赖检测失败: > net-tools 被 mysql-community-server-5.7.17-1.el7.x86_64 需要 > 解决办法: > yum install net-tools 3)缺少numactl > rpm -ivh mysql-community-server-5.7.17-1.el7.x86_64.rpm 报错:warning: mysql-community-server-5.7.9-1.el6.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY error: Failed dependencies: libnuma.so.1()(64bit) is needed by mysql-community-server-5.7.9-1.el6.x86_64 libnuma.so.1(libnuma_1.1)(64bit) is needed by mysql-community-server-5.7.9-1.el6.x86_64 libnuma.so.1(libnuma_1.2)(64bit) is needed by mysql-community-server-5.7.9-1.el6.x86_64 # 解决办法: > yum install numactl
4.初始化数据库
[root@demo mysql_package]# mysqld --initialize
# 初始化之后会在/var/log/mysqld.log 生成随机密码
[root@demo mysql_package]# cat /var/log/mysqld.log
2021-04-09T09:03:17.978601Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-04-09T09:03:18.091933Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-04-09T09:03:18.112196Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-04-09T09:03:18.166873Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 6cf9451e-9912-11eb-9de7-000c29314ed4.
2021-04-09T09:03:18.167740Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2021-04-09T09:03:18.346865Z 0 [Warning] CA certificate ca.pem is self signed.
2021-04-09T09:03:18.560889Z 1 [Note] A temporary password is generated for root@localhost: QEs44hX64j;L
5.修改数据库目录的所属用户及所属组
[root@demo lib]# chown mysql:mysql -R /var/lib/mysql
[root@demo lib]# ll
total 8
drwxr-xr-x. 2 root root 55 Apr 9 16:27 alternatives
drwx------. 3 root root 18 Apr 9 16:29 authconfig
drwxr-xr-x. 2 root root 6 Nov 7 2016 dbus
drwxr-xr-x. 2 root root 6 Apr 9 16:29 dhclient
drwxr-xr-x. 2 root root 6 Nov 12 2016 dnsmasq
drwxr-xr-x. 2 root root 6 Nov 5 2016 games
drwxr-xr-x. 2 root root 6 Nov 12 2016 initramfs
drwxr-xr-x. 2 root root 30 Apr 9 16:27 logrotate
drwx------. 2 root root 6 Apr 9 16:27 machines
drwxr-xr-x. 2 root root 37 Apr 9 16:30 misc
drwxr-x--x. 5 mysql mysql 4096 Apr 9 17:03 mysql
drwxr-x---. 2 mysql mysql 6 Sep 27 2019 mysql-files
drwxr-x---. 2 mysql mysql 6 Sep 27 2019 mysql-keyring
drwx------. 2 root root 262 Apr 9 16:30 NetworkManager
drwxr-xr-x. 2 root root 6 Nov 6 2016 os-prober
drwxr-xr-x. 2 root root 27 Apr 9 16:27 plymouth
drwxr-x---. 3 root polkitd 28 Apr 9 16:27 polkit-1
drwx------. 2 postfix root 25 Apr 9 16:30 postfix
drwxr-xr-x. 2 root root 4096 Apr 9 16:46 rpm
drwxr-xr-x. 2 root root 6 Nov 5 2016 rpm-state
drwx------. 2 root root 29 Apr 9 17:01 rsyslog
drwxr-xr-x. 4 root root 35 Apr 9 16:27 stateless
drwxr-xr-x. 4 root root 56 Apr 9 16:27 systemd
drwx------. 2 tss tss 6 Nov 21 2015 tpm
drwxr-xr-x. 6 root root 80 Apr 9 16:46 yum
6.启动mysql服务
[root@demo lib]# systemctl start mysqld.service
[root@demo lib]# systemctl status mysqld.service
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since Fri 2021-04-09 17:06:59 CST; 5s ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 11518 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
Process: 11500 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
Main PID: 11521 (mysqld)
CGroup: /system.slice/mysqld.service
└─11521 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid...
Apr 09 17:06:59 demo systemd[1]: Starting MySQL Server...
Apr 09 17:06:59 demo systemd[1]: Started MySQL Server.
7.在/var/log/mysqld.log 找到生成的随机密码,登录mysql,修改密码
[root@demo lib]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.28
Copyright (c) 2000, 2019, 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>
mysql> set password=password('123456');
Query OK, 0 rows affected, 1 warning (0.00 sec)
8.访问权限,允许远程登陆
mysql> select user,host,authentication_string from user;
+---------------+-----------+-------------------------------------------+
| user | host | authentication_string |
+---------------+-----------+-------------------------------------------+
| root | localhost | *667F407DE7C6AD07358FA38DAED7828A72014B4E |
| mysql.session | localhost | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| mysql.sys | localhost | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
+---------------+-----------+-------------------------------------------+
3 rows in set (0.00 sec)
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> select user,host,authentication_string from user;
+---------------+-----------+-------------------------------------------+
| user | host | authentication_string |
+---------------+-----------+-------------------------------------------+
| root | localhost | *667F407DE7C6AD07358FA38DAED7828A72014B4E |
| mysql.session | localhost | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| mysql.sys | localhost | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| root | % | *667F407DE7C6AD07358FA38DAED7828A72014B4E |
+---------------+-----------+-------------------------------------------+
4 rows in set (0.00 sec)
mysql>
取消无密码登录模式【注意:忘记密码情况下,采取以下操作修改密码】
#(1)注释或者删除/etc/my.cnf文件中的skip-grant-tables配置
#(2)重启mysql服务(systemctl restart mysqld.service)
#(3)重新修改root密码
mysql> update user set host='%',authentication_string=password('123456') where user='root';
Query OK, 0 rows affected, 1 warning (0.02 sec)
Rows matched: 1 Changed: 0 Warnings: 1
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
#(4)重启mysql服务(systemctl restart mysqld.service)