Mysql 日常使用

Posted by ZhangShun Blog on September 5, 2018

安装

centos7

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
yum -y install gcc gcc-devel gcc-c++ gcc-c++-devel autoconf* automake* zlib* libxml*ncurses-devel ncurses libgcrypt* libtool* cmake openssl openssl-devel bisonbison-devel perl-Data-Dumper boost boost-doc boost-devel ncurses-devel

groupadd mysql
 
useradd -g mysql -s /sbin/nologin mysql

mkdir -p /data/mysql

chown -R mysql:mysql /data/mysql

tar zxf mysql-5.7.15.tar.gz && tar zxf boost_1_59_0.tar.gz

cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/data/mysql -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1  -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DENABLED_LOCAL_INFILE=1 -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DEXTRA_CHARSETS=all -DWITH_BOOST=/data/mysql/boost_1_59_0 -DMYSQL_TCP_PORT=3306 && make && make install
#cp配置文件

cp support-files/my-default.cnf /etc/my.cnf

cp support-files/mysql.server /etc/init.d/mysqld

cp /usr/local/mysql/bin/* /usr/bin/

chmod 755 /etc/init.d/mysqld
### 初始化配置安装:
cd /usr/local/mysql/bin

./mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql

mkdir /data/mysql/log && chown -R mysql:mysql /data/mysql

配置文件 my.cnf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
[client]
port = 3306
socket = /data/mysql/mysql.sock
[mysqld]
binlog_format = mixed
port = 3306
socket = /data/mysql/mysql.sock
basedir = /usr/local/mysql
datadir = /data/mysql
log-bin = mysql1-bin
server-id = 5
binlog-ignore-db = mysql
binlog-ignore-db = information_schema
binlog-ignore-db = sys
binlog-ignore-db = performance_schema
auto-increment-increment = 5
auto-increment-offset = 1
log-slave-updates = ON
max_connections=3000
log_timestamps=SYSTEM
back_log=50
max_user_connections=2000
innodb_thread_concurrency=8
default-storage-engine=InnoDB
innodb_buffer_pool_size=8192M
innodb_log_buffer_size=8M
innodb_log_file_size=48M
innodb_flush_log_at_trx_commit=2
innodb_lock_wait_timeout=50

long_query_time = 1
slow_query_log=YES
slow_query_log_file=/data/mysql/log/slow.log
log-error=/data/mysql/log/error.log 
plugin-load=AUDIT=libaudit_plugin.so
audit_json_file=1
audit_json_file=ON
audit_record_cmds=connect,Quit,show,select,insert,update,delete
audit_whitelist_users=admintool,blacklist,blackselect,procdb,mysql1,gmetric,mysql.sys,{}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
查看mysql临时密码

cat ~/.mysql_secret

mysql修改密码

格式:mysql> set password for 用户名@localhost = password('新密码'); 
 
授权:grant replication slave,replication client on 库名.表名 to '用户名'@'IP地址' identified by '密码';

   flush privileges;

复制:change master to master_host='192.168.1.102',master_user='rep',master_password='123456',master_log_file='mysql-bin.000002',master_log_pos=453;


### mysqldump
**mysqldump -u用戶名 -p密码 -d 数据库名 表名 > 脚本名;**

**mysql -u用戶名 -p密码 数据库名 < 脚本名**

导出整个数据库结构和数据

mysqldump -h localhost -uroot -p123456 database > dump.sql

导出单个数据表结构和数据

mysqldump -h localhost -uroot -p123456  database table > dump.sql

导出整个数据库结构(不包含数据)

mysqldump -h localhost -uroot -p123456  -d database > dump.sql

导出单个数据表结构(不包含数据)

mysqldump -h localhost -uroot -p123456  -d database table > dump.sql

导出存储过程

mysqldump -u 数据库用户名 -p -n -t -d -R 数据库名 > 文件名  。再导入时如果报错ERROR 1235 (42000) at line **: This version of MySQL doesn't yet support ‘multiple triggers with the same action time and event for one table’,需要把trigger关闭

mysqldump -u 数据库用户名 -p -n -t -d -R --triggers=false 数据库名 > 文件名  。 如果报错ErrorCode:1418This function has none of DETERMINISTIC, NOSQL, or READS SQL DATA inits declaration and binary logging is enabled (you *might* want to use the less safe log_bin_trust_function_creators variable)

解决方法是,在/etc/my.cnf中找到[mysqld],在它下面添加这样一行:log-bin-trust-function-creators=1

### 清理binlog

清理十天之前的binlog:PURGE MASTER LOGS BEFORE DATE_SUB(CURRENT_DATE, INTERVAL 10 DAY);

清除指定binlog:PURGE MASTER LOGS TO 'MySQL-bin.010';