Mysql cmake方式安装
1.卸载原有mysql下载MySQL安装包 [root@localhost ~]# yum remove mysql* -y [root@localhost ~]# mkdir /tmp/downloads [root@localhost ~]# cd /tmp/downloads [root@localhost downloads]# wget http://mirrors.sohu.com/mysql/MySQL-5.5/mysql-5.5.54.tar.gz [root@localhost mysql-5.5.54]# userdel mysql [root@localhost mysql-5.5.54]# groupadd mysql [root@localhost mysql-5.5.54]# useradd -g mysql -s /sbin/nologin -M mysql 2.解压并安装 [root@localhost downloads]# tar zxf mysql-5.5.54.tar.gz [root@localhost downloads]# cd mysql-5.5.54 [root@localhost mysql-5.5.54]# yum -y install gcc-c++ cmake ncurses-devel ncurses [root@localhost mysql-5.5.54]# mkdir -p /data/3306 [root@localhost mysql-5.5.54]# cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql-5.5.54 #安装路径 > -DMYSQL_DATADIR=/data/3306/ #数据路径 > -DMYSQL_UNIX_ADDR=/tmp/mysql.sock #sockt路径 > -DDEFAULT_CHARSET=utf8 #字符集 > -DDEFAULT_COLLATION=utf8_general_ci #字符校对规则 > -DENABLED_LOCAL_INFILE=ON # > -DWITH_INNOBASE_STORAGE_ENGINE=1 #INNODB存储引擎 > -DWITH_FEDERATED_STORAGE_ENGINE=1 > -DIWTH_BLACKHOLE_STORAGE_ENGINE=1 > -DWITH_EXAMPLE_STORAGE_ENGINE=1 > -DWITH_PARTITION_STORAGE_ENGINE=1 [root@localhost mysql-5.5.54]# make && make install #安装后调整 [root@localhost mysql-5.5.54]# ln -s /usr/local/mysql-5.5.54/ /usr/local/mysql [root@localhost mysql-5.5.54]# yes|cp support-files/my-small.cnf /etc/my.cnf [root@localhost mysql-5.5.54]# echo 'export PATH=/usr/local/mysql/bin:$PATH' >> /etc/profile [root@localhost mysql-5.5.54]# source /etc/profile [root@localhost mysql-5.5.54]# chown -R mysql.mysql /usr/local/mysql-5.5.54/ [root@localhost mysql-5.5.54]# chown -R mysql.mysql /data/3306/ [root@localhost mysql-5.5.54]# /usr/local/mysql-5.5.54/scripts/mysql_install_db --basedir=/usr/local/mysql-5.5.54/ --datadir=/data/3306/ --user=mysql [root@localhost mysql-5.5.54]# yes|cp support-files/mysql.server /etc/init.d/mysqld [root@localhost mysql-5.5.54]# chmod 700 /etc/init.d/mysqld [root@localhost mysql-5.5.54]# service mysqld start [root@localhost mysql-5.5.54]# chkconfig mysqld on
优化mysql
1.删除test库(这个库不安全) mysql> drop database test; 2.删除无关用户和主机名 mysql> select user,host from mysql.user; +-------------+-----------+ | user | host | +-------------+-----------+ | root | 127.0.0.1 | | root | ::1 | | root | C-server | | daniel | localhost | | danieu_tf8l | localhost | | root | localhost | +-------------+-----------+ 6 rows in set (0.05 sec) mysql> delete from mysql.user; #删除所有用户 mysql> grant all privileges on *.* to system@'localhost' identified by '123'with grant option; Query OK, 0 rows affected (0.00 sec) #重新取名root并重新赋权 mysql> grant all privileges on *.* to system@'127.0.0.1' identified by '123' with grant option; Query OK, 0 rows affected (0.00 sec) mysql> select user,host from mysql.user; +--------+-----------+ | user | host | +--------+-----------+ | system | localhost | +--------+-----------+ 1 row in set (0.00 sec) mysql> select user,host from mysql.user; +--------+-----------+ | user | host | +--------+-----------+ | system | 127.0.0.1 | | system | localhost | +--------+-----------+
MySQL数据库备份及压缩备份
#备份单个数据库 [root@localhost mysql-5.5.54]# cd / [root@localhost /]# mkdir /backup #创建备份目录 [root@localhost /]# mysqldump -usystem -p123 -B Daniel >/backup/Daniel_$(date +%F).sql #备份单个数据库,加B比不加多一个创建备份库的命令 [root@localhost /]# egrep -v "^--|*|^$" /backup/Daniel_2018-02-12.sql DROP TABLE IF EXISTS `test`; CREATE TABLE `test` ( `id` int(4) NOT NULL AUTO_INCREMENT, `name` char(20) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8; LOCK TABLES `test` WRITE; INSERT INTO `test` VALUES (1,'zhaoyue'),(2,'jesse'),(3,'jeacen'),(4,'elain'),(5,'wodi'),(6,'yingsui'),(7,'zhangyang'),(8,'zaixiangpan'),(9,'呵呵'); UNLOCK TABLES; #查看一下备份内容 #现在我们登进去删掉Daniel库 #然后再次创建Daniel库 [root@localhost /]# mysql -usystem -p123 Daniel </backup/Daniel_2018-02-12.sql #恢复 #压缩备份 [root@localhost /]# mysqldump -usystem -p123 -B Daniel|gzip >/backup/Daniel_$(date +%F).sql.gz #指定字符集备份 [root@localhost /]# mysqldump -usystem -p123 -B --default-character-set=utf8 Daniel|gzip >/backup/Daniel_$(date +%F)_utf8.sql.gz #备份多个数据库 #先创建三个数据库test1,test2,test3 [root@localhost /]# mysqldump -usystem -p123 --default-character-set=utf8 -B test1 test2 test3|gzip >/backup/Daniel_$(date +%F)_utf8.sql.gz #这是放在了一个文件里,如何做分库备份呢? #思路:写个脚本,去查数据库的所有库,然后写到列表里,逐条备份 #备份多个表 命令:mysqldump -u用户名 -p 数据库名 表名1 表名2 > 备份的文件名 [root@localhost /]# mysqldump -usystem -p Daniel test>/backup/test_table_$(date +%F).sql #Daniel是库后面跟表就OK,不要加B [root@localhost /]# egrep -v "^--|*|^$" /backup/test_table_2018-02-13.sql #查看一下,这是备份单个,接下来是辈分多个,我们先在Daniel库中创建几个表 mysql> create table t1(id int); Query OK, 0 rows affected (0.08 sec) mysql> create table t2(id int); Query OK, 0 rows affected (0.00 sec) mysql> create table t3(id int); Query OK, 0 rows affected (0.06 sec) [root@localhost /]# mysqldump -usystem -p Daniel test t1 t2 t3>/backup/test_table_$(date +%F).sql #备份多个 [root@localhost /]# egrep -v "^--|*|^$" /backup/test_table_2018-02-13.sql #如何进行分表备份 单表备份多写几次啊 #备份数据库结构 用-d就行 [root@localhost /]# mysqldump -usystem -p -d Daniel>/backup/Daniel_$(date +%F).sql [root@localhost /]# mysqldump -usystem -p -d Daniel test>/backup/test_table_$(date +%F).sql #备份表结构 #前面用egrep的时候过滤很多,因为备份文件里的垃圾信息太多了,我们可以加参数解决 --comacpt #使输出内容更加简洁,不包括默认选项中各种注释,有如下几个参数的功能 --skip-add-drop-table --no-set-names --skip-disable-key --skip-locks #其他参数 -x 锁表 #备份的时候禁止写入数据,否则会乱掉,可以读 -l 锁表 #不允许读
MySQL恢复实战
#source恢复,登录到mysql界面,使用source 加备份数据库名字(要在这个备份数据库文件的当前路径进入mysql,也就是通过相对路径恢复),在这里恢复也可以指定恢复的字符集-- set names utf8; #mysql恢复,去掉dump直接使用mysql也可恶意恢复
MySQL非交互式操作数据库
#通过-e命令实现非交互式对话 [root@localhost ~]# mysql -usystem -p123 -e 'use Daniel;select * from test;' #通过-e参数我可以在命令直接查看Daniel数据库的test表的所有东西,而且还可以过滤 [root@localhost ~]# mysql -usystem -p123 -e 'use Daniel;select * from test;'|grep zhao [root@localhost ~]# mysql -usystem -p123 -e 'show processlist;' #查看进程数 [root@localhost ~]# mysql -usystem -p123 -e 'show full processlist; #显示的sql语句更全 [root@localhost ~]# mysql -usystem -p123 -e 'show variables;' #查看变量 [root@localhost ~]# mysql -usystem -p123 -e 'show global status;' #查看状态 [root@localhost ~]# mysql -usystem -p123 -e 'show global status;'|egrep "Com_select|Com_insert|Com_delete|Com_update" Com_delete 0 #删除操作0条 Com_delete_multi 0 Com_insert 0 Com_insert_select 0 Com_select 127 Com_update 0 Com_update_multi 0 [root@localhost ~]# mysql -usystem -p123 -e "set global key_buffer_size = 32777218;" #修改全局参数,但重启不会生效 #其他非交互方法 1.echo [root@localhost ~]# echo "show databases;" | mysql -usystem -p123 2.cat [root@localhost ~]# cat |mysql -usystem -p123 <<EOF > show databases; > EOF