Mysql基本操作
创建表:
create table test01_02(id varchar(50) not null auto_increment primary key,
name nvarchar(40) null default "002",
age int(5)null default 444);
select创建表
create table tb_new_name select * from tb_old_name;
显示数据库信息语法
show tables [from db_name] [like wild]
show columns from tbl_name [from db_name] [like wild]
show index from tbl_name [from db_name]
show table status [from db_name] [like wild]
{describe| desc} tbl_name {col_name | wild}
有时你可能需要改变一下现有表的结构,那么alter table语句将是你的合适选择。 增加列
alter table tbl_name add col_name type
例如增加一个weight列:
alter table test01_01 add weight int;
修改列
alter table test01_01 modify weight varchar(50); #alter table member change id id int auto_increment;
删除列
alter table test01_01 drop weight;
另外一种常用方式,例如: 给列更名
alter table test01_01 change weight wei int;
给表更名
alter table test01_01 rename test01_04;
删除表
drop table [if exists] tbl_name [, tbl_name,...]
flush privileges; #刷新权限表
delete from mysql.user where host="%"; #删除mysql数据库里面的user表里host为%的记录
grant all privileges on *.* to 'chenmingxia'@'%' identified by 'shiyiwen'; #授所有的权限给这个用户
有时候会出现匹配不到这个用户的报错,这个时候我们就要先创建用户 ,然后在赋予权限了。如下
CREATE USER 'sstuser'@'localhost' IDENTIFIED BY 'sstuser';
GRANT RELOAD,LOCK TABLES,REPLICATION CLIENT ON *.* TO 'sst_user'@'localhost';
delete from mysql.user where host="%" and user="tongbu@"; #相对于上面这个多增加了一个条件
主从同步恢复
mysqldump -u chenmingxia -pshiyiwen --all-databases > mysql.sql
source /data/mysql.sql
grant replication slave on *.* to repluser@'10.80.11.204' identified by '123456';
change master to master_host='192.168.0.1',master_user='tongbu@',master_password='123456',master_log_file='binlog.000196',master_log_pos=311273201;
update mysql.user set password= password('shiyiwen') where user = 'root';
跳过主从错误
SET GLOBAL sql_slave_skip_counter =1;
备份和回复指定表
备份
mysqldump -u user -p db tab1 tab2 > db.sql
恢复
mysql -u user -p db < db.sql
记录点滴 Author:V 后续更新