[关闭]
@xtccc 2018-07-03T03:14:40.000000Z 字数 2387 阅读 5159

MySQL使用

给我写信
GitHub

此处输入图片的描述

Database





初始化MySQL


在安装完MySQL后,MySQL会为root用户产生一个默认的初始密码,如下:

[root@ecs1 ~]# grep 'temporary password' /var/log/mysqld.log

2015-11-07T06:48:24.236714Z 1 [Note] A temporary password is generated for root@localhost: X_f#f+Zxm6P&

这里可以看出,初始的密码就是X_f#f+Zxm6P&,用该密码登录到MySQL的控制台后,需要修改改密码才能继续操作数据库:

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'NJZD@2014*xiaotao';
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)




无法从远程登录MySQL


上面我们可以从localhost登录MySQL,但是使用同样的用户名和密码从远程的可视化界面客户端就无法登录到MySQL了,如下:
QQ20151107-0@2x.png-95.9kB



这是因为当前的策略是不允许从localhost之外的地方以root身份登录到MySQL,可以如下修改系统表:

mysql> use mysql;

mysql> update user set host='%' where user='root';

mysql> flush privileges;




Dump and Import


从*.sql文件将数据导入到MySQL

local在mysql client内操作
1. 登录到MySQL;
2. 使用某个数据库(如不存在,首先创建);
3. 将该文件导入到该数据库中

如下:

mysql> create database kms;
mysql> use kms;
mysql> source /disk1/users/tao/standard_cloud-2015-1102-1318-33.sql;



远程用mysqldump操作
MySQL Server在远程host,只要本机有mysql client,就可以进行数据的导入和导出。

$ mysql -h [host] -u [user] -p'[passwd]' [schema] < [filename]

该操作不会输出进度及细节。




导出数据到文件


local在mysql client内操作

mysql> select A, to_base64(B) from fsn.product
into outfile '/data/fsn.dump'
fields terminated by X'01'
lines terminated by X'02';

这里,X'01'表示ASCII码为1的字节, to_base64()方法需要在5.6以上的版本才具有。

如果执行时报错: The MySQL server is running with the --secure-file-priv option so it cannot execute this statement

说明权限出了问题,参考 MySQL : How to tackle --secure-file-priv



远程用mysql命令操作

$ mysqldump -h [host] -u [user] -p'[passwd]' [schema] [table] > [filename]

该操作不会输出进度及细节。



Dump / Load 的批处理

  1. #!/bin/bash
  2. declare -a arr=("attribute" "attributelist" "billing_cycle" "commitlog" "compatibilitymatrix" "compatibilitymatrix2" "contractbody" "contractmodifier" "contractoption" "contractratecode" "contractroot" "contractrule" "contractscenario" "customer_transform_rule" "customerbillingcycle" "deftable" "deftablesummary" "dictionary" "dictionaryentry" "eligibilitymatrix" "globaltable" "internaluser" "marketplacetable" "parameter" "reftable" "reftable2" "rulemodel" "scenarios")
  3. for i in "${arr[@]}"
  4. do
  5. echo "dumping $i ..."
  6. mysqldump -h [ip] -u [user] -p'[passwd]' [schema] "$i" > "sce.$i.sql"
  7. ## 如果是load,则换成下面的
  8. ## mysql -h [host] -u [user] -p'[passwd]' [new-schema] < "sce.$i.sql"
  9. done
  10. echo "all done !"




ResultSet中的重名列


假设:
表test1有4列: pk, c0, c1, c2
表test2有4列: pk, c1, c2, c3

现在希望将这两张表以pk为主键关联起来进行查询,那么查询结果ResultSet中就分别存在test1和test2的c1列和c2列,怎样来区分属于两张表的列名呢?

答案是在查询时对列使用别名,如下:

select t1.pk, t1.c0, t1.c1, t1.c2, t2.c1, t2.c2, t2.c3
from test1 as t1, test2 as t2
where t1.pk = t2.pk




添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注