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

在CentOS7上安装MySQL DB

几个步骤:

1  设置yum存储库

rpm -Uvh //ift.tt/2F9V4ai

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

禁用MySQL Repo文件中的所有存储库:

SED -I. 's /启用= 1 / enabled = 0 /' /etc/yum.repos.d/mysql-community.repo.

执行以下命令安装:

yum -enabledepo = mysql80-community安装mysql-community-server

3  启动MySQL服务

服务MySQLD开始

4  显示默认的根临时密码

Grep. “临时密码” /var./log/mysqld.log.

[[email protected] ~]# service mysqld start
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 root:

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.
Using existing password for root.

Estimated strength of the password: 100
Change the password for root ? ((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 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) : n

 ... skipping.


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) : n

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

 ... skipping.
All done!
[[email protected] ~]# mysql -u root -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 DB

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

1  To install it, update the package index on your server with apt:

  • sudo apt update 

2  然后安装默认包:

  • sudo apt install mysql-server

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

3  Configure MySQL

运行安全脚本:

  • sudo mysql_secure_installation

这将带您完成一系列提示,您可以在您可以在MySQL安装的安全选项进行一些更改。

4  连接和登录MySQL

要使用密码连接到MySQL为 , you will need to switch its authentication method from auth_socket to mysql_native_password. To do this, open up the MySQL prompt from your terminal:

  • sudo mysql

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

  • SELECT user,authentication_string,plugin,host FROM mysql.user;
输出
+------------------+-------------------------------------------+-----------------------+-----------+ | user | authentication_string | plugin | host | +------------------+-------------------------------------------+-----------------------+-----------+ | root | | 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)

在这个例子中,你可以看到  user does in fact authenticate using the auth_socket plugin. To configure the  account to authenticate with a password, run the following ALTER USER command. Be sure to change password to a strong password of your choosing, and note that this command will change the  您在第2步中设置的密码:

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

Then, run FLUSH PRIVILEGES which tells the server to reload the grant tables and put your new changes into effect:

  • FLUSH PRIVILEGES;

如果您启用了密码身份验证 ,如前一段所述,您需要使用不同的命令来访问MySQL shell。以下将使用常规用户权限运行MySQL客户端,您只能通过验证来获得数据库中的管理员权限:

  • mysql -u root -p

从那里开始,创建一个新用户并给它一个强密码:

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

然后,授予您的新用户适当的权限。例如,您可以将用户权限授予数据库中的所有表,以及添加,更改和删除用户权限的电源,使用此命令:

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

Note that, at this point, you do not need to run the FLUSH PRIVILEGES command again. This command is only needed when you modify the grant tables using statements like INSERTUPDATE, or DELETE. Because you created a new user, instead of modifying an existing one, 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

If MySQL isn’t running, you can start it with sudo systemctl start mysql.

For an additional check, you can try connecting to the database using the mysqladmin tool, which is a client that lets you run administrative commands. For example, this command says to connect to MySQL as  (-u root), prompt for a password (-p), and return the version.

  • sudo mysqladmin -p -u root version

您应该看到类似的输出:

输出
mysqladmin  Ver 8.42 Distrib 5.7.21, for Linux on 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. http://blog.fabiandinkins.com/2020/09/install-mysql-8-into-centos-7-and.html

经过 Jon.

发表评论