基本操作
显示数据库
show databases;
选择数据库
use 数据库名;
显示数据库中的表
show tables;
显示数据表的结构
describe 表名;
显示表中记录
SELECT * FROM 表名
建库
create databse 库名;
创建指定编码的库
utf-8
CREATE DATABASE IF NOT EXISTS 库名 DEFAULT CHARSET utf8 COLLATE utf8_general_ci;
gbk(未测试)
CREATE DATABASE IF NOT EXISTS 库名 DEFAULT CHARACTER SET gbk COLLATE gbk_chinese_ci;
建表
create table 表名 (字段设定列表); mysql> create table name( -> id int auto_increment not null primary key , -> uname char(8), -> gender char(2), -> birthday date ); Query OK, 0 rows affected (0.03 sec) mysql> show tables; +------------------+ | Tables_in_userdb | +------------------+ | name | +------------------+ 1 row in set (0.00 sec) mysql> describe name; +----------+---------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------+---------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | uname | char(8) | YES | | NULL | | | gender | char(2) | YES | | NULL | | | birthday | date | YES | | NULL | | +----------+---------+------+-----+---------+----------------+ 4 rows in set (0.00 sec)
注: auto_increment 自增
primary key 主键
增加记录
insert into name(uname,gender,birthday) values('张三','男','1971-10-01');
修改记录
update name set birthday='1971-01-10' where uname='张三';
删除记录
delete from name where uname='张三';
删除表
drop table 表名
删除库
drop database 库名;
备份数据库
mysqldump -u root -p --opt 数据库名>备份名; //进入到库目录
ex:
mysqldump -u root -p linux > ~/linux.sql
gzip压缩
mysqldump -u root -p linux | gzip > ~/linux_150428.sql.gz
恢复
mysql -u root -p 数据库名<备份名; //恢复时数据库必须存在,可以为空数据库
ex:
mysql -u root -p linux < ~/linux_150401.sql
或者登陆mysql后:
source ~/linux_150401.sql;
数据库授权
格式:
grant select on 数据库.* to 用户名@登录主机 identified by "密码"
例1、增加一个用户user001密码为123456,让他可以在任何主机上登录,并对所有数据库有查询、插入、修改、删除的权限。首先用以root用户连入MySQL,然后键入以下命令:
grant select,insert,update,delete on *.* to user001@"%" identified by "123456";
例2、增加一个用户user002密码为123456,让此用户只可以在localhost上登录,也可以设置指定IP,并可以对数据库test进行查询、插入、修改、删除的操作 (localhost指本地主机,即MySQL数据库所在的那台主机)
//这样用户即使用知道user_2的密码,他也无法从网上直接访问数据库,只能通过MYSQL主机来操作test库。 //首先用以root用户连入MySQL,然后键入以下命令: grant select,insert,update,delete on test.* to user002@localhost identified by "123456";
其他操作
MySQL忘记root密码
修改MySQL的配置文件/etc/my.cnf
在[mysqld]
下添加一行
skip-grant-tables
保存配置文件后,重启MySQL服务
service mysqld restart
再次进入MySQL命令行mysql -uroot -p
,输入密码时直接回车,就会进入MySQL数据库了,这个时候按照常规流程修改root密码即可。
MySQL修改密码
use mysql; UPDATE user SET Password = password ('your_password') WHERE User = 'root';
或者:
mysqladmin -uroot -p旧密码 password 新密码
PS:-p和旧密码之间没有空格,password和新密码之间有空格
导出数据库表的数据
select * from linux.table-name into outfile "~/table-name.txt";
检查名为linux的数据库.myi数据库表是否存在问题
mysqld服务器停下来
service mysqld stop myisamchk server/mysql/data/linux/*.MYI
如果有问题,应该用-r参数来修复
myisamchk -r server/mysql/data/linux/*.MYI
显示用户选择的数据库和表
mysqlshow -uroot -p [数据库名]
MySQL添加用户、删除用户与授权
MySql中添加用户,新建数据库,用户授权,删除用户,修改密码
1.2 创建用户:
INSERT INTO mysql.user
(host,user,password,select_priv,insert_priv,update_priv,delete_priv,ssl_cipher,x509_issuer,x509_subject)
VALUES('localhost','kemo',password('6wq3Hbe2a3QZV96y'),'Y','Y','Y','Y','','','');
GRANT USAGE ON *.* TO 'user01'@'localhost' IDENTIFIED BY '123456' WITH GRANT OPTION; GRANT USAGE ON kemo.* TO kemo@localhost IDENTIFIED BY '6wq3Hbe2a3QZV96y' WITH GRANT OPTION;
这样就创建了一个名为:test 密码为:1234 的用户。
注意:此处的"localhost",是指该用户只能在本地登录,不能在另外一台机器上远程登录。如果想远程登录的话,将"localhost"改为"%",表示在任何一台电脑上都可以登录。也可以指定某台机器可以远程登录。
2.为用户授权
授权格式:grant 权限 on 数据库.* to 用户名@登录主机 identified by "密码";
首先为用户创建一个数据库(testDB):
create database testDB;
2.3 授权test用户拥有testDB数据库的所有权限(某个数据库的所有权限):
grant all privileges on testDB.* to test@localhost identified by '1234'; grant all privileges on kemo.* to kemo@localhost identified by '6wq3Hbe2a3QZV96y';
mysql>flush privileges;//刷新系统权限表
格式:grant 权限 on 数据库.* to 用户名@登录主机 identified by "密码";
如果想指定部分权限给一用户,可以这样来写:
grant select,update on testDB.* to test@localhost identified by '1234';
mysql>flush privileges; //刷新系统权限表
2.5 授权test用户拥有所有数据库的某些权限:
grant select,delete,update,create,drop on *.* to test@"%" identified by "1234";
test用户对所有数据库都有select,delete,update,create,drop 权限。
@"%" 表示对所有非本地主机授权,不包括localhost。(localhost地址设为127.0.0.1,如果设为真实的本地地址,不知道是否可以,没有验证。)
对localhost授权:加上一句grant all privileges on testDB.* to test@localhost identified by '1234';即可。
-
删除用户
mysql -u root -p
Delete FROM user Where User='test' and Host='localhost';
flush privileges;
drop database testDB; //删除用户的数据库
删除账户及权限
drop user 用户名@'%';
drop user 用户名@localhost;
-
修改指定用户密码
update mysql.user set password=password('新密码') where User="test" and Host="localhost";
flush privileges;
-
列出所有数据库
show databases;
-
切换数据库
use '数据库名';
-
列出所有表
show tables;
- 显示数据表结构
describe 表名;
- 删除数据库和数据表
drop database 数据库名;
drop table 数据表名;
备忘
MySQL5.5升级5.6后,执行grant出错:ERROR 2013 (HY000): Lost connection to MySQL server during query
MySQL5.5升级5.6后,执行grant出错:ERROR 2013 (HY000): Lost connection to MySQL server during query了,下面我们来看解决方法.
MySQL 5.5 升级到 MySQL5.6,在什么都没有更改的情况下,直接将data放置到MySQL 5.6下运行,一开始就一切正常,但在新加一个数据库后,分配权限时,就出错了:
ERROR 2013 (HY000): Lost connection to MySQL server during query
一开始以为是连接不上socket,因为同时有看到错误:
Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock
但查看之后,发现sock正常,没任何问题,然后就跑到官网下面看mysql 5.6升级指南,其中有这么一段:
After upgrading to a new version of MySQL, run mysql_upgrade (see Section 4.4.7, “mysql_upgrade — Check and Upgrade MySQL Tables”). This program checks your tables, and attempts to repair them if necessary. It also updates your grant tables to make sure that they have the current structure so that you can take advantage of any new capabilities. (Some releases of MySQL introduce changes to the structure of the grant tables to add new privileges or features.) mysql_upgrade does not upgrade the contents of the help tables. For upgrade instructions, see Section 5.1.10, “Server-Side Help”. mysql_upgrade should not be used when the server is running with --gtid-mode=ON, since it may make changes in nontransactional system tables in the mysql database, many of which are MyISAM and cannot be changed to use a different storage engine. See GTID mode and mysql_upgrade.
那就明白了,执行一下:
mysql_upgrade -u root -p
执行完后,再执行grant语句,问题解决。