MySQL三分鐘快速入門


前言

你須要準備什麼?mysql

  • 能夠鏈接互聯網的一臺Macbook
  • Terminal終端
1、安裝
  • 執行安裝命令。
brew install mysql

Tips:brew是MacOS下的包管理工具,若命令不存在,請安裝後執行。nginx

  • 執行結果
Updating Homebrew...
==> Auto-updated Homebrew!
Updated 1 tap (homebrew/services).
No changes to formulae.

==> Downloading https://mirrors.aliyun.com/homebrew/homebrew-bottles/bottles/protobuf-3.13.0.catalina.bottle.tar.gz
==> Downloading https://mirrors.aliyun.com/homebrew/homebrew-bottles/bottles/mysql-8.0.21_1.catalina.bottle.tar.gz
==> Installing dependencies for mysql: protobuf
==> Installing mysql dependency: protobuf
==> Pouring protobuf-3.13.0.catalina.bottle.tar.gz
==> Caveats
Emacs Lisp files have been installed to:
  /usr/local/share/emacs/site-lisp/protobuf
==> Summary
?  /usr/local/Cellar/protobuf/3.13.0: 266 files, 19.8MB
==> Installing mysql
==> Pouring mysql-8.0.21_1.catalina.bottle.tar.gz
==> /usr/local/Cellar/mysql/8.0.21_1/bin/mysqld --initialize-insecure --user=yunnasheng --basedir=/usr/local/Cellar/mysql/8.0.21_1 --datadir=/usr/local/var/mysql --tmpdir=/tmp
==> Caveats
We've installed your MySQL database without a root password. To secure it run:
    mysql_secure_installation

MySQL is configured to only allow connections from localhost by default

To connect run:
    mysql -uroot

To have launchd start mysql now and restart at login:
  brew services start mysql
Or, if you don't want/need a background service you can just run:
  mysql.server start
==> Summary
?  /usr/local/Cellar/mysql/8.0.21_1: 290 files, 291.2MB
==> `brew cleanup` has not been run in 30 days, running now...
Removing: /usr/local/Cellar/gettext/0.20.1... (1,893 files, 18.4MB)
Removing: /Users/yunnasheng/Library/Caches/Homebrew/gettext--0.20.1.catalina.bottle.tar.gz... (8.3MB)
Removing: /usr/local/Cellar/openssl@1.1/1.1.1d... (7,983 files, 17.9MB)
Removing: /usr/local/Cellar/pcre/8.43... (204 files, 5.5MB)
Removing: /Users/yunnasheng/Library/Logs/Homebrew/tree... (64B)
Removing: /Users/yunnasheng/Library/Logs/Homebrew/wget... (64B)
Removing: /Users/yunnasheng/Library/Logs/Homebrew/libidn2... (64B)
Removing: /Users/yunnasheng/Library/Logs/Homebrew/libunistring... (64B)
Removing: /Users/yunnasheng/Library/Logs/Homebrew/nginx... (64B)
Removing: /Users/yunnasheng/Library/Logs/Homebrew/gettext... (64B)
Removing: /Users/yunnasheng/Library/Logs/Homebrew/openssl@1.1... (64B)
Removing: /Users/yunnasheng/Library/Logs/Homebrew/pcre... (64B)
Pruned 1 symbolic links and 3 directories from /usr/local
==> Caveats
==> protobuf
Emacs Lisp files have been installed to:
  /usr/local/share/emacs/site-lisp/protobuf
==> mysql
We've installed your MySQL database without a root password. To secure it run:
    mysql_secure_installation

MySQL is configured to only allow connections from localhost by default

To connect run:
    mysql -uroot

To have launchd start mysql now and restart at login:
  brew services start mysql
Or, if you don't want/need a background service you can just run:
  mysql.server start

1.2 啓動

  • 執行啓動MySQL命令
 mysql.server start

  • 執行結果
Starting MySQL
. SUCCESS!

1.3 配置安全策略

執行命令sql

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: y

There are three levels of password validation policy:

LOW    Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary                  file
# 選擇一個密碼級別
Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 0
Please set the password for root here.
# 設置密碼
New password:

