文档资料

导入导出

  1. # 导出库
  2. $ mysqldump -u 用户名 -p 数据库名 > 导出的文件名
  3. $ mysqlpump -u root -p dbName > /home/data/dbName.sql
  4. # 导出表
  5. $ mysqldump -u root -p dbName tableName > sqlFilePath
  6. # 导入
  7. $ mysql -u 用户名 -p 密码 数据库名 < 数据库名.sql
  8. $ mysql -u root -p root dbName < dbName.sql

允许root远程连接

  1. # 开启3306端口
  2. $ mysql> use mysql;
  3. $ mysql> select `host`, `user` from user; // 查看
  4. $ mysql> update user set host='%' where user='root' and host='localhost';
  5. $ mysql> flush privileges;
  6. $ exit;
  7. $ service mysqld restart;

windows系统修改 mysql 数据库 user 表中 root 为 %:
image.png

修改密码

  1. $ mysql> use mysql;
  2. $ mysql> update user set authentication_string=password("test") where user='root';
  3. $ mysql> flush privileges;
  4. $ exit;

若忘记密码,可在 [mysqld] 后面任意一行添加 “skip-grant-tables” 用来跳过密码验证:

  1. $ vi /etc/my.cnf
  2. # ... 添加 skip-grant-tables
  3. $ service mysqld restart;

常用知识 - 图2

Windows操作

  1. net start mysql #启动
  2. net stop mysql #关闭

中国地区SQL表

area.sql

时间戳 时间 转换

  1. select from_unixtime(1451997924) as new_time
  2. # 结果:2016-01-05 20:45:24
  3. select from_unixtime(1451997924,'%Y-%m-%d %H:%i:%s') as new_time
  4. # 结果:2016-01-05 20:45:24
  5. select unix_timestamp('2016-01-02')
  6. # 结果:1451664000

设置字段

设置字段命令示例:

  1. # irm_influencer
  2. # create: brand
  3. ALTER TABLE `irm_influencer` ADD `brand` varchar(50) NOT NULL DEFAULT '' COMMENT '所属品牌' AFTER `operator`;
  4. # irm_activity_to_influencer
  5. # update: brand,product_number
  6. # delete: belong_brand、cooperation_brand
  7. ALTER TABLE irm_activity_to_influencer MODIFY `brand` varchar(50) NOT NULL DEFAULT '' COMMENT '合作品牌';
  8. ALTER TABLE irm_activity_to_influencer MODIFY `product_number` int(11) NOT NULL DEFAULT '1';
  9. ALTER TABLE irm_activity_to_influencer DROP `belong_brand`;
  10. ALTER TABLE irm_activity_to_influencer DROP `product_number`;

自动备份

备份数据脚本示例:

  1. time=us_songmics_dev_$(date '+%Y-%m-%d-%H-%M-%S')
  2. ###################数据库配置信息#######################
  3. user=songmics
  4. passwd='Songmics!@#123'
  5. dbname=us_songmics_dev
  6. mysql_back_path=/home/data
  7. ##################执行备份#############################
  8. mysqldump -u$user -p$passwd $dbname > $mysql_back_path/$time.sql

可加入到定时任务中。

创建只读账号

  1. mysql -u root -p
  2. # 其中,百分号“%”表示用户可以从任何ip的客户端登陆,如果要限定指定的ip登陆,只需要把百分号"%"改为指定的ip即可。
  3. CREATE USER 'demo'@'%' IDENTIFIED BY 'demo123';
  4. # 授权用户只能在irm数据库中执行select语句
  5. GRANT SELECT ON irm.* TO 'demo'@'%';
  6. # 刷新权限
  7. FLUSH PRIVILEGES;