您的位置:首页 > 运维架构 > Linux

linux上操作mysql数据库

2016-11-13 15:40 274 查看
首先先安装mysql

ubuntu下

sudo apt-get install mysql-server

centos

yum install -y mysql-server mysql mysql-deve

sudo /etc/init.d/mysql start启动mysql,或者sudo service mysql start

netstat -lntup|grep 3306查看端口3306

grant all privileges on *.* to system@'localhost' identified by 'xxx' with grant option;创建新的用户并赋予所有权限xxx密码

flush privileges;刷新权限

mysql -u root -pxxx登录mysql

查看mysql数据库里操作命令历史

cat /root/.mysql_history

# 查看MySQL字符集设置情况

show variables like 'character_set%';

# 查看库的字符集

show create database db;

# 查看表的字符集

show create table db_tb\G

# 查询所有

show collation;

# 设置表的字符集

set tables utf8;

操作相关

#登陆mysql数据库

mysql -uroot –p

#查看有哪些库

show databases;

#删除test库

drop database test;

#使用test库

use test;

#查看有哪些表

show tables;

#查看suoning表的所有内容

select * from suoning;

#查看当前版本

select version();

#查看当前用户

select user();

#查看用户和主机列,从mysql.user里查看

select user,host from mysql.user;

#删除前为空,后为localhost的库

drop user ""@localhost;

#刷新权限

flush privileges;

#跳出数据库执行命令

system ls;

quit

数据库操作

1、查看数据库

SHOW DATABASES;

# 默认数据库:

  mysql - 用户权限相关数据

  test - 用于用户测试数据

  information_schema - MySQL本身架构相关数据

2、创建数据库

# utf-8 编码

CREATE DATABASE 数据库名称 DEFAULT CHARSET utf8 COLLATE utf8_general_ci;

# gbk 编码

CREATE DATABASE 数据库名称 DEFAULT CHARACTER SET gbk COLLATE gbk_chinese_ci;

3、使用数据库

USE db_name;

# 可以不使用分号

4、用户管理

# 创建用户

create user '用户名'@'IP地址' identified by '密码';

# 删除用户

drop user '用户名'@'IP地址';

# 修改用户

rename user '用户名'@'IP地址'; to '新用户名'@'IP地址';;

# 修改密码

set password for '用户名'@'IP地址' = Password('新密码')

PS:用户权限相关数据保存在mysql数据库的user表中,所以也可以直接对其进行操作(不建议)

# 查看当前用户

select user();

# 查看所有用户

select host,user from mysql.user;

# 人性化显示所有用户

SELECT DISTINCT CONCAT('User: ''',user,'''@''',host,''';') AS query FROM mysql.user;

# 查看用户的所有权限

show grants for 'nick'@'%';

mysql> SELECT DISTINCT CONCAT('User: ''',user,'''@''',host,''';') AS query FROM mysql.user;
+---------------------------+
| query |
+---------------------------+
| User: 'nick'@'%'; |
| User: 'root'@'localhost'; |
+---------------------------+
rows in set (0.00 sec)

mysql> select host,user from mysql.user;
+-----------+------+
| host | user |
+-----------+------+
| % | nick |
| localhost | root |
+-----------+------+
rows in set (0.00 sec)

mysql> show grants for 'nick'@'%';
+-----------------------------------------------------------------------------------------------------+
| Grants for nick@% |
+-----------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'nick'@'%' IDENTIFIED BY PASSWORD '*ECE7D02DCD7D4EF7CFE8E3B249FD1D5062A821F7' |
| GRANT ALL PRIVILEGES ON `kaoshi`.* TO 'nick'@'%' |
| GRANT ALL PRIVILEGES ON `xxxxx`.* TO 'nick'@'%' |
| GRANT ALL PRIVILEGES ON `xxxxxx`.`chouti` TO 'nick'@'%' |
+-----------------------------------------------------------------------------------------------------+
rows in set (0.00 sec)

5、授权管理

# 查看权限

show grants for '用户'@'IP地址'

# 授权

grant 权限 on 数据库.表 to '用户'@'IP地址'

# 取消权限

revoke 权限 on 数据库.表 from '用户'@'IP地址'

常用权限:

all privileges 除grant外的所有权限

select 仅查权限

select,insert 查和插入权限

usage 无访问权限

对于目标数据库以及内部其他:

数据库名.* 数据库中的所有

数据库名.表 指定数据库中的某张表

数据库名.存储过程 指定数据库中的存储过程

*.* 所有数据库

对于用户和IP:

用户名@IP地址 用户只能在改IP下才能访问

用户名@192.168.1.% 用户只能在改IP段下才能访问(通配符%表示任意)

用户名@% 用户可以再任意IP下访问(默认IP地址为%)

all privileges 除grant外的所有权限

select 仅查权限

select,insert 查和插入权限

...

usage 无访问权限

alter 使用alter table

alter routine 使用alter procedure和drop procedure

create 使用create table

create routine 使用create procedure

create temporary tables 使用create temporary tables

create user 使用create user、drop user、rename user和revoke all privileges

create view 使用create view

delete 使用delete

drop 使用drop table

execute 使用call和存储过程

file 使用select into outfile 和 load data infile

grant option 使用grant 和 revoke

index 使用index

insert 使用insert

lock tables 使用lock table

process 使用show full processlist

select 使用select

show databases 使用show databases

show view 使用show view

update 使用update

reload 使用flush

shutdown 使用mysqladmin shutdown(关闭MySQL)

super

all privileges 除grant外的所有权限

select 仅查权限

select,insert 查和插入权限

...

usage 无访问权限

alter 使用alter table

alter routine 使用alter procedure和drop procedure

create 使用create table

create routine 使用create procedure

create temporary tables 使用create temporary tables

create user 使用create user、drop user、rename user和revoke all privileges

create view 使用create view

delete 使用delete

drop 使用drop table

execute 使用call和存储过程

file 使用select into outfile 和 load data infile

grant option 使用grant 和 revoke

index 使用index

insert 使用insert

lock tables 使用lock table

process 使用show full processlist

select 使用select

show databases 使用show databases

show view 使用show view

update 使用update

reload 使用flush

shutdown 使用mysqladmin shutdown(关闭MySQL)

super
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: