Centos7 部署mysql-8.0.30-1.el7.x86_64

目录

1. MySQL官网下载最新MySQL安装包
2. 上传到Centos7并解压
3. 卸载以前安装过多MySQL(没安装过则跳过)
4. 安装MySQL
5. Mysql数据库初始化配置
6. 设置MySQL远程登录账号


MySQL官网下载最新MySQL安装包

官网下载地址:https://dev.mysql.com/downloads/mysql/

Centos7 部署mysql-8.0.30-1.el7.x86_64_第1张图片

返回目录

上传到Centos7并解压

tar -xvf  mysql-8.0.30-1.el7.x86_64.rpm-bundle.tar


返回目录

卸载以前安装过多MySQL(没安装过则跳过)

# 查看系统中是否以rpm包安装的mysql
[root@master softs]# rpm -qa | grep -i mysql
[root@master softs]#
# 如果有执行卸载mysql
rpm -e mysql-community-common.xxx

# 清空相关mysql的所有目录以及文件
[root@master softs]# whereis mysql
mysql: /usr/lib64/mysql /usr/share/mysql
# or
[root@master softs]# find / -name mysql
/usr/lib64/mysql
/usr/share/mysql

mysql: /usr/lib64/mysql /usr/share/mysql
[root@master softs]# rm /usr/lib64/mysql -rf
[root@master softs]# rm /usr/share/mysql -rf


返回目录

安装MySQL

按顺序安装

  • 必要安装(注意顺序)
  • 安装common
  • 安装lib 依赖于common,确保已经卸载mariadb
  • 依赖于libs
  • 依赖于client、common

必须安装(注意顺序)

# 必须按照顺序
rpm -ivh mysql-community-common-8.0.30-1.el7.x86_64.rpm
rpm -ivh mysql-community-client-plugins-8.0.30-1.el7.x86_64.rpm
rpm -ivh mysql-community-libs-8.0.30-1.el7.x86_64.rpm
rpm -ivh mysql-community-client-8.0.30-1.el7.x86_64.rpm
yum install net-tools -y
rpm -ivh mysql-community-icu-data-files-8.0.30-1.el7.x86_64.rpm
rpm -ivh mysql-community-server-8.0.30-1.el7.x86_64.rpm

debug

[root@master softs]# rpm -ivh mysql-community-libs-8.0.30-1.el7.x86_64.rpm
警告:mysql-community-libs-8.0.30-1.el7.x86_64.rpm: 头V4 RSA/SHA256 Signature, 密钥 ID 3a79bd29: NOKEY
错误:依赖检测失败:
        mysql-community-client-plugins = 8.0.30-1.el7 被 mysql-community-libs-8.0.30-1.el7.x86_64 需要
        mariadb-libs 被 mysql-community-libs-8.0.30-1.el7.x86_64 取代

# 解决方案一
rpm -ivh mysql-community-client-plugins = 8.0.30-1.el7
rpm -ivh mysql-community-libs-8.0.30-1.el7.x86_64

# 解决方案二
解决的方法就是在rpm 语句后面加上 --nodeps --force,安装时忽略依赖关系
[root@master softs]# rpm -ivh mysql-community-libs-8.0.30-1.el7.x86_64.rpm
警告:mysql-community-libs-8.0.30-1.el7.x86_64.rpm: 头V4 RSA/SHA256 Signature, 密钥 ID 3a79bd29: NOKEY
错误:依赖检测失败:
        mysql-community-client-plugins = 8.0.30-1.el7 被 mysql-community-libs-8.0.30-1.el7.x86_64 需要
[root@master softs]# rpm -ivh mysql-community-libs-8.0.30-1.el7.x86_64.rpm --nodeps --force
警告:mysql-community-libs-8.0.30-1.el7.x86_64.rpm: 头V4 RSA/SHA256 Signature, 密钥 ID 3a79bd29: NOKEY
准备中...                          ################################# [100%]
正在升级/安装...
   1:mysql-community-libs-8.0.30-1.el7################################# [100%]
[root@master softs]#


非必要安装(注意顺序)


