欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  数据库

MySQL运维之--日常维护操作_MySQL

程序员文章站 2022-04-18 16:06:53
...
一、Linux内核和发行版本 uname -a
cat /etc/issue

二、glibc的版本
/lib/libc.so.6 ---没有man函数据的动态链接库

三、MySQL的版本
MySQL二进制分发版的文件名格式为:mysql-VERSION-OS.tar.gz
例如:Linux-Generic(glibc 2.5) (x86,64bit),Compressed TAR Archive(mysql-5.6.16-linux-glibc2.5-x86_64.tar.gz)

1、下载
http://dev.mysql.com/downloads/mysql/
2、查rpm包装在什么目录下
rpm -qpl MySQL-server-5.6.23-1.el6.i686.rpm |more
rpm -qpl MySQL-client-5.6.23-1.el6.x86_64.rpm |more
3、更改rpm安装路径
rpm --help
rpm --prefix --relocate
rpmbuild spec binary rpm
yum install


四、rpm安装
rpm -ivh xxx.rmp
rpm -pql xxx.rmp


MySQL 实例安装和启动
1. 安装
mysql_install_db --defaults-file=/root/data/mysql3306/my.cnf --basedir=/usr/ --datadir=/root/data/mysql3306/data
2. 启动
mysqld_safe --defaults-file=/root/data/mysql3306/my.cnf &
3. 登录
mysql -h127.0.0.1 -uroot -P3306 -p




五、安装演示:

1.关闭mysql
ps -ef |grep mysqld
kill 3397 3801

2.安装
cat init3306
sh init3306.sh


3.启动
cat start3306.sh
sh start3306.sh

注意:mysql_install_db(通过安装rpm包产生mysql_install_db),有如下命令查看:
rpm -pql MySQL-server-5.5.42-1.linux2.6.i386.rpm |grep install
which mysql_install_db


4.看日志
tail -100f /root/data/mysql3306/log/alert.log
发生数据字典不存在,就会自动创建。。。。


5.查看进程
ps -ef |grep mysqld
mysqld_safe是mysqld的父进程



6.登录
sh my3306.sh --mysql -h127.0.0.1 -uroot --P3306


六、脚本:
1.---安装mysql: init3306.sh
rm -rf /root/data/mysql3306/data/*
rm -rf /root/log/mysql3306/iblog/*
rm -rf /root/log/mysql3306/binlog/*
chmod -R 777 /root/data/mysql3306/data/
chmod -R 777 /root/log/mysql3306/iblog/
chmod -R 777 /root/log/mysql3306/binlog/

chmod 755 /root/data/mysql3306/my.cnf

mysql_install_db --defaults-file=/root/data/mysql3306/my.cnf --basedir=/usr/ --datadir=/root/data/mysql3306/data

chmod -R 777 /root/data/mysql/3306/data/
chmod -R 777 /root/log/mysql3306/iblog/
chmod -R 777 /root/log/mysql3306/binlog/



2.--启动mysql: start336.sh
mysqld_safe --defaults-file=/root/data/mysql3306/my.cnf &


3.--登录mysql: my3306.sh
mysql -h127.0.0.1 -uroot -P3306 -p


------研究下
cd /root/data/mysql3306/data/mysql
cd /root/data/mysql3306/data/performance_schema --性能相关的
cd /root/data/mysql3306/data/test ---测试库
cd /root/log/mysql3306/iblog/ ---innodb自己的数据和日志




------了解my.cnf(多实例用端口来取分)
vi /root/data/mysql3306/my.cnf


[client]
port=3306
socket=/root/data/mysql3306/run/mysql.sock


[mysql]
port=3306
promprt=\\u@\\d \\r:\\m:\\s>


[mysqld]
default-storage-engine=INNODB
character-set-server=iatin1
explicit_defaults_for_timestamp=true


#dir
innodb_log_group_home_dir=/root/log/mysql3306/iblog
innodb_data_home_dir=/root/log/mysql3306/iblog
basedir=/usr
datadir=/root/data/mysql3306/data
tmpdir=/root/data/mysql3306/tmp
slave_load_tmpdir=/root/data/mysql3306/tmp
log-error=/root/data/mysql3306/log/alert.log
slow_query_log_file=/root/data/mysql3306/log/slow.log
relay_log_info_file=/root/log/mysql3306/binlog/relay-log.info
master-info-file=/root/log/mysql3306/binlog/master.info
socket=/root/data/mysql3306/run/mysql.sock
log-bin=/root/log/mysql3306/binlog/binlog
relay-log=/root/log/mysql3306/binlog/relaylog


innodb_force_recovery=0


七、操作mysql


1、登录mysql:
本地: mysql -u$usrename -p$password
远程: mysql -u$username -p$passwrod -h$ip
多实例:mysql -u$username -p$passwrod -P$port

2、用户操用
(1)创建用户
方法一:
insert into mysql.user(user,host,password) values('mytest','localhost',password('1234'));
flush privilege;
方法二:create user mystest@'%' identified by '1234';

(2)用户授权
单纯的授权
grant all privileges on *.* to mytest@localhost;
grant insert,update,delete,select on *.* to mytest@localhost;
授权并创建用户
grant all privileges on *.* to mytest@localhost identified by '1234';--创建用户并刷缓存,
(等同于:insert into mysql.user ,flush privilege)
grant all privileges on *.* to mytest@localhost; --对象权限
grant super on *.* to mytest@'%'; --系统权限 (supert相当于oracle中的dba权限)



3、实操
show databases; --查看所有的数据库
use mysql; --切到mysql数据库
use tables; --在mysql库的tables
select user,host,password from mysql.user; ----查mysql的所有用户,这个是由mysql_install_db创建的
grant all privilege on *.* to test_1@'%'; --all代表(select update,delete,alter admin,super_acl),第一个*用户,第二个*对象,%所有的主机
mysql -h127.0.0.1 -utest_1 ----用grant创建的用户登录mysql
select user(); ---当前是什么用户
create database jianfeng; ---创建数据库(mysql中的数据库类似于oracle中的schema
create table user(id int) engine=innodb ---创建表;
grant select on jianfeng.user to test_1@'%'; ---jianfeng.user表的查询授权给test_1用户
insert into mysql.user(user,host,password) values('test_2','%',password('1234')); --用这种方法创建test_2用户,有个问题权限没有
flush privileges; ---把mysql.user表的用户权限重新刷到内存中
show master status\G;
change master to xxx;
show processlist; ---查看当前用户的连接,线程形式(类似oracle中的v$session)


4、drop table处理
rename table test_1 to test;(可以快速切回来rename table test to test_1;)
备份mysqldump:mysqldump -h127.0.0.1 -uroot mydb gyj_t1 >/tmp/gyj_t1.sql
drop table test;


5、自增主键(最好是自己定义主键,系统默认的是全局的增量)
create table test (id int primary key auto_increment,name varchar(100)) engine=innodb;
show create table test\G;
create index test_name_idx on test(name);
show create table test\G;
insert into test(name) values('test');
commit;
select * from test;




6、alter table处理 --会动原来的数据,需要拷贝数据
alter table test add coll int;


7、执行计划
select * from test where id=1\G;
explain select * from test where id=1;
create index test_id_coll_idx on test(id,coll);
explain select * from test where id=1;
create index test_col_name on test(coll,name);
explain select * from test where coll>10 and name='xx';
show create table test\G;
alter table test drop index test_name_idx;
explain select * from test where coll>10 and name>'xx';


8、数据导出
(1)用dump导出数据
mysqldump -h127.0.0.1 -uroot mydb gyj_t1 >/tmp/xx.sql
drop table test;
source /tmp/xx.sql --导入数据


(2)用select导出数据
select * from test into outfile '/tmp/yy.sql';


9、数据迁移
(1)停机方式
mysqldump/loadata
(2)不停机方式
物理上:搭备库(可以级联5.5-->5.6,向下兼容的)
把主库read only,备库就能把主库转过来的binlog消化完,再把备库切为主
show variables like '%read%';
set global read_only=on;
insert into test(name) values('xx'); --插不进的,不能用root用户
(3)不同平台小表:oracle--->mysql
脚本:synfull.pl

(4)不同平台的一个大表迁多:增量迁移
a.把数据的全量迁过去
b.把迁的过程中产生的日志传过去
c.apply增量
d.锁表切切换

(5)增量
a.Oracle:物化视图
b.MySQL:trigger
create trigger tri_test
before insert,delete,update
insert test_log value(type,id);
end;
/
insert into test values(1,'xxx');
test_log value('insert','1');
lock table test;
应用切换

10、binlog
reset master; --会把当前的binlog清掉
show binlog events;
create table x1(id int);
show binlog events;
insert into x1 values(1);
commit;
show binlog events;
类似于: mysqlbinlog -vvv binlog.00001 > /tmp/binlog.log
vi /tmp/binlog.log
WAL: write ahead log,日志优先写


11、归档
flush logs;
show master status;


write ahead log. recover backup, duriably. undo acid mvcc

12、参数和统计信息
show variables; ----参数
show variables like '%bin%';
show status; ----统计信息
show global status like'%insert%';
insert into test(name) values('xxxxx');


show variables like '%default%';
set global default_storage_engine=myisam; ---不影响当前会话的操作,影响新建立的连接
set session default_storage_engine=myisam; ---影响当前会话的操作


---连接池
max_connect
min_connect
max_idle
time_out

disconnect --释放