mysql的基本使用

登入mysql:

$ mysql -u dbuser1 -p 
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.6.26 MySQL Community Server (GPL)

Copyright (c) 2000, 2015, 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.

登入之后,首先要设置客户端字符集跟操作系统一致

查看系统字符集:

$ locale 
LANG=en_US.UTF-8
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=

设置客户端字符集:

mysql> set NAMES utf8;
Query OK, 0 rows affected (0.01 sec)

重设密码:

mysql> set password=password('1234');

创建 数据库:

注意创建的数据库字符集要跟操作系统的字符集一致

例:

创建一个名称为db1的数据库,字符集为utf-8

mysql> CREATE DATABASE db1 CHARACTER SET utf8;
Query OK, 1 row affected (0.10 sec)
使用数据库

例:

使用名称为db1的数据库

mysql> USE db1;
Database changed
 
 

查看已有所有数据库

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| db1                |
| mysql              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.04 sec)

创建新用户:

注意不要以root用户使用数据库,不安全。

例:

创建一个名称为dbuser1,对数据库db1有所有操作权限的用户,密码为19940308

mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> GRANT ALL ON db1.* TO dbuser1 IDENTIFIED BY '19940308';
Query OK, 0 rows affected (0.10 sec)

创建表:

例:
创建一张名称为table1的表,表中有name1列(定长),name2列(可变长),age列(int)

mysql> CREATE TABLE table1 (name1 char(100), name2 varchar(100), age int);
Query OK, 0 rows affected (0.33 sec)

查看本数据库中所有表:

mysql> show tables;
+---------------+
| Tables_in_db1 |
+---------------+
| table1        |
+---------------+
1 row in set (0.00 sec)

查看表的结构:

mysql> desc table1;
+-------+--------------+------+-----+---------+-------+
| Field | Type         | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| name1 | char(100)    | YES  |     | NULL    |       |
| name2 | varchar(100) | YES  |     | NULL    |       |
| age   | int(11)      | YES  |     | NULL    |       |
+-------+--------------+------+-----+---------+-------+
3 rows in set (0.07 sec)

插入数据:

mysql> INSERT INTO table1 ( name1, name2, age) VALUES ('张三','李四', 20);
Query OK, 1 row affected (0.06 sec)

查看表内容:

mysql> SELECT * FROM table1;
+--------+--------+------+
| name1  | name2  | age  |
+--------+--------+------+
| 张三   | 李四   |   20 |
+--------+--------+------+
1 row in set (0.00 sec)

批量执行sql语句

首先创建一个后缀名为.sql的文件(保证字符集一致)

进入数据库中后

mysql> source my.sql;

查看当前用户

mysql> select user();
mysql> select current_user();

































你可能感兴趣的:(mysql的基本使用)