rpm -ivh mysql-community-libs-compat-8.0.30-1.el7.x86_64.rpm
rpm -ivh mysql-community-embedded-compat-8.0.30-1.el7.x86_64.rpm
rpm -ivh mysql-community-devel-8.0.30-1.el7.x86_64.rpm --nodeps --force
rpm -ivh mysql-community-test-8.0.30-1.el7.x86_64.rpm --nodeps --force

返回目录

Mysql数据库初始化配置

# root账号配置

# 初始化数据库
[root@master softs]# mysqld --initialize --console
[root@master softs]# cd /var/lib/


# 目录授权
# mysql8自动创建mysql组和用户,目录/var/lib/mysql/全部授权给mysql
[root@master mysql]# ll
总用量 81344
-rw-r-----. 1 root root       56 913 12:11 auto.cnf
-rw-------. 1 root root     1676 913 12:11 ca-key.pem
-rw-r--r--. 1 root root     1112 913 12:11 ca.pem
-rw-r--r--. 1 root root     1112 913 12:11 client-cert.pem
-rw-------. 1 root root     1680 913 12:11 client-key.pem
-rw-r-----. 1 root root   196608 913 12:11 #ib_16384_0.dblwr
-rw-r-----. 1 root root  8585216 913 12:11 #ib_16384_1.dblwr
-rw-r-----. 1 root root     6111 913 12:11 ib_buffer_pool
-rw-r-----. 1 root root 12582912 913 12:11 ibdata1
drwxr-x---. 2 root root     4096 913 12:11 #innodb_redo
drwxr-x---. 2 root root        6 913 12:11 #innodb_temp
drwxr-x---. 2 root root     4096 913 12:11 mysql
-rw-r-----. 1 root root 28311552 913 12:11 mysql.ibd
drwxr-x---. 2 root root     8192 913 12:11 performance_schema
-rw-------. 1 root root     1680 913 12:11 private_key.pem
-rw-r--r--. 1 root root      452 913 12:11 public_key.pem
-rw-r--r--. 1 root root     1112 913 12:11 server-cert.pem
-rw-------. 1 root root     1680 913 12:11 server-key.pem
drwxr-x---. 2 root root       27 913 12:11 sys
-rw-r-----. 1 root root 16777216 913 12:11 undo_001
-rw-r-----. 1 root root 16777216 913 12:11 undo_002
[root@master mysql]# cd ..
[root@master lib]# ll
总用量 12
drwxr-xr-x. 2 root    root      42 913 11:52 alternatives
drwx------. 3 root    root      17 823 18:31 authconfig
drwxr-xr-x. 2 root    root       6 1120 2015 dbus
drwxr-xr-x. 2 root    root       6 913 10:25 dhclient
drwxr-xr-x. 2 root    root       6 86 2015 dnsmasq
drwxr-xr-x. 2 root    root       6 812 2015 games
drwxr-xr-x. 2 root    root       6 1120 2015 initramfs
-rw-r--r--. 1 root    root     402 913 11:41 logrotate.status
drwx------. 2 root    root       6 823 18:22 machines
drwxr-xr-x. 2 root    root       6 913 11:52 misc
drwxr-x--x. 7 mysql   mysql   4096 913 12:11 mysql
drwxr-x---. 2 mysql   mysql      6 77 06:46 mysql-files
drwxr-x---. 2 mysql   mysql      6 77 06:46 mysql-keyring
drwxr-xr-x. 2 root    root      50 913 10:25 NetworkManager
drwxr-xr-x. 2 ntp     ntp        6 623 2020 ntp
drwxr-xr-x. 2 root    root       6 610 2014 os-prober
drwxr-xr-x. 2 root    root      26 823 18:22 plymouth
drwxr-x---. 3 root    polkitd   27 823 18:22 polkit-1
drwx------. 2 postfix root      24 823 18:39 postfix
drwxr-xr-x. 2 root    root    4096 913 11:31 rpm
drwxr-xr-x. 2 root    root       6 812 2015 rpm-state
drwx------. 2 root    root      28 823 19:14 rsyslog
drwxr-xr-x. 4 root    root      33 823 18:22 stateless
drwxr-xr-x. 5 root    root      66 823 18:38 systemd
drwx------. 2 tss     tss        6 1121 2015 tpm
drwxr-xr-x. 6 root    root      75 913 11:59 yum
[root@master lib]# chown -R mysql:mysql /var/lib/mysql/
[root@master lib]# cd mysql
[root@master mysql]# ll
总用量 81344
-rw-r-----. 1 mysql mysql       56 913 12:11 auto.cnf
-rw-------. 1 mysql mysql     1676 913 12:11 ca-key.pem
-rw-r--r--. 1 mysql mysql     1112 913 12:11 ca.pem
-rw-r--r--. 1 mysql mysql     1112 913 12:11 client-cert.pem
-rw-------. 1 mysql mysql     1680 913 12:11 client-key.pem
-rw-r-----. 1 mysql mysql   196608 913 12:11 #ib_16384_0.dblwr
-rw-r-----. 1 mysql mysql  8585216 913 12:11 #ib_16384_1.dblwr
-rw-r-----. 1 mysql mysql     6111 913 12:11 ib_buffer_pool
-rw-r-----. 1 mysql mysql 12582912 913 12:11 ibdata1
drwxr-x---. 2 mysql mysql     4096 913 12:11 #innodb_redo
drwxr-x---. 2 mysql mysql        6 913 12:11 #innodb_temp
drwxr-x---. 2 mysql mysql     4096 913 12:11 mysql
-rw-r-----. 1 mysql mysql 28311552 913 12:11 mysql.ibd
drwxr-x---. 2 mysql mysql     8192 913 12:11 performance_schema
-rw-------. 1 mysql mysql     1680 913 12:11 private_key.pem
-rw-r--r--. 1 mysql mysql      452 913 12:11 public_key.pem
-rw-r--r--. 1 mysql mysql     1112 913 12:11 server-cert.pem
-rw-------. 1 mysql mysql     1680 913 12:11 server-key.pem
drwxr-x---. 2 mysql mysql       27 913 12:11 sys
-rw-r-----. 1 mysql mysql 16777216 913 12:11 undo_001
-rw-r-----. 1 mysql mysql 16777216 913 12:11 undo_002
[root@master mysql]#

