Mysql 初始化配置,使用总结
1. 添加,授权用户
mysql>CREATE USER 'monty'@'localhost' IDENTIFIED BY 'some_pass'; //创建用户
mysql>GRANT ALL PRIVILEGES ON *.* TO 'monty'@'localhost'
->WITH GRANT OPTION; //授权用户
mysql> SHOW GRANTS FOR 'admin'@'localhost'; //查看用户权限 granted
mysql> SHOW CREATE USER 'admin'@'localhost'\G //查看用户权限 ungranted
mysql>CREATE USER 'custom'@'localhost' IDENTIFIED BY 'obscure';
mysql>GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP
->ON bankaccount.*
->TO 'custom'@'localhost'; //部分授权
mysql>CREATE USER 'custom'@'host47.example.com' IDENTIFIED BY 'obscure';
mysql>GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP
->ON expenses.*
->TO 'custom'@'host47.example.com';
mysql>CREATE USER 'custom'@'%.example.com' IDENTIFIED BY 'obscure';
mysql>GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP
->ON customer.*
->TO 'custom'@'%.example.com';
-
The first account can access the
bankaccount
database, but only from the local host. -
The second account can access the
expenses
database, but only from the hosthost47.example.com
. -
The third account can access the
customer
database, from any host in theexample.com
domain. This account has access from all machines in the domain due to use of the “%
” wildcard character in the host part of the account name.
2. 修改密码
root@localhost[(none)]> set password for 'fred'@'localhost'=password('passwd');
Query OK, 0 rows affected (0.00 sec)
- root@localhost[(none)]> flush privileges;
- Query OK, 0 rows affected (0.00 sec)
3. 使用mysqldump导出固定条件的数据库,使用source导入数据库
我们来看几个常用用例:
(1)导出整个数据库
mysqldump -u 用户名 -p 数据库名 > 导出的文件名 mysqldump -u wcnc -p smgp_apps_wcnc > wcnc.sql
(2)导出一个表
mysqldump -u 用户名 -p 数据库名 表名> 导出的文件名 mysqldump -u wcnc -p smgp_apps_wcnc users> wcnc_users.sql
(3)导出一个数据库结构
mysqldump -u wcnc -p -d --add-drop-table smgp_apps_wcnc >d:\wcnc_db.sql #-d 不导出数据只导出结构 --add-drop-table 在每个create语句之前增加一个drop table
(4)导入数据库,常用source 命令
#进入mysql数据库控制台, mysql -u root -p mysql>use 数据库
mysql>set names utf8; (先确认编码,如果不设置可能会出现乱码,注意不是UTF-8) #然后使用source命令,后面参数为脚本文件(如这里用到的.sql) mysql>source d:\wcnc_db.sql
4. 删除数据库
drop database 数据库名