1.导出整个数据库(sql脚本)
#mysqldump -u 用户名 -p 数据库名 > 导出的文件名
mysqldump -u root -p db_name > test_db.sql
2.导出数据库一个表
#mysqldump -u 用户名 -p 数据库名 表名> 导出的文件名
mysqldump -u root -p test_db users> test_users.sql
3.导入sql文件(不登录到mysql)
#mysql -u 用户名 -p 数据库名 < sql文件
mysql -u root -p test_db < user.sql
4.导入sql文件(登录mysql)
#source 文件路径
mysql> source ~/test.sql
5.mysql连表查询语句:
用”` “号包裹表明和字段名还可以防止表明或字段名与关键字冲突
$sql = "SELECT `weibo`.* FROM `weibo` LEFT JOIN `account` ON `weibo`.`account_open_id` = `account`.`open_id` WHERE `posted_at` BETWEEN {$now}-`account`.`scrape_time`*60 AND {$now}-`account`.`scrape_time`*60+60";
6.添加用户并设置权限
GRANT SELECT,INSERT,UPDATE,DELETE ON <databasesName>.<tableName> TO <userName>@'<ip>' IDENTIFIED BY '<password>';
FLUSH PRIVILEGES;
eg:
# 给‘wang’这个用户赋予所有数据库的所有表SELECT,INSERT权限,并可以在任何IP地址下登录,密码为123456
GRANT SELECT,INSERT ON *.* TO wang@'%' IDENTIFIED BY '123456';
# 刷新权限
FLUSH PRIVILEGES;
7.查看数据库中具体某个用户的权限
mysql> show grants for 'cactiuser'@'%';
+-------------------------------------------------+
| Grants for cactiuser@%
+-------------------------------------------------+
| GRANT PROCESS, SUPER ON *.* TO 'cactiuser'@'%' IDENTIFIED BY PASSWORD '*DAFF917B80E3314B1ABECBA9DF8785AFD342CE89' |
| GRANT ALL PRIVILEGES ON `cacti`.* TO 'cactiuser'@'%'
+-------------------------------------------------+
2 rows in set (0.00 sec)
- MySQL联表查询按照数量排序的sql语句
SELECT `package`.* FROM `package` LEFT JOIN `package_card` ON `package`.`id` = `package_card`.`package_id` GROUP BY `package_card`.`package_id` HAVING `belong`=2 ORDER BY COUNT(*) DESC LIMIT 20;
- SQL一条语句统计记录总数及各状态数
SELECT COUNT(1) 总记录数,SUM(case when status=1 then 1 else 0 end) 正确数,SUM(case when status=0 then 1 else 0 end) 错误数 FROM table