1、授权远程访问,针对IP和用户、DB的 grant {privilege list} on {dbname}.* to ‘{user}’@'{ip}’ identified by ‘{pwd}’ with grant option ex: grant all privilege on *.* to ‘root’@’%’ identified by ‘***’ with grant option; — %代表所有IP grant all privileges on *.* to ‘exam’@’192.168.0.%’ identified by ‘***’ with grant option; — 指定IP段被授权所有权限,用指定账户/密码 注意事项: mysql.user表中,如果没有localhost/127.0.0.1这样的记录(默认会产生的),则通过ssh的连接,也会失败,如果是自己机器上的库,因为默认是localhost的
二、DDL语句(数据定义语句)
1 、create user {usrname} identified by ‘{pwd}’;2、create database {dbname} character set ‘utf8’ collate ‘utf8_general_ci’;
三、DML语句(数据操作语句)
show databases //列出 mysql server 数据库show tables [from db_name] //列出数据库数据表show table status [from db_name] //列出数据表及表状态信息desc tb_name //列出数据表信息show full columns/fields from tbl_name //列出表字段完整属性show index from tbl_name [from db_name] //列出表索引show status //列出 db server 状态show variables //列出 mysql 系统环境变量show processlist //列出DB的执行进程show grants for user //列出某用户权限show index from db.tbname // 查看表的索引show (full) processlist // 查看会话进程,有full显示全部,无full显示前100kill id // 杀死某个会话, id可通过show processlist看到 两张表间的批量更新(不同于sqlserver、oracle等,因为mysql的update中不允许有from语句)update moa_ios_devicetoken m1, moa_ios_devicetoken m2 set m1.device_token=m2.device_token where m1.user_id=m2.user_id and m1.line_id in (1,2,4) and m2.line_id in (5,6,8); Mysql导出表结构及表数据 mysqldump用法:(mysqldump用法前提,必须有DB,且use DB)
修改表字段的语句:1、增加字段: alter table tbname add columnName 字段类型 [是否允许非空]2、改字段名:alter table tbname change oldColumnName newColumnName 字段类型 [是否允许非空]3、改字段类型:alter table tbname modify columnName 字段类型 [是否允许非空];4、删除字段:alter table tbname drop column columnName;5、修改字段注释:alter table tbname modify column columnName 字段类型 default defvalue comment ‘字段注释’;mysql启动停止: service mysql start mysqladmin -uroot -ppwd shutdownmysql连接授权:grant all privileges on im.* to root@’%’ identified by ‘123456’ — im是我自己的库,%代表任何连接常用操作集锦:1、删除重复记录的方法: a 获取重复记录,插入到临时表中。 create table user_info_a select recordid,name,userid,min(recordid) as mid from user_info group by userid having count(1) > 1; b 级联表删除记录 delete user_info from user_info,user_info_a where user_info.userid=user_info_a.userid and user_info.recordid>user_info_a.mid; c drop table user_info_a 2、导入excel到mysql中的简易方法(这里拿人员举例) a 待导入数据 b 按表结构构造后数据 E列就是构造出来的SQL, 然后可直接copy到txt中,并保存成.sql文件,通过source 导入到库中。 E列写法: =”insert into user_info(recordid,companyid,department,app_sys_ids,userjid,userid,name,creator,createdate) values(default,’ZTE’,114,’1,2′,”&”‘”&TRIM(A1052)&”@moaservice.zte.com.cn’,”&C1052&”,”&D1052&”,’admin’,now());”生成的SQL,以第一行为例: insert into user_info(recordid,companyid,department,app_sys_ids,userjid,userid,name,creator,createdate) values(default,’ZTE’,114,’1,2′,’[email protected]’,’10009240′,’余昆’,’admin’,now()); 四、注释语句mysql下支持3种注释,#,/**/,– , 最后一个– 必须后加一个空格,否则无效,这个不同于oracle,使用时应注意。 五、丢失root密码时,如何重置密码?mysqld_safe –skip-grant-tables& mysql -u root mysql
mysql> UPDATE user SET password=PASSWORD(“new password”) WHERE user=’root’;