MySQL 上手教程
安装通过官网选择版本下载安装。Mac 上可通过 Homebrew 方便地安装: $ brew install mysql 检查安装是否成功: $ mysql --version
mysql Ver 8.0.15 for osx10.14 on x86_64 (Homebrew) 注意到安装成功后的提示里有一些有用的信息: ==> 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
==> Analytics
install: 61,745 (30 days), 203,280 (90 days), 869,736 (365 days)
install_on_request: 58,156 (30 days), 187,881 (90 days), 795,173 (365 days)
build_error: 0 (30 days)
第一件事我们需要启动 MySQL 服务, $ brew services start mysql 另外,停止及重启的命令为:
卸载$ brew uninstall mysql 该命令并没有彻底卸载 MySQL,比如设置过的 root 密码,MySQL 相关的配置都还保留。如需彻底卸载,可参考这里 以及这里。 上手相关的资源初始可从这个上手教程开始。 数据库的连接默认安装下,MySQL 提供了 root 账户且不需要密码,所以可通过如下命令在本地快速连接, $ mysql -uroot 不过还是让我们先为其设置一个密码,操作起来比较规范也更接近于真实生产环境。运行 $ mysql_secure_installation 设置好之后来看如何连接到 MySQL 服务。连接 MySQL 的命令为: $ mysql -h host -u user -p 其中,
设置密码后便不能再缺省密码登录了, $ mysql -uroot
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO) 需要带上 $ mysql -h localhost -u root -p
Enter password: 本机的数据库可省略掉 host 的指定,即 成功登录后可看到欢迎信息: Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 25
Server version: 8.0.15 Homebrew
此时便进入了 MySQL 的命令行,可以和数据库进行交互了,比如重置刚才设置的 root 密码: mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password'; 输入 mysql> help; 执行 MySQL 命令行及 SQL 语句一些注意点:
以下是通过 MySQL 命令行对数据库的一些基本操作。 列出数据库通过 mysql> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.04 sec) 虽然 MySQL 文档里有说其自带了一个名为 创建数据库通过 CREATE DATABASE <database_name> 比如,创建名为 mysql> CREATE DATABASE pets;
Query OK, 1 row affected (0.09 sec) 然后通过 mysql> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| pets |
| sys |
+--------------------+
5 rows in set (0.00 sec) 删除数据库相应地,删除数据库的命令为 重命名数据库MySQL 中没有提供数据库重命名的功能,但可以间接地实现。 先使用想要的名字创建一个新的数据库,再将旧数据库中的表重命名到新数据库下。 mysql> CREATE database new_db_name;
mysql> RENAME TABLE old_db_name.table1 TO new_db_name.table1, old_db_name.table2 TO new_db_name.table2;
mysql> DROP database old_db_name; 关于表的重命名,参见下面表的部分。 数据库间的切换首先通过 mysql> USE pets; 通过 mysql> SELECT DATABASE();
+------------+
| DATABASE() |
+------------+
| pets |
+------------+
1 row in set (0.00 sec) 创建表通过 以下语句会在数据库中创建一张名为 CREATE TABLE cats
(
id INT unsigned NOT NULL AUTO_INCREMENT, # Unique ID for the record
name VARCHAR(150) NOT NULL, # Name of the cat
owner VARCHAR(150) NOT NULL, # Owner of the cat
birth DATE NOT NULL, # Birthday of the cat
PRIMARY KEY (id) # Make the id the primary key
); 其中第一列为列名,紧随其后的是该列的数据类型,然后是指定其是否可空。 上面代码可直接粘贴执行。平时自己手动输入的情况下,在进入多行模式行,如果发现前一行输入有误,那么很不幸,根据文档的描述,没有办法回到上一行进行修正。只能键入 通过 mysql> SHOW TABLES;
+----------------+
| Tables_in_pets |
+----------------+
| cats |
+----------------+
1 row in set (0.01 sec) 通过 mysql> DESCRIBE cats;
+-------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(150) | NO | | NULL | |
| owner | varchar(150) | NO | | NULL | |
| birth | date | NO | | NULL | |
+-------+------------------+------+-----+---------+----------------+
4 rows in set (0.00 sec) 修改表名如果想修改已经创建的表名,可通过 mysql> RENAME TABLE todo TO todos
Query OK, 0 rows affected (0.04 sec) 插入记录通过 INSERT INTO cats ( name, owner, birth) VALUES
( 'Sandy', 'Lennon', '2015-01-03' ),
( 'Cookie', 'Casey', '2013-11-13' ),
( 'Charlie', 'River', '2016-05-21' ); 关于各数据类型及相应值的设置可参见 Literal Values。 查询记录通过 简单情况下的使用格式为 mysql> SELECT * FROM cats;
+----+---------+--------+------------+
| id | name | owner | birth |
+----+---------+--------+------------+
| 1 | Sandy | Lennon | 2015-01-03 |
| 2 | Cookie | Casey | 2013-11-13 |
| 3 | Charlie | River | 2016-05-21 |
+----+---------+--------+------------+
3 rows in set (0.01 sec) 前面提到过语句的结束除了通过分号,还有 mysql> SELECT * FROM cats\G
*************************** 1. row ***************************
id: 1
name: Sandy
owner: Lennon
birth: 2015-01-03
*************************** 2. row ***************************
id: 2
name: Cookie
owner: Casey
birth: 2013-11-13
*************************** 3. row ***************************
id: 3
name: Charlie
owner: River
birth: 2016-05-21
3 rows in set (0.00 sec) 注意使用 mysql> SELECT * FROM cats\G;
...
通过添加 mysql> SELECT name FROM cats WHERE owner = 'Casey';
+--------+
| name |
+--------+
| Cookie |
+--------+
1 row in set (0.00 sec) 更新记录通过 mysql> SELECT * FROM cats;
+----+---------+--------+------------+
| id | name | owner | birth |
+----+---------+--------+------------+
| 1 | Sandy | Lennon | 2015-01-03 |
| 3 | Charlie | River | 2016-05-21 |
+----+---------+--------+------------+
2 rows in set (0.01 sec)
上面更新时,新的 删除记录通过 mysql> DELETE FROM cats WHERE name='Cookie';
Query OK, 1 row affected (0.00 sec)
清空表线下测试时,会造成大量测试数据。可通过 TRUNCATE [TABLE] tbl_name 清空前需谨慎,你应该不会想要在生产环境随便给到这个权限。 列的添加通过 mysql> ALTER TABLE cats ADD gender CHAR(1) AFTER name;
Query OK, 0 rows affected (0.22 sec)
Records: 0 Duplicates: 0 Warnings: 0
创建表时,除了通过设置 mysql> ALTER TABLE cats ADD gender CHAR(1) DEFAULT 'M' AFTER name; 前面通过 mysql> SHOW CREATE TABLE cats\G
*************************** 1. row ***************************
Table: cats
Create Table: CREATE TABLE `cats` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(150) COLLATE utf8mb4_general_ci NOT NULL,
`gender` char(1) COLLATE utf8mb4_general_ci DEFAULT NULL,
`owner` varchar(150) COLLATE utf8mb4_general_ci NOT NULL,
`birth` date NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci
1 row in set (0.00 sec) 关键是该语句的输出可直接用来在其他地方执行,然后创建出相同的表,比如上线的时候在线上环境执行。 列的删除通过 mysql> DESCRIBE cats;
+--------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(150) | NO | | NULL | |
| gender | char(1) | YES | | NULL | |
| owner | varchar(150) | NO | | NULL | |
| birth | date | NO | | NULL | |
+--------+------------------+------+-----+---------+----------------+
5 rows in set (0.01 sec)
账户及权限默认的 root 账户权限很高,一般不用于日常的作业。 可根据需要创建不同权限的账户来进行管理和作业。
账户创建与分配的权限以下脚本展示了账户的创建并为其分配相应权限。 CREATE USER 'finley'@'localhost'
IDENTIFIED BY 'password';
该脚本分别为 其中 CREATE USER 'admin'@'localhost'
IDENTIFIED BY 'password';
上面的脚本创建了名为 CREATE USER 'dummy'@'localhost'; 上面的脚本创建名为 上面创建的账户都是全局作用域,即没有限制数据库。通过为账户指定相应的数据库可限制账户只对某些库有相应操作权限,达到更加精细的设置。 CREATE USER 'custom'@'localhost'
IDENTIFIED BY 'password';
GRANT ALL
ON bankaccount.*
TO 'custom'@'localhost'; 上面脚本创建的 CREATE USER 'custom'@'host47.example.com'
IDENTIFIED BY 'password';
GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP
ON expenses.*
TO 'custom'@'host47.example.com'; 上面脚本对 CREATE USER 'custom'@'%.example.com'
IDENTIFIED BY 'password';
GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP
ON customer.addresses
TO 'custom'@'%.example.com'; 上面脚本创建 权限的查看通过 mysql> SHOW GRANTS FOR 'wayou'@'localhost';
+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Grants for wayou@localhost |
+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, RELOAD, SHUTDOWN, PROCESS, FILE, REFERENCES, INDEX, ALTER, SHOW DATABASES, SUPER, CREATE TEMPORARY TABLES, LOCK TABLES, EXECUTE, REPLICATION SLAVE, REPLICATION CLIENT, CREATE VIEW, SHOW VIEW, CREATE ROUTINE, ALTER ROUTINE, CREATE USER, EVENT, TRIGGER, CREATE TABLESPACE, CREATE ROLE, DROP ROLE ON *.* TO `wayou`@`localhost` WITH GRANT OPTION |
| GRANT APPLICATION_PASSWORD_ADMIN,BACKUP_ADMIN,BINLOG_ADMIN,BINLOG_ENCRYPTION_ADMIN,CONNECTION_ADMIN,ENCRYPTION_KEY_ADMIN,GROUP_REPLICATION_ADMIN,PERSIST_RO_VARIABLES_ADMIN,REPLICATION_SLAVE_ADMIN,RESOURCE_GROUP_ADMIN,RESOURCE_GROUP_USER,ROLE_ADMIN,SERVICE_CONNECTION_ADMIN,SESSION_VARIABLES_ADMIN,SET_USER_ID,SYSTEM_VARIABLES_ADMIN,XA_RECOVER_ADMIN ON *.* TO `wayou`@`localhost` WITH GRANT OPTION |
+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec) 通过 mysql> SHOW CREATE USER 'wayou'@'localhost';
+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| CREATE USER for wayou@localhost |
+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| CREATE USER 'wayou'@'localhost' IDENTIFIED WITH 'caching_sha2_password' AS 'xxx' REQUIRE NONE PASSWORD EXPIRE DEFAULT ACCOUNT UNLOCK PASSWORD HISTORY DEFAULT PASSWORD REUSE INTERVAL DEFAULT PASSWORD REQUIRE CURRENT DEFAULT |
+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec) 权限的回收与账户的删除通过 回收所有全局权限的示例: REVOKE ALL
ON *.*
FROM 'finley'@'%.example.com';
回收数据库作用域权限的示例: REVOKE CREATE,DROP
ON expenses.*
FROM 'custom'@'host47.example.com'; 回收表作用域权限的示例: REVOKE INSERT,UPDATE,DELETE
ON customer.addresses
FROM 'custom'@'%.example.com'; 同样,可通过 最后是账户的删除,通过 DROP USER 'finley'@'localhost'; 查看用户列表通过查询内置的 mysql> SELECT User FROM mysql.user;
+------------------+
| user |
+------------------+
| mysql.infoschema |
| mysql.session |
| mysql.sys |
| root |
| wayou |
+------------------+
5 rows in set (0.00 sec) 相关资源
|