mysql 的常用命令及常见问题解决方法
运行sql
C:\Users\Martin>mysql -uroot -pyang cdm_db <d:/cdm_db.sql
运行sql
mysql>source /tmp/terminal.sql;
mysql忘记密码:
mysqladmin -uroot flush-privileges password "newpassword"
mysql的select into file命令
SELECT a,b,a+b INTO OUTFILE ‘/tmp/result.txt‘
FIELDS TERMINATED BY ‘,‘ OPTIONALLY ENCLOSED BY ‘"‘
LINES TERMINATED BY ‘\n‘
FROM test_table;
创建用户
grant all on 数据库名.* to 用户名@localhost identified by ‘密码‘;
删除用户
mysql>Delete FROM user Where User="xxxxx" and Host="localhost";
mysql>flush privileges;
mysql不能运行中文sql的问题,运行如下命令:
set names ‘gbk‘;
新加用户服务xcj,新加的用户不能马上生效
mysql> grant all on *.* to xcj@‘%‘ identified by "xcj_passwd";
Query OK, 0 rows affected (0.04 sec)
#生效新加用户xcj权限
mysql> flush privileges;
Query OK, 0 rows affected (0.03 sec)
Load Data InFile ‘C:/Data.txt‘ Into Table `TableTest` Fields Terminated By ‘,‘ Enclosed By ‘"‘ Escaped By ‘"‘ Lines Terminated By ‘\r\n‘;
alter table ccp_terminal_os_msg add column terminalmodecode varchar(200);
alter table ccp_terminal_os_msg add column osVersion varchar(200);
alter table ccp_company_terminal_msg add column osVersion varchar(200);
alter table ccp_company_terminal_msg add column osname varchar(200);
从另外一张表选取数据更新表
UPDATE ut01 T1 INNER JOIN t23h T2 ON T1.msisdn=T2.msisdn
SET userservicetype=0+T2.userservicestype;
mysql导出整个库的建表及insert语句到一个文件中
mysqldump -uroot -proot mc_db>init.sql
mysql导出一个表的建表语句及insert语句到一个文件中
mysqldump --skip-opt -uroot -p database tablename > script.sql
linux启动mysql
/etc/init.d/mysql start
service mysql start
mysql操作用户的基本操作
1. CREATE USER
语法:
CREATE USER ‘username‘@‘host‘ IDENTIFIED BY ‘password‘;
例子: CREATE USER ‘dog‘@‘localhost‘ IDENTIFIED BY ‘123456‘;
CREATE USER ‘pig‘@‘192.168.1.101_‘ IDENDIFIED BY ‘123456‘;
CREATE USER ‘pig‘@‘%‘ IDENTIFIED BY ‘123456‘;
CREATE USER ‘pig‘@‘%‘ IDENTIFIED BY ‘‘;
CREATE USER ‘pig‘@‘%‘;
实例1:
mysql> create user jss;
这样创建的用户,可以从任意安装了mysql客户端,并能够访问目标服务器的机器上创建连接,无须密码.例如,从ip:10.0.0.99的客户端执行连接:
mysql -ujss -h 172.16.1.110
查看该用户:
mysql> select user,host,password from user where user=‘jss‘;
SELECT USER(); //显示当前用户
实例2:
mysql> create user jss_ps identified by ‘jss‘;
用户连接时,必须指定密码,那就可以在创建用户时,通过指定identified by子句来设定密码
用密码登陆:
mysql -ujss_ps -p -h 172.16.1.110
如果希望指定的用户只能从某台指定的域(domain)或主机访问,可以在创建用户时指定host,例如,指定用户只能从10.0.0.99访问
mysql> create user [email protected] identified by password ‘123456‘;
2. 使用GRANT语句
语法:mysql> grant 权限1,权限2,...权限n on 数据库名称.表名称 to 用户名@用户地址 identified by ‘连接口令‘;
权限1,权限2,...权限n代表
select,insert,update,delete,create,drop,index,alter,grant,references,reload,shutdown,process,file等14个权限
实例:
mysql>grant select,insert,update,delete,create,drop on vtdc.employee to [email protected] identified by ‘123‘;
给来自10.163.225.87的用户joe分配可对数据库vtdc的employee表进行select,insert,update,delete,create,drop等操作的权限,并设定口令为123。
mysql>grant all privileges on vtdc.* to [email protected] identified by ‘123‘;
给来自10.163.225.87的用户joe分配可对数据库vtdc所有表进行所有操作的权限,并设定口令为123。
mysql>grant all privileges on *.* to [email protected] identified by ‘123‘;
给来自10.163.225.87的用户joe分配可对所有数据库的所有表进行所有操作的权限,并设定口令为123。
mysql>grant all privileges on *.* to joe@localhost identified by ‘123‘;
给本机用户joe分配可对所有数据库的所有表进行所有操作的权限,并设定口令为123。
3. 直接向mysql.user表插入记录:
mysql> insert into user (host,user,password) values (‘%‘,‘jss_insert‘,password(‘jss‘));
mysql>flush privileges; //刷新系统权限表
4. 修改mysql用户密码方式:
a. 使用mysqladmin语法:mysqladmin -u用户名 -p旧密码 password 新密码
例如:mysqladmin -u root -p 123 password 456;
b. 直接修改user表的用户口令:
语法:update mysql.user set password=password(‘新密码‘) where User="phplamp" and Host="localhost";
实例:update user set password=password(‘54netseek‘) where user=‘root‘;
flush privileges;
c. 使用SET PASSWORD语句修改密码:语法:
SET PASSWORD FOR ‘username‘@‘host‘ = PASSWORD(‘newpassword‘);
如果是当前登陆用户用SET PASSWORD = PASSWORD("newpassword");
实例:
set password for root@localhost=password(‘‘);
SET PASSWORD FOR name=PASSWORD(‘new password‘);
SET PASSWORD FOR ‘pig‘@‘%‘ = PASSWORD("123456");
5. 删除用户和撤销权限:
a. 取消一个账户和其权限
Drop USER user;
drop user username@‘%‘
drop user username@localhost
b. 取消授权用户:
语法:REVOKE privilege ON databasename.tablename FROM ‘username‘@‘host‘;
例子: REVOKE SELECT ON *.* FROM ‘pig‘@‘%‘;
REVOKE SELECT ON test.user FROM ‘pig‘@‘%‘;
revoke all on *.* from sss@localhost ;
revoke all on user.* from ‘admin‘@‘%‘;
SHOW GRANTS FOR ‘pig‘@‘%‘; //查看授权
c. 删除用户:
语法: Delete from user where user = "user_name" and host = "host_name" ;
例子:delete from user where user=‘sss‘ and host=‘localhost‘;
MySql5.6操作时报错:You must SET PASSWORD before executing this statement解决
mysql> SET PASSWORD = PASSWORD(‘123456‘);
Query OK, 0 rows affected (0.03 sec)
mysql> create database roger;
Query OK, 1 row affected (0.00 sec)
也就是用mysql> SET PASSWORD = PASSWORD(‘123456‘);这句话重新设置一次密码!
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。