这篇文章总结了在CentOS / Ubuntu上安装MySQL DB的步骤,并说明了如何生成db数据,然后将它们填充到MySQL数据库中。 

在CentOS7上安装MySQL数据库

几个步骤:

1  设置百胜仓库

rpm -Uvh //ift.tt/2F9V4ai

2  安装MySQL 8社区服务器版本

禁用mysql repo文件中的所有存储库:

sed -i ‘s / enabled = 1 / enabled = 0 /’ /etc/yum.repos.d/mysql-community.repo

执行以下命令进行安装:

yum –enablerepo = mysql80-community安装mysql-community-server

3  启动MySQL服务

服务mysqld启动

4  显示默认的根临时密码

grep “临时密码” /变种/log/mysqld.log

[[email protected] ~]# 服务mysqld启动
Redirecting to /bin/systemctl start mysqld.service
[[email protected] ~]# grep "A temporary password" /var/log/mysqld.log
2020-08-26T17:40:27.257318Z 6 [Note] [MY-010454] [Server] A temporary password is generated for [email protected]: yurRebprM7#<
[[email protected] ~]#


5  使用MySQL安全安装更改root密码


[[email protected] ~]# mysql_secure_installation

Securing the MySQL server deployment.

Enter password for user 根:

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

New password:

Re-enter new password:
The 'validate_password' component is installed 上 the server.
The subsequent steps will run with the existing configuration
of the component.
Using existing password for 根.

Estimated strength of the password: 100
Change the password for 根 ? ((Press y|Y for Yes, any other key for No) : n

 ... skipping.
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 上ly 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) : n

 ... skipping.


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

Disallow 根 login remotely? (Press y|Y for Yes, any other key for No) : n

 ... skipping.
By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended 上ly 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) : n

 ... skipping.
All done!
[[email protected] ~]# mysql -u 根 -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 8.0.21 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> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.01 sec)



在CentOS7上安装MySQL数据库

在Ubuntu 18.04上,默认情况下,APT软件包存储库中仅包含最新版本的MySQL。在撰写本文时,这是MySQL 5.7

1  要安装它,请使用以下命令更新服务器上的软件包索引 apt:

  • sudo apt update 

2  然后安装默认软件包:

  • sudo apt install mysql-server

这将安装MySQL,但不会提示您设置密码或进行任何其他配置更改。因为这会使您的MySQL安装不安全,所以接下来将解决此问题。

3  Configure MySQL

运行安全脚本:

  • sudo mysql_secure_installation

这将引导您完成一系列提示,在其中您可以对MySQL安装的安全性选项进行一些更改。

4 连接并登录MySQL

为了使用密码以以下方式连接到MySQL ,您需要将其身份验证方法从 auth_socket to mysql_native_password。为此,请从终端打开MySQL提示符:

  • sudo mysql

接下来,使用以下命令检查每个MySQL用户帐户使用的身份验证方法:

  • SELECT user,authentication_string,plugin,host FROM mysql.user;
输出量
+------------------+-------------------------------------------+-----------------------+-----------+ | user | authentication_string | plugin | host | +------------------+-------------------------------------------+-----------------------+-----------+ | 根 | | auth_socket | localhost | | mysql.session | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost | | mysql.sys | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost | | debian-sys-maint | *CC744277A401A7D25BE1CA89AFF17BF607F876FF | mysql_native_password | localhost | +------------------+-------------------------------------------+-----------------------+-----------+ 4 rows in set (0.00 sec)

在此示例中,您可以看到  用户实际上确实使用 auth_socket 插入。配置  要使用密码进行身份验证的帐户,请运行以下命令 ALTER USER 命令。一定要改变 password 输入您选择的强密码,并注意此命令将更改  您在步骤2中设置的密码:

  • ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';

然后跑 FLUSH PRIVILEGES 它告诉服务器重新加载授权表并使新的更改生效:

  • FLUSH PRIVILEGES;

如果您启用了密码验证 ,如前几段所述,您将需要使用其他命令来访问MySQL Shell。以下内容将以常规用户权限运行您的MySQL客户端,并且您将仅通过身份验证在数据库内获得管理员权限:

  • mysql -u 根 -p

从那里,创建一个新用户并为其设置一个强密码:

  • CREATE USER 'sammy'@'localhost' IDENTIFIED BY 'password';

然后,为新用户授予适当的特权。例如,您可以使用以下命令向数据库内的所有表授予用户特权,以及添加,更改和删除用户特权的能力:

  • GRANT ALL PRIVILEGES ON *.* TO 'sammy'@'localhost' WITH GRANT OPTION;

请注意,此时,您无需运行 FLUSH PRIVILEGES 再次命令。仅当使用以下语句修改授权表时才需要此命令 INSERTUPDATE, 要么 DELETE。由于您创建了一个新用户,而不是修改现有用户, FLUSH PRIVILEGES is unnecessary here.

接下来,退出MySQL shell:

  • exit

最后,让我们测试一下MySQL的安装。

5 测试MySQL安装

无论您如何安装,MySQL都应该已经开始自动运行。要对此进行测试,请检查其状态。

  • systemctl status mysql.service

您将看到类似于以下的输出:

输出量
● mysql.service - MySQL Community Server
   Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: en
   Active: active (running) since Wed 2018-04-23 21:21:25 UTC; 30min ago
 Main PID: 3754 (mysqld)
    Tasks: 28
   Memory: 142.3M
      CPU: 1.994s
   CGroup: /system.slice/mysql.service
           └─3754 /usr/sbin/mysqld

如果MySQL未运行,则可以使用以下命令启动它 sudo systemctl start mysql.

如果需要其他检查,可以尝试使用 mysqladmin 工具,它是允许您运行管理命令的客户端。例如,此命令说要以  (-u 根), prompt for a password (-p), and return the version.

  • sudo mysqladmin -p -u 根 version

您应该看到类似于以下的输出:

输出量
mysqladmin  Ver 8.42 Distrib 5.7.21, for 的Linux 上 x86_64
Copyright (c) 2000, 2018, 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.

Server version      5.7.21-1ubuntu1
Protocol version    10
Connection      Localhost via UNIX socket
UNIX socket     /var/run/mysqld/mysqld.sock
Uptime:         30 min 54 sec

Threads: 1  Questions: 12  Slow queries: 0  Opens: 115  Flush tables: 1  Open tables: 34  Queries per second avg: 0.006

这意味着MySQL已启动并正在运行。

创建一个数据库


mysql> create database sales;
Query OK, 1 row affected (0.13 sec)


mysql>
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sales              |
| sys                |
+--------------------+
5 rows in set (0.01 sec)

mysql> use sales;
Database changed
mysql>

填充数据

去 //www.generatedata.com/ 为您的数据库生成所需的数据。 

将生成的SQL语言复制/粘贴到MySQL会话中以填充数据。 

参考文献

来自Blogger //blog.fabiandinkins.com/2020/09/install-mysql-8-into-centos-7-and.html

通过 约翰

发表评论