1. 登陆MYSQL
mysql -u${user_name}
mysql -u${user_name} -p${password}
-u -p参数后没有空格
2. 创建数据库
CREATE DATABASE `${database_name}` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
例:CREATE DATABASE `test_db` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
3. 创建MYSQL用户
创建一个没有任何权限的账号
CREATE USER '${username}'@'${host}' IDENTIFIED BY '${password}';
例:CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'password';
授权给创建的账号
GRANT ${permission} ON ${database name}.${table name} TO '${username}'@'${host}';
例:GRANT ALL PRIVILEGES ON *.* TO 'newuser'@'localhost';
授权只读权限
GRANT SELECT ON *.* TO 'pasa'@'localhost';
权限生效
FLUSH PRIVILEGES;
删除用户
DROP USER '${user}'@'${host}';
例如:drop user 'root'@'::1';
drop user 'root'@'iz28uzbk2yaz';
drop user 'root'@'127.0.0.1';
drop user ''@'localhost';
drop user ''@'iz28uzbk2yaz';
4、设定mysql root账户密码
SET PASSWORD FOR 'root'@'localhost' = PASSWORD('${password}');
5、创建数据库表
CREATE TABLE IF NOT EXISTS tasks (
task_id INT(11) NOT NULL AUTO_INCREMENT,
subject VARCHAR(45) DEFAULT NULL,
start_date DATE DEFAULT NULL,
end_date DATE DEFAULT NULL,
description VARCHAR(200) DEFAULT NULL,
PRIMARY KEY (task_id) ) ENGINE=MyISAM;
6、修改MYSQL数据存储路径
a、systemctl stop mysql
systemctl status mysql
b、mv /var/lib/mysql /path
c、vim /etc/my.cnf,修改如下配置:
datadir=/var/lib/mysql ----> datadir=/path/mysql;
socket=/path/mysql/mysql.sock ----> socket=/var/lib/mysql/mysql.sock;
log-error=/var/log/mysqld.log ----> log-error=/path/logs/mysql/mysqld.log
d、ln -s /path/mysql /var/lib/mysql,如果不调用mysql命令本地访问mysql,可以不用设置。
e、systemctl start mysql
注意:被mysql server使用的文件需要有mysql用户,mysql组的权限.
7、导入导出SQL脚本
导入mysql脚本
登入mysql: mysql> source /${path}/test.sql;
导出mysql脚本
mysqldump -u${user} -p ${database} > mysql_backup.sql
mysqldump -u${user}-p --database mysql test > my_databases.sql
mysqldump -u${user}-p --all-databases > all_databases.sql
导出数据库结构
mysqldump -u${user} -p -d --add-drop-table mysql > mysql_define.sql
例:mysqldump -uroot -p baidu_user > baidu_user.sql
8、撤销用户权限
REVOKE ALL PRIVILEGES ON *.* FROM 'pig'@'%';
9、删除数据库和数据表
drop database ${dbname};
drop table ${table};
例:drop database baidu_user;
此处仅是一些常用的指令用法,欢迎各位读者学习交流。