MySQL 作为最常用的数据库之一. 总结一些常用命令便于日常使用.
一, 关于库的操作
1, 查看数据库
- MySQL> show databases;
- +--------------------+
- | Database |
- +--------------------+
- | information_schema |
- | MySQL |
- | performance_schema |
- | sys |
- | ty |
- | womenscms3 |
- +--------------------+
2, 创建数据库并设置字符编码为 utf8
- MySQL> create database test001 default character set utf8;
- Query OK, 1 row affected (0.01 sec)
3, 删库 (慎重操作)
- MySQL> drop database womenscms3;
- Query OK, 112 rows affected (3.94 sec)
4, 用户授权
- MySQL> grant select,insert,update,delete on *.* to 'root'@'%';
- Query OK, 0 rows affected (0.01 sec)
5, 查看用户权限
- MySQL> show grants for root;
- +-------------------------------------------+
- | Grants for [email protected]% |
- +-------------------------------------------+
- | GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' |
- +-------------------------------------------+
- 1 row in set (0.00 sec)
- MySQL> select * from MySQL.user where user='root'\G;
- *************************** 1. row ***************************
- Host: localhost
- User: root
- Select_priv: Y
- Insert_priv: Y
- Update_priv: Y
- Delete_priv: Y
- Create_priv: Y
- Drop_priv: Y
- Reload_priv: Y
- Shutdown_priv: Y
- Process_priv: Y
- File_priv: Y
- Grant_priv: Y
- References_priv: Y
- Index_priv: Y
- Alter_priv: Y
- Show_db_priv: Y
- Super_priv: Y
- Create_tmp_table_priv: Y
- Lock_tables_priv: Y
- Execute_priv: Y
- Repl_slave_priv: Y
- Repl_client_priv: Y
- Create_view_priv: Y
- Show_view_priv: Y
- Create_routine_priv: Y
- Alter_routine_priv: Y
- Create_user_priv: Y
- Event_priv: Y
- Trigger_priv: Y
- Create_tablespace_priv: Y
- ssl_type:
- ssl_cipher:
- x509_issuer:
- x509_subject:
- max_questions: 0
- max_updates: 0
- max_connections: 0
- max_user_connections: 0
- plugin: mysql_native_password
- authentication_string: *F846B31F10DD4389C384272E70B9BBA3AD9E1F94
- password_expired: N
- password_last_changed: 2019-06-24 18:10:54
- password_lifetime: NULL
- account_locked: N
常用命令
create database name; 创建数据库
use databasename; 选择数据库
drop database name; 直接删除数据库, 不提醒
show tables; 显示表
describe tablename; 表的详细描述
select 中加上 distinct 去除重复字段
显示当前 MySQL 版本和当前日期
select version(),current_date;
修改 MySQL 中 root 的密码:
- shell>MySQL -u root -p
- MySQL> update user set password=password("root123″) where user='root';
MySQL> flush privileges 刷新权限
MySQL>use dbname; 打开数据库
MySQL>show databases; 显示所有数据库
MySQL>show tables; 显示数据库 MySQL 中所有的表
MySQL>desc user; 显示表 MySQL 数据库中 user 表的列信息)
grant
创建一个可以从任何地方连接到服务器的一个超管账户, 必须分配一个密码
MySQL> grant all privileges on *.* to 'user_name'@'localhost' identified by 'password' ;
格式: grant select on 数据库.* to 用户名 @登录主机 identified by "密码"
删除授权:
- MySQL> revoke all privileges on *.* from [email protected]"%";
- MySQL> delete from user where user="root" and host="%";
- MySQL> flush privileges;
重命名表:
MySQL> alter table t1 rename t2;
备份:
mysqldump -hhostname -uusername -ppassword databasename> backup.sql;
恢复:
MySQL -hhostname -uusername -ppassword databasename< backup.sql;
附录
MySQL 文件分布
/etc/rc.d/init.d/mysqld:MySQL 服务器启动脚本
/usr/bin/mysqlshow: 显示数据库, 表和列信息
/usr/libexec/mysqld: 服务器的进程程序文件
/usr/libexec/mysqlmanager: 实例管理程序文件
/usr/share/doc/: 存放说明文件的目录
/usr/share/man/man 1/......: 存放手册页的目 m 录
/var/lib/MySQL/: 服务器数据库文件存储目录
/var/log/mysqld.log:MySQL 服务器的日志文件
来源: http://www.bubuko.com/infodetail-3156775.html