mysql数据库总结

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
云数据库 RDS MySQL Serverless,价值2615元额度,1个月
简介:

1.[root@localhost ~]# yum -y install mysql mysql-server //利用yum在线安装mysql数据库


2.[root@localhost ~]# chkconfig mysqld on //设置开机启动mysqld服务


3.[root@localhost ~]# service mysqld start //启动mysqld服务


4.[root@localhost ~]# mysqladmin -uroot password 123456 //设置mysql密码为123456


5.[root@localhost ~]# mysql -uroot -p123456 //进入mysql数据库最后别带p123456,如果别人利用history就知道密码是什么,最好直接p不回显输入密码安全,为了方便请输入history -c参数清理掉history记录。

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.0.95 Source distribution

Copyright (c) 2000, 2011, 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> help //学会看help呵呵

For information about MySQL products and services, visit:
   http://www.mysql.com/
For developer information, including the MySQL Reference Manual, visit:
   http://dev.mysql.com/
To buy MySQL Enterprise support, training, or other products, visit:
   https://shop.mysql.com/

List of all MySQL commands:
Note that all text commands must be first on line and end with ';'
?         (\?) Synonym for `help'.
clear     (\c) Clear the current input statement.
connect   (\r) Reconnect to the server. Optional arguments are db and host.
delimiter (\d) Set statement delimiter.
edit      (\e) Edit command with $EDITOR.
ego       (\G) Send command to mysql server, display result vertically.
exit      (\q) Exit mysql. Same as quit.
go        (\g) Send command to mysql server.
help      (\h) Display this help.
nopager   (\n) Disable pager, print to stdout.
notee     (\t) Don't write into outfile.
pager     (\P) Set PAGER [to_pager]. Print the query results via PAGER.
print     (\p) Print current command.
prompt    (\R) Change your mysql prompt.
quit      (\q) Quit mysql.
rehash    (\#) Rebuild completion hash.
source    (\.) Execute an SQL script file. Takes a file name as an argument.
status    (\s) Get status information from the server.
system    (\!) Execute a system shell command.
tee       (\T) Set outfile [to_outfile]. Append everything into given outfile.
use       (\u) Use another database. Takes database name as argument.
charset   (\C) Switch to another charset. Might be needed for processing binlog with multi-byte charsets.
warnings  (\W) Show warnings after every statement.
nowarning (\w) Don't show warnings after every statement.

For server side help, type 'help contents'


6.mysql> status //查看mysql连接状态和其它参数。
--------------
mysql  Ver 14.12 Distrib 5.0.95, for redhat-linux-gnu (i686) using readline 5.1

Connection id:          11
Current database:
Current user:           root@localhost
SSL:                    Not in use
Current pager:          stdout
Using outfile:          ''
Using delimiter:        ;
Server version:         5.0.95 Source distribution
Protocol version:       10
Connection:             Localhost via UNIX socket
Server characterset:    latin1
Db     characterset:    latin1
Client characterset:    latin1
Conn.  characterset:    latin1
UNIX socket:            /var/lib/mysql/mysql.sock
Uptime:                 10 min 55 sec

Threads: 1  Questions: 26  Slow queries: 0  Opens: 12  Flush tables: 1  Open tables: 6  Queries per second avg: 0.040


7.mysql> show databases; //查看数据库默认有3个数据库
+--------------------+
| Database           |
+--------------------+
| information_schema | 
| mysql              | 
| test               | 
+--------------------+
3 rows in set (0.03 sec)


8.mysql> create database zh888; //创建zh888数据库
Query OK, 1 row affected (0.00 sec)
mysql> show databases;//在次查看数据库多出一个zh888的数据库
+--------------------+
| Database           |
+--------------------+
| information_schema | 
| mysql              | 
| test               | 
| zh888              | 
+--------------------+
4 rows in set (0.00 sec)

9.mysql> use zh888; //进入zh888数据库
Database changed

mysql> show tables; //查看zh888中没有表
Empty set (0.00 sec)

10.mysql> create table zh888table (name varchar(20),sex char(1));
Query OK, 0 rows affected (0.00 sec) //创建zh888table表


11.mysql> show tables; //查看zh888数据的表为zh888table
+-----------------+
| Tables_in_zh888 |
+-----------------+
| zh888table      | 
+-----------------+
1 row in set (0.00 sec)


12.mysql> desc zh888table; //查看zh888table表的结构
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| name  | varchar(20) | YES  |     | NULL    |       | 
| sex   | char(1)     | YES  |     | NULL    |       | 
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)


13.mysql> insert into zh888table values("zh","b"); //往zh888table表中添加记录
Query OK, 1 row affected (0.00 sec)

 

14.mysql> select *from zh888table; //查看zh888table表所有数据
+------+------+
| name | sex  |
+------+------+
| zh   | b    | 
+------+------+
1 row in set (0.00 sec)

15.mysql> update zh888table set name="zhh" where sex="b"; //更新数据
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

16.mysql> select *from zh888table; //查看zh888table数据
+------+------+
| name | sex  |
+------+------+
| zhh  | b    | 
+------+------+
1 row in set (0.00 sec)

 

17.mysql> update zh888table set sex="g" where name="zhh";//更新数据
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select *from zh888table;//查看zh888table数据。
+------+------+
| name | sex  |
+------+------+
| zhh  | g    | 
+------+------+
1 row in set (0.00 sec)


18.mysql> delete from zh888table; //清空zh888table数据
Query OK, 1 row affected (0.00 sec)

mysql> select *from zh888table; //查看数据为空
Empty set (0.00 sec)


19.mysql> drop table zh888table; //删除zh888table表
Query OK, 0 rows affected (0.00 sec)

20.mysql> drop database zh888; //删除zh888数据库
Query OK, 0 rows affected (0.04 sec)

mysql> use mysql; //进入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


格式:grant 权限 on 数据库.* to 用户名@登录主机 identified by "密码"; 


21.mysql> insert into mysql.user(host,user,password)values("localhost","zh888",password("123456"));
Query OK, 1 row affected, 3 warnings (0.00 sec) //此处的"localhost",是指该用户只能在本地登录,不能在另外一台机器上远程登录。如果想远程登录的话,将"localhost"改为"%",表示在任何一台电脑上都可以登录,也可以指定某台机器可以远程登录。

22.mysql>create database zh888; //创建数据库zh888

23.mysql>grant all privileges on zh888.* to zh888@localhost identified by '123456'; //授权zh888用户拥有zh888数据库所有权限(某个数据库的所有权限)

24.mysql>flush privileges;//刷新系统权限表


授权zh888用户拥有所有数据库的某些权限
25.mysql>grant select,delete,update,create,drop on *.* to zh888@"%" identified by "123456"; 
//zh888用户对所有数据库都有select,delete,update,create,drop 权限。
@"%" 表示对所有非本地主机授权,不包括localhost。(localhost地址设为127.0.0.1,如果设为真实的本地地址,不知道是否可以,没有验证。)对localhost授权:加上一句grant all privileges on  zh888.* to zh888@localhost identified by '123456';即可。

26.部分权限给一个用户
mysql>grant select,update on zh888.* to zh888@localhost identified by '123456';

mysql>flush privileges; //刷新系统权限表

27.删除用户

mysql>Delete FROM user Where User='zh888' and Host='localhost';

mysql>flush privileges;

mysql>drop database zh888; //删除用户的数据库

删除账户及权限:drop user 用户名@'%';

        drop user 用户名@ localhost;

28.修改指定用户密码

mysql -u root -p

mysql>update mysql.user set password=password('新密码') where User="zh888" and Host="localhost";

mysql>flush privileges;



本文转自zh888 51CTO博客,原文链接:http://blog.51cto.com/zh888/943328,如需转载请自行联系原作者
 

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
7月前
|
关系型数据库 MySQL 数据库
|
7月前
|
SQL 存储 关系型数据库
|
9月前
|
SQL 关系型数据库 MySQL
MYSQL数据库7
MYSQL数据库7
33 0
|
9月前
|
关系型数据库 MySQL 数据库
MySQL数据库(二)下
MySQL数据库(二)下
|
7月前
|
存储 关系型数据库 MySQL
|
7月前
|
存储 关系型数据库 MySQL
|
8月前
|
关系型数据库 MySQL 数据库
mysql数据库介绍
mysql数据介绍
33 1
|
9月前
|
SQL 关系型数据库 MySQL
MYSQL数据库5
MYSQL数据库5
30 0
|
9月前
|
SQL 存储 关系型数据库
MYSQL数据库1
MYSQL数据库1
48 0
|
9月前
|
SQL 存储 关系型数据库
MySQL数据库(四)
MySQL数据库(四)