本地连接数据库
shell> mysql -h host
-P port
-u root -p
查看数据库包含的所有schemas
mysql> show databases;
连接某个数据库
mysql> use database
;
查看数据库包含的所有表
mysql> show tables;
为root用户配置权限
mysql> use mysql;
mysql> create user ‘root’@’%’ identified by ‘password
‘;
mysql> grant all privileges on *.* to ‘root’@’%’ with grant option;
mysql> flush privileges; # 重新加载授权表
数据库迁移
首先导出数据库
shell> mysqldump –databasesyour database
>sql file
注意 导出的sql文件,会自动包含create database和use语句, 如果不想包括这两行, 比如需要重新定义数据库名, 去掉–databases选项即可, 如下:
shell> mysqldumpyour database
>sql file
导入数据库
shell> mysql <sql file
或者登入sql命令行界面输入:
sql> sourcesql file
注意 阿里云云数据库在普通账号下不能直接创建数据库. 需要手动通过官方网站创建数据库, 然后给账号分配读写权限, 再输入上述命令.
表名
表名引用的标准做法是使用反引号进行引用, 这样表名可以使用任意字符串. 否则对于一些特殊表明比如带!等表名, 查询会出错.
Error Code: 1175.
You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column To disable safe mode, toggle the option in Preferences -> SQL Queries and reconnect.
SET SQL_SAFE_UPDATES = 0;
mysqlimport: 大量数据快速导入
mysqlimport -h 192.168.1.201 -u root -p -P 6033 sat refund_unmatched_sales.txt --local
其中, sat为数据库, refund_unmatched_sales.txt为包含大量数据的文本文件, 其文件名必须与数据库的表名相同. –local必须要, 指示refund_unmatched_sales.txt为本地文件, 而非数据库机器上的文件.
root密码忘记解决方法
修改/etc/my.cnf配置文件
在[mysqld]段增加: skip-grant-tables重启mysql
/etc/init.d/mysqld restart进入mysql修改root密码
mysqluse mysql;
update user password = password(‘your new password’) where user = ‘root’;
flush privileges;
quit改回/etc/my.cnf配置文件
重启mysql