这两天,接连遇到数据库。在脑子里转了一遍,好像现在仅仅会表的增删改查。。
当时没有好好学,现在来补补。以前多少学过些,所以过得比较快。
阅读书籍:《Mysql必知必会》chapter1~3。
思路:安装数据库工具+创建数据库+创建用户+授权
但是可能好些地方不合适。因为目前好多都不知道。等这本书看到 28节安全管理 的时候,回头修改。
回头我来总结这段:
安装数据库(DBMS+client)+创建数据库(创建/销毁)+创建用户(创建/销毁/用户查询/密码修改)+授权(权限查看/修改)+连接+等
注:文章的最后放了一些不相关的参考文章。那些并非这篇文章的参考文章。而是阅读1~3节中,电子版笔记中涉及到的一些文章,作为扩展阅读,列出在后。参考中的有些内容,我也并非都知道。只要深度能够畅通的阅读《Mysql必知必会》就好。
数据库的安装
数据库(database) 保存有组织的数据的容器(通常是一个文件或一组文件)。
⏰
误用导致混淆 人们通常用数据库这个术语来代表他们使用的数据库软件。这是不正确的,它是引起混淆的根源。确切地说,数据库软件应称为DBMS(数据库管理系统)。数据库是通过DBMS创建和操纵的容器。数据库可以是保存在硬设备上的文件,但也可以不是。在很大程度上说,数据库究竟是文件还是别的什么东西并不重要,因为你并不直接访问数据库;你使用的是DBMS,它替你访问数据库。
我感觉没有必要在windows上安装数据库。所以下面仅是linux(ubuntu)下安装数据库。
参考:在腾讯云 Ubuntu18.04 安装配置 MySQL 5.7(踩坑警告⚠️)
上面这篇参考文章很好。全伙了。 但当我们带着思考去阅读这篇文章的时候,发现它任然不够好。
不够好的地方,我偷懒,没有去查阅资料。😃
我下面还是赘述下。但不会提及理由。
#安装
$ sudo apt-get install mysql-server
$ sudo apt-get install mysql-client
$ systemctl status mysql
$ sudo systemctl enable mysql
客户端的使用工具,我暂时用的是命令行。图形化以前用过14天的navicat 。我去看了下收费,好贵。
命令行,先凑活着吧。
MySQL Workbench使用教程、在Ubuntu 20.04上安装MySQL Workbench
2022/2/1对这部分进行更新。
- 参考:A Quick Guide to Using the MySQL APT Repository。安装成功后,可以看到配置文件位置,error日志位置。
$ sudo apt-get install mysql-server ... done! update-alternatives: 使用 /var/lib/mecab/dic/ipadic-utf8 来在自动模式中提供 /var /lib/mecab/dic/debian (mecab-dictionary) 正在设置 mysql-server-8.0 (8.0.27-0ubuntu0.20.04.1) ... update-alternatives: 使用 /etc/mysql/mysql.cnf 来在自动模式中提供 /etc/mysql/my. cnf (my.cnf) Renaming removed key_buffer and myisam-recover options (if present) mysqld will log errors to /var/log/mysql/error.log ➜ ~ systemctl status mysql ● mysql.service - MySQL Community Server Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset:> Active: active (running) since Tue 2022-02-01 09:56:47 CST; 5min ago Main PID: 3787 (mysqld) Status: "Server is operational" Tasks: 37 (limit: 4650) Memory: 353.6M CGroup: /system.slice/mysql.service └─3787 /usr/sbin/mysqld 2月 01 09:56:45 dacao-0 systemd[1]: Starting MySQL Community Server... 2月 01 09:56:47 dacao-0 systemd[1]: Started MySQL Community Server. lines 1-12/12 (END)
- Testing the Server ,执行一些简单的测试以确保它可以令人满意地工作。
➜ ~ sudo mysqladmin version # 使用mysqladmin验证服务器是否正在运行。 mysqladmin Ver 8.0.27-0ubuntu0.20.04.1 for Linux on x86_64 ((Ubuntu)) Copyright (c) 2000, 2021, 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. Server version 8.0.27-0ubuntu0.20.04.1 Protocol version 10 Connection Localhost via UNIX socket UNIX socket /var/run/mysqld/mysqld.sock Uptime: 10 hours 3 min 18 sec Threads: 2 Questions: 4 Slow queries: 0 Opens: 117 Flush tables: 3 Open tables: 36 Queries per second avg: 0.000 ➜ ~ sudo mysqlshow # 使用mysqlshow查看存在哪些数据库。已安装数据库的列表可能会有所不同,但始终至少包括mysql和 information_schema。 +--------------------+ | Databases | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+
- Securing the Initial MySQL Account、 mysql_secure_installation — Improve MySQL Installation Security
# root用户可以免密连接数据库 ➜ ~ su - 密码: root@dacao-0:~# mysql -u root Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 22 Server version: 8.0.27-0ubuntu0.20.04.1 (Ubuntu) Copyright (c) 2000, 2021, 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> # 分配密码,可以使用下面的命令,修改root-password # mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'root-password'; # mysql_secure_installation:您可以为root帐户设置密码;您可以删除root可从本地主机外部访问的帐户;您可以删除匿名用户帐户;您可以删除test数据库 ➜ ~ sudo mysql_secure_installation Securing the MySQL server deployment. Connecting to MySQL using a blank password. VALIDATE PASSWORD COMPONENT can be used to test passwords and improve security. It checks the strength of password and allows the users to set only those passwords which are secure enough. Would you like to setup VALIDATE PASSWORD component? Press y|Y for Yes, any other key for No: Please set the password for root here. New password: Re-enter new password: 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) : y - Dropping test database... Success. - Removing privileges on test database... Success. 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!
- 不要使用mysql的root用户,创建一个新的用户。操作见下方。
- 允许远程连接mysql:How to bind MySQL server to more than one IP address?、20.5.6.2 X Plugin Options and System Variables-略,将
/etc/mysql/mysql.conf.d/mysqld.cnf
中bind-address = 127.0.0.1
注释掉。 - 应用程序可以与运行 X 插件的 MySQL 服务器实例建立逻辑会话,将将
/etc/mysql/mysql.conf.d/mysqld.cnf
中mysqlx-bind-address = 127.0.0.1
注销掉。 - DBeaver连接时报错:Public Key Retrieval is not allowed Public Key Retrieval is not allowed
数据库管理
#登录root
mysql -u root -p
#创建数据库
mysql> CREATE DAtABASE crashcourse;
#查看是否创建成功
mysql> show databases;
用户管理
#创建用户
mysql> CREATE USER 'dacao'@'%' IDENTIFIED BY '12345678';
#查看是否创建成功
mysql> show databases;
mysql> use mysql;
mysql> show tables;
mysql> desc user;
mysql> select User from user;
权限管理
2020/5/3一次修改
MySQL服务器的安全基础是: 用户应该对他们需要的数据具有适当的访问权,既不能多也不能少。
Q:比如呢?
A:
- 多数用户只需要对表进行读和写,但少数用户甚至需要能创建和删除表;
- 你可能想允许用户添加数据,但不允许他们删除数据;
- 某些用户(管理员)可能需要处理用户账号的权限,但多数用户不需要;
我记得orancle中有角色这个概念。
但是我现在看的这本《mysql必知必会》有点旧。有些知识点已经过时啦。
我暂时没去搜索。
#授权
GRANT ALL ON crashcourse.* TO 'dacao'@'%'
#既然可以授权,当然也可以撤销授权
#这里我们撤回用户对该数据库中表中元素删除的权利
REVOKE DELETE ON crashcourse.* TO 'dacao'@'%'
附录
2022/10/4 修
时间长了,忘了咋弄了,所以这里补充一个快速搭建过程。
sudo apt-get install mysql-server
# https://www.linuxmi.com/ubuntu-20-04-mysql-workbench.html
sudo apt install mysql-workbench-community
# 数据库root用户默认设置是无法远程登录。系统root用户下,可以免密登录数据库
# 使用系统root免密登录,数据库root用户;在数据库root用户下,创建一个可以远程访问的新用户
su -
mysql -u root
CREATE USER 'dacao'@'%' IDENTIFIED BY 'dacao';
# 因为我之前已经创建名为dacao的用户,所以再次创建会失败。我需要修改下该用户的密码
show databases; # 查看当前已经存在那些数据库
+--------------------+
| Database |
+--------------------+
| http_serve_demo1 |
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
use mysql # 用户信息保存在mysql这个database中,所以我们选定这个database
show tables; # 查看mysql这个database下有哪些表,不出意外用户存在这个user表
+------------------------------------------------------+
| Tables_in_mysql |
+------------------------------------------------------+
| columns_priv |
| component |
.......
| user |
+------------------------------------------------------+
# 查看user表的结构
desc user;
+--------------------------+-----------------------------------+------+-----+-----------------------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------------------+-----------------------------------+------+-----+-----------------------+-------+
| Host | char(255) | NO | PRI | | |
| User | char(32) | NO | PRI | | |
......
| max_user_connections | int unsigned | NO | | 0 | |
| plugin | char(64) | NO | | caching_sha2_password | |
| authentication_string | text | YES | | NULL | |
.....
| password_expired | enum('N','Y') | NO | | N | |
+--------------------------+-----------------------------------+------+-----+-----------------------+-------+
51 rows in set (0.00 sec)
select * from user where User='dacao'; # 查看下dacao用户的信息
# 参考mysql用户秘密修改:https://www.sjkjc.com/mysql/change-password/
ALTER USER dacao@'%' IDENTIFIED BY 'qwert';
# 数据库root再创建一个数据库
create database http_serve_demo1;
# 将创建的数据库的所有权限给新创建的用户
GRANT ALL privileges ON http_serve_demo1.* TO 'dacao'@'%';
此时,打开mysql-workbench
,修改下登录信息,则可以顺利登录。
在数据库中添加表结构。
show databases;
USE http_serve_demo1;
CREATE TABLE user(
username char(50) NULL,
passwd char(50) NULL
)ENGINE=InnoDB;
INSERT INTO user(username, passwd) VALUES('zhangsan', 'aaaaaa');
select * from user;
参考文章
在腾讯云 Ubuntu18.04 安装配置 MySQL 5.7(踩坑警告⚠️)
MySQL 5.7 INFORMATION_SCHEMA 详解