[MySQL] Linux operation and maintenance of common script

table of Contents

Practices

1, data manipulation, must be careful online is even worse! ! !
2, the data line operation must be backed up! Need a complete backup available, use their own backup the easiest way to rollback.
3, data manipulation, data can be modified resolved never to delete
4, the operation is not uncertain, undefined command does not knock
5, if a problem occurs, such as data loss, data file is corrupted, you must first complete backup of the current environment, to ensure that subsequent operations will not cause a greater impact, so that people can solve problems left to solve.
6, not allowed to move, modify or delete any of the data file to run in the database.
7, not allowed to delete the recent generation, not backed up binlog
8 does not allow the OS command line, type the password

Common Commands

The following script at the Linux command line execution

1, start the MySQL

/usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf --user=mysql &

Parameter Description:
== - == Defaults-File # FF0000 specified configuration file path
== - user # ff0000 == specify the MySQL server processes running user

2, shut down MySQL

/usr/local/mysql/bin/mysqladmin -uroot -p -S /tmp/mysql.sock shutdown

Parameter Description:
== - == S # FF0000 our sock file address
== shutdown # ff0000 == means closed database

3, into the MySQL client

/usr/local/mysql/bin/mysql -uroot -p -h192.168.0.1 -P3306 -S /tmp/mysql.sock

Parameter Description:
== - == U # FF0000 user name
== - p # ff0000 == password
== - h # ff0000 == address
== - P # ff0000 == port

4. Export SQL query data

/usr/local/mysql/bin/mysql -uroot -p -h192.168.0.1 -P3306 -S /tmp/mysql.sock -e "use db_name;select * from test1;"

Parameter Description:
== - back then e # ff0000 == SQL statements, must be placed in the last argument, followed by a double quotation mark and parcel of SQL statements. Equivalent to enter the MySQL client executes the SQL

5, data backup
full backup (all equipment used for an open GTID)

mysqldump -uroot -p -S /tmp/mysql.sock --single-transaction --master-data=2 -R -E --triggers --opt -A > all_db.sql

Export specified database

mysqldump -uroot -p -S /tmp/mysql.sock --single-transaction -B db_1 db_2 > db_1_2.sql

Export specified table

mysqldump -uroot -p -S /tmp/mysql.sock --single-transaction db_1 table_1 table_2 > table_1_2.sql

Derived from the where condition

mysqldump -uroot -p -S /tmp/mysql.sock --single-transaction --where="status=1" db_1 table_1 > table_1.sql

Parameters:
== - == SINGLE-Transaction # FF0000 export transaction in a manner to ensure data integrity
== - master-data = 2 # ff0000 == derives the current position the binlog
== - == R & lt deriving # FF0000 stored procedure routines
== - == E # FF0000 export event event
== - == export the triggers # FF0000 trigger the triggers
== - == opt # FF0000 with --add-drop-table, --add- locks , --create-Options, - Quick, --extended-INSERT, --lock-the Tables, --set-charset, --disable-Keys
== - == B # FF0000 designated to be exported database, more use a comma separated
== - where # ff0000 == export conditions, with the SQL where clause
== - no-create-db # ff0000 == does not print create database statement
== - no-create-info # ff0000 == create table statement is not printed
== - no-data # ff0000 == insert statement does not export, i.e. not export data in the table, the table structure is used to derive only

Common SQL

The following script MySQL client / command line

-- 查看数据库
show databases;

-- 查看建库语句
show create database db_name;

进入数据库
use database

-- 查看当前进入数据库的所有表
show tables;

-- 模糊匹配
show tables like '%%';

-- 查看指定数据库的所有表
show tables from db_name;

-- 查看建表语句
show create table table_name;

-- 查看表结构
show columns from table_name;

-- 修改表名
RENAME TABLE table_name TO new_table_name [, table_name2 TO new_table_name2] … ;

-- 修改表存储引擎
alter table table_name engine=innodb;

-- 删除表
DROP TABLE table_name;

-- 添加列
ALTER TABLE table_name ADD column_1 VARCHAR(40);

-- 删除列
ALTER TABLE table_name DROP column_1;

-- 修改列
ALTER TABLE table_name MODIFY column_1 VARCHAR(40) NOT NULL;

-- 修改列名
ALTER TABLE table_name CHANGE birth_date date_of_birth DATE;

-- 添加索引
ALTER TABLE table_name ADD INDEX idx_test(column_1,[column_2]...);

-- 删除索引
DROP INDEX idx_test ON table_name;

-- 查看索引
show index from table_name; 

-- 添加外键约束
ALTER TABLE table_name ADD CONSTRAINT fk_name FOREIGN KEY(column_1) REFERENCES t2(column_1); 

-- 查询前10 行数据,limit 关键字必须放在 SQL 语句的最后
select * from test1 limit 10;

-- 查看第 6-10 行数据 
select * from test1 limit 5,5;

-- 按照 create_time 正序排序,asc 关键字默认可不写
select * from test1 order by create_time asc;

-- 按照 create_time 倒序排序
select * from test1 order by create_time desc;


-- 修改语句
UPDATE table_name SET column_1=23,column_2='JOE' WHERE ID=3;

-- 删除语句
DELETE FROM test1 WHERE ID=3;

authority management

-- 创建用户
create user user1@'192.168.1.1';
create user user1@'192.168.1.1' password expire interval 30 day;

-- 授权、初始密码
grant select,insert,delete,update,create on db_name.* to user1@'192.168.1.1' identified by 'password';

-- 修改密码
alter user user1@'192.168.1.1' identified by 'password';

-- 修改用户密码过期时间
alter user user1@'192.168.1.1' password expire interval 30 day;

-- 锁定用户
alter user user1@'192.168.1.1' ACCOUNT LOCK;

-- 解锁用户
alter user user1@'192.168.1.1' ACCOUNT UNLOCK;

-- 撤回权限
revoke update,create on db_name.* from user1@'192.168.1.1';

-- 刷新权限 (修改权限之后必须执行)
flush privileges;

-- 查看权限
show grants for user1@'192.168.1.1';

Management Command

-- 查看全部连接
show full processlist;

-- 查看 MGR 节点状态
select * from performance_schema.replication_group_members;

-- 查看参数
show [global|session] variables like '%buffer_pool%';

-- 修改参数 (仅限于可以在线修改的参数)
set [global|session] autocommit=1;

-- 查看状态
show [global|session] status like '%Com_%';

-- 查看从库状态
show slave status\G

-- 启动从库复制线程
start slave;

-- 启动从库 IO 线程
start slave IO_THREAD;

-- 启动从库 SQL 线程
start slave SQL_THREAD;

-- 启动指定 channel 从库线程
start slave for channel "moshi";

-- 停止从库复制线程
stop slave

-- 清除从库配置:在从库执行,用于重建主从关系,不可恢复,谨慎操作
reset slave all;

Guess you like

Origin www.cnblogs.com/BabySermonizer/p/11444110.html