# 启动服务
[root@master mysql]# systemctl start mysqld
[root@master mysql]# systemctl status mysqld
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since 二 2022-09-13 12:16:57 CST; 29s ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 41108 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
 Main PID: 41132 (mysqld)
   Status: "Server is operational"
   CGroup: /system.slice/mysqld.service
           └─41132 /usr/sbin/mysqld

913 12:16:57 master.phlh123.cn systemd[1]: Starting MySQL Server...
913 12:16:57 master.phlh123.cn systemd[1]: Started MySQL Server.
[root@master mysql]#


# 加密

# 加密debug
[root@master lib]# mysql_secure_installation

Securing the MySQL server deployment.

Enter password for user root:
Error: Access denied for user 'root'@'localhost' (using password: YES)

# 解决方案 /etc/my.cnf文件尾加skip-grant-tables
[root@master lib]# service mysqld stop
Redirecting to /bin/systemctl stop  mysqld.service
[root@master lib]# 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: inactive (dead) since 二 2022-09-13 12:28:41 CST; 14s ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 41132 ExecStart=/usr/sbin/mysqld $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
  Process: 41108 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
 Main PID: 41132 (code=exited, status=0/SUCCESS)
   Status: "Server shutdown complete"

913 12:16:57 master.phlh123.cn systemd[1]: Starting MySQL Server...
913 12:16:57 master.phlh123.cn systemd[1]: Started MySQL Server.
913 12:28:40 master.phlh123.cn systemd[1]: Stopping MySQL Server...
913 12:28:41 master.phlh123.cn systemd[1]: Stopped MySQL Server.
[root@master lib]# vi /etc/my.cnf
[root@master lib]# cat /etc/my.cnf
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/8.0/en/server-configuration-defaults.html