Re-enter new password:

Estimated strength of the password: 50
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.

# 是否要刪除匿名用戶?按 y 表示贊成,其餘任意鍵表示拒絕
Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.

# 一般root用戶只從本地登陸,這樣能夠確保有人沒法猜想來自網絡的root密碼
Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

# 是否要禁止遠程root登陸?
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : no

 ... 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) : no

 ... 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!

2、快速開始

2.1 登陸MySQL

  • 輸入命令後,根據提示輸入剛纔設置的root用戶的登陸密碼
mysql -u root -p

Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.21 Homebrew

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>

2.2 查看有哪些數據庫

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

2.3 建立數據庫

  • 新建一個數據庫名爲local_test的庫,設置字符集爲utf8mb4
create database local_test character set utf8mb4;

  • 驗證數據庫是否建立成功
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| local_test         |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

2.4 建立用戶

  • 建立一個登陸名爲dev_user,密碼爲@dev6pWd,容許在任意IP登陸的用戶
mysql>create user 'dev_user'@'%' identified by '@dev6pWd';
Query OK, 0 rows affected (0.01 sec)

若是建立用戶時出現錯誤:Your password does not satisfy the current policy requirements 請先查看密碼策略後,修改密碼再建立。安全

  • 查看密碼策略
mysql> SHOW VARIABLES LIKE 'validate_password%';
+--------------------------------------+--------+
| Variable_name                        | Value  |
+--------------------------------------+--------+
| validate_password.check_user_name    | ON     |
| validate_password.dictionary_file    |        |
| validate_password.length             | 8      |
| validate_password.mixed_case_count   | 1      |
| validate_password.number_count       | 1      |
| validate_password.policy             | MEDIUM |
| validate_password.special_char_count | 1      |
+--------------------------------------+--------+
7 rows in set (0.00 sec)

擴展語法bash

  • 修改用戶 - bash rename user '用戶名'@'IP地址' to '新用戶名'@'IP地址';
  • 修改密碼 - set password for '用戶名'@'IP地址'=Password('新密碼');
  • 指定ip登陸 -( 192.168.1.開頭的local_admin用戶,密碼爲admin登陸)  create user 'local_admin'@'192.168.1.%' identified by 'admin';

2.5 用戶受權

  • 給dev_user用戶授予local_test庫的全部權限,固然grant這個命令不會給dev_user,只有root用戶纔有這個命令的權限。
grant all privileges on local_test.* to 'dev_user'@'%';
Query OK, 0 rows affected (0.01 sec)

  • 刷新受權信息
flush privileges;

2.6 建立表

  • 切換dev_user用戶
# 退出MySQL
mysql> exit
Bye
# 使用dev_user用戶登陸
mysql -u dev_user -p
# 輸入密碼
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 13
Server version: 8.0.21 Homebrew

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>

  • 切換到local_test數據庫
# 查看擁有的數據庫
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| local_test         |
+--------------------+
2 rows in set (0.00 sec)
# 使用local_test庫
mysql> use local_test;
Database changed
mysql>
# 顯示全部表
mysql> show tables;
Empty set (0.00 sec)

  • 建立表
CREATE TABLE IF NOT EXISTS `lt_dict`(
   `id` VARCHAR(64) NOT NULL ,
   `code` VARCHAR(128) NOT NULL,
   `name` VARCHAR(128) NOT NULL,
   `status` VARCHAR(12) NOT NULL,
   PRIMARY KEY ( `id` )
)ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

  • 插入數據
INSERT INTO lt_dict (id, code, name, status) VALUES ('3c3da1d4b3b94bd9b33fb340dfdb605c', '001', 'cjack-60', '0');

  • 查詢數據
mysql> select * from lt_dict;
+----------------------------------+------+----------+--------+
| id                               | code | name     | status |
+----------------------------------+------+----------+--------+
| 3c3da1d4b3b94bd9b33fb340dfdb605c | 001  | cjack-60 | 0      |
+----------------------------------+------+----------+--------+
1 row in set (0.00 sec)

恭喜你!完成了MySQL的基本入門!!網絡

相關文章
相關標籤/搜索