mysql笔记
2019-11-09
修改并查看注释(
2019-11-09
安装
sudo apt install mysql-server mysql-client |
使用
用户登录
# 连接到MySQL服务器 |
添加用户
insert
use mysql |
key | value |
---|---|
Host | 'localhost' |
User | 'niabie' |
Select_priv | 'Y' |
Insert_priv | 'Y' |
Update_priv | 'Y' |
Delete_priv | 'Y' |
Create_priv | 'Y' |
Drop_priv | 'Y' |
Reload_priv | 'Y' |
Shutdown_priv | 'Y' |
Process_priv | 'Y' |
File_priv | 'Y' |
Grant_priv | 'Y' |
References_priv | 'Y' |
Index_priv | 'Y' |
Alter_priv | 'Y' |
Show_db_priv | 'Y' |
Super_priv | 'Y' |
Create_tmp_table_priv | 'Y' |
Lock_tables_priv | 'Y' |
Execute_priv | 'Y' |
Repl_slave_priv | 'Y' |
Repl_client_priv | 'Y' |
Create_view_priv | 'Y' |
Show_view_priv | 'Y' |
Create_routine_priv | 'Y' |
Alter_routine_priv | 'Y' |
Create_user_priv | 'Y' |
Event_priv | 'Y' |
Trigger_priv | 'Y' |
Create_tablespace_priv | 'Y' |
ssl_type | '' |
ssl_cipher | '' |
x509_issuer | '' |
x509_subject | '' |
max_questions | 0 |
max_updates | 0 |
max_connections | 0 |
max_user_connections | 0 |
plugin | 'mysql_native_password' |
authentication_string | password('' ) |
password_expired | 'N' |
password_last_changed | '2019-11-07 15:17:00' |
password_lifetime | null |
account_locked | 'N' |
用户具有创建数据库等的权限,
authentication_string
不需要则填null
create
create user lynx; |
用户没有创建数据库的权限. 根据版本的变化,
authentication_string
赋值可能改为=''
修改数据库位置
- 查看数据库存放位置
show variables like '%dir%'; |
修改数据库存放位置
- 暂停mysql服务
sudo systemctl stop mysql |
- 转移目录
sudo rsync -av /var/lib/mysql {dir} |
- 修改mysql配置
/etc/mysql/mysql.conf.d/mysqld.cnf
[mysqld] |
- 修改apparmor访问rules
/etc/apparmor.d/tunables/alias
alias /var/lib/mysql -> {dir}/mysql/, |
sudo systemctl restart apparmor |
- 创建mysql所需的的最小文件结构
sudo mkdir /var/lib/mysql/mysql -p |
- 重启mysql
sudo systemctl start mysql |
基本操作
# 创建数据库 |
查看相关信息
- 查看版本等
select version(); |
- 查看端口
show variables like 'port'; |
语法
建表
create table if not exists customers( |
修改并查看注释(comment
)
alter table customers |
复制表
复制结构
create table table_new like table_old |
复制数据
insert into table_new select * from table_old |
insert
去重
ignore
遇到重复的不进行insert
insert ignore into table (column) |
on duplicate key update
TODO
replace into
遇到重复的, 将完全覆盖
insert ignore into table (column) |
更新表数据
update table_name set item="shit" where fuck="fuck"; |
每组前n
计算时间差
SELECT TIMESTAMPDIFF(MONTH,'2009-10-01','2009-09-01'); |
其中MONTH
部分可以更改为
SECOND
MINUTE
HOUR
DAY
MONTH
YEAR
通配符
select season_id, id, title, p_year, c_year from season |
数据回滚
binlog2sql
安装
git clone https://github.com/danfengcao/binlog2sql.git |
使用
用不了, pymysql
版本冲突