[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove the leading "# " to disable binary logging
# Binary logging captures changes between backups and is enabled by
# default. It's default setting is log_bin=binlog
# disable_log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
#
# Remove leading # to revert to previous value for default_authentication_plugin,
# this will increase compatibility with older clients. For background, see:
# https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_default_authentication_plugin
# default-authentication-plugin=mysql_native_password

datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
skip-grant-tables
[root@master lib]# service mysqld stop
Redirecting to /bin/systemctl stop  mysqld.service
[root@master lib]# service mysqld start
Redirecting to /bin/systemctl start  mysqld.service

# 免密登录mysql,设置mysql的root账号为空
[root@master lh]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.30 MySQL Community Server - GPL

Copyright (c) 2000, 2022, 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> use 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> select host,user,authentication_string,plugin from user;
+-----------+------------------+------------------------------------------------------------------------+-----------------------+
| host      | user             | authentication_string                                                  | plugin                |
+-----------+------------------+------------------------------------------------------------------------+-----------------------+
| localhost | mysql.infoschema | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password |
| localhost | mysql.session    | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password |
| localhost | mysql.sys        | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password |
| localhost | root             | $A$005$"%0YkRU)4cw6tFY]UYJK5AzhKh7IAiu2w6eCvUUV2OpwtvHdl5m1dtkHacC | caching_sha2_password |
+-----------+------------------+------------------------------------------------------------------------+-----------------------+
4 rows in set (0.00 sec)

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

mysql> exit
Bye
[root@master lh]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.30 MySQL Community Server - GPL

Copyright (c) 2000, 2022, 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> user;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'user' at line 1
mysql> use 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> select host,user,authentication_string,plugin from user;
+-----------+------------------+------------------------------------------------------------------------+-----------------------+
| host      | user             | authentication_string                                                  | plugin                |
+-----------+------------------+------------------------------------------------------------------------+-----------------------+
| localhost | mysql.infoschema | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password |
| localhost | mysql.session    | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password |
| localhost | mysql.sys        | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password |
| localhost | root             |                                                                        | caching_sha2_password |
+-----------+------------------+------------------------------------------------------------------------+-----------------------+
4 rows in set (0.00 sec)

mysql> exit
Bye

# 退出mysql, 删除/etc/my.cnf文件最后的 skip-grant-tables 重启mysql服务;
[root@master lh]# service mysqld stop
Redirecting to /bin/systemctl stop  mysqld.service
[root@master lh]# ls
hadooptest  linuxLearning  scripts  softs

[root@master lh]# vim /etc/my.cnf
[root@master lh]# service mysqld start
Redirecting to /bin/systemctl start  mysqld.service

[root@master lh]# vim /etc/my.cnf
[root@master lh]# service mysqld start
Redirecting to /bin/systemctl start  mysqld.service

# New password:用户自己定义一个root密码
[root@master lh]# mysql_secure_installation

Securing the MySQL server deployment.

Connecting to MySQL using a blank password.

The existing password for the user account root has expired. Please set a new password.

New password:

Re-enter new password:
The 'validate_password' component is installed on the server.
The subsequent steps will run with the existing configuration
of the component.
Please set the password for root here.

New password:
Sorry, you can't use an empty password here.

New password:

Re-enter new password:
Sorry, passwords do not match.

New password:

Re-enter new password:

Estimated strength of the password: 100
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.


Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
Success.

By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.


Remove test database and access to it? (Press y|Y for Yes, any other key for No) : n

 ... skipping.
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.

All done!
[root@master lh]#

# 设置开机mysql自启动
systemctl enable mysqld

返回目录

设置MySQL远程登录账号

# MySQL root 账号登录
mysql -u root -p

# 查看用户表
mysql> user mysql;
mysql> select host,user,authentication_string,plugin from user;

# 创建可远程连接用户
mysql> create user 'lh'@'%' identified by 'tt123456!';
mysql> grant all privileges on *.* to 'lh'@'%' with grant option;
mysql> alter user 'lh'@'%' identified with mysql_native_password by 'tt123456!';
mysql> flush privileges;

Centos7 部署mysql-8.0.30-1.el7.x86_64_第2张图片

返回目录

你可能感兴趣的:(Centos7,MySQL,mysql,数据库,服务器)