MySQL
0
无    2020-09-02 12:00:26    0    0
myron

2.1在线安装mysql

安装MySQL YUM资源库

yum localinstall https://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm

安装MySQL 5.7

yum install -y mysql-community-server

启动MySQL服务器和MySQL的自动启动

systemctl start mysqld.service
systemctl enable mysqld.service

查看首次登录密码

cat /var/log/mysqld.log|grep 'A temporary password'
# 最后一行冒号后面的部分就是初始密码。

登录mysql并修改密码

mysql -p
 
 
#修改全局安全参数
mysql> set global validate_password_policy=0;
mysql> set global validate_password_length=1;
 
 
#修改密码
mysql> alter user 'root'@'localhost' identified by '123456';
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' ;
 
 
#刷新权限
mysql> flush privileges;
 
#使用sql语句退出
mysql> exit;
重启mysql
service mysqld restart


问题描述:

mysql8 降版本到5.7 无法启动报错

Table flags are 0 in the data dictionary bu t the flags in file ./ibdata1 are 0x4800!

 

处理方法:

mysql8   mysqldump 导出数据库  

mysql5.7 导入数据时报错

Unknown collation: 'utf8mb4_0900_ai_ci'

 

解决方法:
打开sql文件,将文件中的所有
utf8mb4_0900_ai_ci替换为utf8_general_ci
utf8mb4替换为utf8
保存后再次运行sql文件,运行成功

 

 


MySQL Packet for query is too large

Caused by: com.mysql.jdbc.PacketTooBigException: Packet for query is too large (3227 > 1024). You can change this value on the server by setting the max_allowed_packet' variable.
at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:3178)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1917)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2060)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2536)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2465)
at com.mysql.jdbc.StatementImpl.executeQuery(StatementImpl.java:1383)
at sun.reflect.GeneratedMethodAccessor34.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.logicalcobwebs.proxool.ProxyStatement.invoke(ProxyStatement.java:100)
at org.logicalcobwebs.proxool.ProxyStatement.intercept(ProxyStatement.java:57)
at $java.sql.Statement
EnhancerByProxool
EnhancerByProxool
5159dcd0.executeQuery(<generated>)
at org.springframework.jdbc.core.JdbcTemplate$1QueryStatementCallback.doInStatement(JdbcTemplate.java:441)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:396)
... 52 more



原因: 查询出的数据包过大,默认情况下mysql 的字段容量不够装,所以抛出此异常

解决办法:

1.首先在控制台查询一下:SHOW VARIABLES LIKE '%max_allowed_packet%';

默认情况下,最大允许数据包的容量是1M ,需要修改更大一点。20M

2.到mysql 的安装目录下找到 my.ini 文件中,新增

[mysqld]

max_allowed_packet=20M

备注:默认情况下是没有上述的标记,有就修改,没有就添加

3. 重启mysql服务

4.在控制台查看是否修改成功,在命令控制台输入:SHOW VARIABLES LIKE '%max_allowed_packet%';

显示:

mysql> SHOW VARIABLES LIKE '%max_allowed_packet%';
+--------------------------+------------+
| Variable_name | Value |
+--------------------------+------------+
| max_allowed_packet | 20971520 |
| slave_max_allowed_packet | 1073741824 |
+--------------------------+------------+
2 rows in set

5.修改成功,有效!

后续:

遇到mysql 服务停掉后,有时候设置的max_allowed_packed 的没有用,需要使用

set global max_allowed_packet = 2*1024*1024*10;
设置成功了,以后重启也没事。



——————————————————————————
mysql8 默认登录问题


> select host,user,plugin from mysql.user;


> ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'MyNewPass!';

 

ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '1Q#!bURw68Vw';

 ALTER USER 'root'@'%' IDENTIFIED BY '1Q#!bURw68Vw';

 

alter user user() identified by '1Q#!bURw68Vw';

——————————————————————————


更改cptl_dev用户密码
——————————————————————————

update mysql.user set authentication_string=password('NSqVVBBS6Qd9') where user='cptl_dev' and Host = '%';

NSqVVBBS6Qd9

 

mysql5.7 更改root密码

update mysql.user set authentication_string=password('1Q#!bURw68Vw') where user='root' and Host = '%';

 

update mysql.user set authentication_string=password('1Q#!bURw68Vw') where user='root' and host='%';



——————————————————————————

mysqlforum ,innodb_lru_scan_depth

changing innodb_lru_scan_depth value to 256 have improved the insert queries execution time + no warning message in log, the default was innodb_lru_scan_depth=1024;

SET GLOBAL innodb_lru_scan_depth=256;


——————————————————————————

岩岩授权 122 2018年 05月 10日 星期四 20:41:17 CST


grant select, update on activiti_ys.* to scf_ys_yy@'%';

grant select, update on core_ys.* to scf_ys_yy@'%';

grant insert on activiti_ys.* to scf_ys_yy@'%';
grant insert on core_ys.* to scf_ys_yy@'%';

__________________________________________________

Centos7安装MySQL

1、安装repo源
yum install http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm

2、安装软件

grant all privileges on *.* to root@'%' identifyed by 'Pa$$w0rd2';

 

grant all privileges on *.* to root@'%' identified by '1Q#!bURw68Vw';

__________________________________________________

数据库授权

MariaDB [mysql]> grant all privileges on *.* to 'root'@'123.57.240.208' identified by 'yishiyun<>?' with grant option;


__________________________________________________

[root@opbj01 ops]# mysql -uyizhibo -p"J(ae=s/lo8Bs." -h118.26.135.132

___________________________________________________

查询结果显示100条记录

MariaDB [stat]> select * from train_status limit 100 \G;

Oracle > select * from train_status where rownum<=100;

____________________________________________________

mysql 用户授权

默认情况下 mysql 是以root 用户进入 并且操作的,那么要想以其他用户进入并且对数据库进行增删改查的工作那么就需要 以root 用户的身份进入 给数据库授权 给 相应的用户

server0:/usr/local/mysql/bin # mysql -uroot -p
Enter password:

mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| solrclient |
| test |
+--------------------+

mysql> grant all privileges on solrclient.* to suse@localhost identified by 'suse‘;
Query OK, 0 rows affected (0.00 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
mysql>\q

还可以进行更细粒度的 授权:

//如果想指定部分权限给一用户,可以这样来写:
mysql>grant select,update on solrDB.* to suse@localhost identified by ‘suse’;

3.删除用户
  @>mysql -u root -p
  @>密码
  mysql>DELETE FROM user WHERE User='suse'and Host=”localhost”;
  mysql>flush privileges;
  //删除用户的数据库
  mysql>drop database solrDB;
  4.修改指定用户密码
  @>mysql -u root -p
  @>密码
  mysql>update mysql.user set password=password(‘新密码’) where User='suse' and Host=”localhost”;
  mysql>flush privileges;
  mysql>quit;

在MySQL 5.7版本中,备份迁移数据库的时候,还原时提示如下报错信息 
ERROR 1840 (HY000) at line 24: @@GLOBAL.GTID_PURGED can only be set when @@GLOBAL.GTID_EXECUTED is empty

当前GTID_EXECUTED参数已经有值

解决方法:

方法一:reset mater

这个操作可以将当前库的GTID_EXECUTED值置空

方法二:--set-gtid-purged=off

在dump导出时,添加--set-gtid-purged=off参数,避免将gtid信息导出

mysqldump -uroot -p --set-gtid-purged=off -d yoon > yoon.sql


____________________________________________________

NUMA对MySQL性能的影响
我们现在使用的PC服务器都是NUMA架构的,下图是Intel 5600 CPU的架构:NUMA的内存分配策略有四种:
1.缺省(default):总是在本地节点分配(分配在当前进程运行的节点上);
2.绑定(bind):强制分配到指定节点上;
3.交叉(interleave):在所有节点或者指定的节点上交织分配;
4.优先(preferred):在指定节点上分配,失败则在其他节点上分配。
因为NUMA默认的内存分配策略是优先在进程所在CPU的本地内存中分配,会导致CPU节点之间内存分配不均衡,当某个CPU节点的内存不足时,会导致swap产生,而不是从远程节点分配内存。这就是所谓的swap insanity现象。
MySQL采用了线程模式,对于NUMA特性的支持并不好,如果单机只运行一个MySQL实例,我们可以选择关闭NUMA,关闭的方法有三种:1.硬件层,在BIOS中设置关闭;2.OS内核,启动时设置numa=off;3.可以用numactl命令将内存分配策略修改为interleave(交叉),有些硬件可以在BIOS中设置。
如果单机运行多个MySQL实例,我们可以将MySQL绑定在不同的CPU节点上,并且采用绑定的内存分配策略,强制在本节点内分配内存,这样既可以充分利用硬件的NUMA特性,又避免了单实例MySQL对多核CPU利用率不高的问题。
资源隔离方案
1.CPU,Memory
numactl –cpubind=0 –localalloc,此命令将MySQL绑定在不同的CPU节点上,cpubind是指NUMA概念中的CPU节点,可以用numactl –hardware查看,localalloc参数指定内存为本地分配策略。
2.IO
我们在机器中内置了fusionio卡(320G),配合flashcache技术,单机的IO不再成为瓶颈,所以IO我们采用了多实例共享的方式,并没有对IO做资源限制。多个MySQL实例使用相同的物理设备,不同的目录的来进行区分。
3.Network
因为单机运行多个实例,必须对网络进行优化,我们通过多个的IP的方式,将多个MySQL实例绑定在不同的网卡上,从而提高整体的网络能力。还有一种更高级的做法是,将不同网卡的中断与CPU绑定,这样可以大幅度提升网卡的效率。
4.为什么不采用虚拟机
虚拟机会耗费额外的资源,而且MySQL属于IO类型的应用,采用虚拟机会大幅度降低IO的性能,而且虚拟机的管理成本比较高。所以,我们的数据库都不采用虚拟机的方式。

____________________________________________________

# mysqladmin -uroot -p variables

方法一:
mysqld_safe 中加入2处 -O max_connections=1000

$NOHUP_NICENESS $ledir/$MYSQLD $defaults –basedir=$MY_BASEDIR_VERSION –datadir=$DATADIR $USER_OPTION –pid-file=$pid_file –skip-locking -O max_connections=1000 >> $err_log 2>&1

eval “$NOHUP_NICENESS $ledir/$MYSQLD $defaults –basedir=$MY_BASEDIR_VERSION –datadir=$DATADIR $USER_OPTION –pid-file=$pid_file –skip-locking $args -O max_connections=1000 >> $err_log 2>&1″


方法二:
mysql>show variables;
Linux VI 命令/etc/my.cnf
[mysqld]
set-variable=max_connections=250 #加入这些内容
:wq

/etc/init.d/mysqld restart


_____________________________________________________

  mysql -u root

  mysql> use mysql;

  mysql> UPDATE user SET Password = PASSWORD('newpass') WHERE user = 'root';

  mysql> FLUSH PRIVILEGES;

_____________________________________________________

## backup all databases
# mysqldump -uroot -p123456 --all-databases --events > all_0520.sql

 

#mysqldump -h10.17.10.233 -P30003 -uroot -p'1Q#!bURw68Vw'  --all-databases --triggers --routines --events > `date +%F`.sql


(3) 使用mysqldump
使用mysqldump来得到一个数据快照可分为以下几步:
<1>锁表:如果你还没有锁表,你应该对表加锁,防止其它连接修改数据库,否则,你得到的数据可以是不一致的。如下:
mysql> FLUSH TABLES WITH READ LOCK;
<2>在另一个连接用mysqldump创建一个你想进行复制的数据库的转储:
shell> mysqldump --all-databases --lock-all-tables >dbdump.db
<3>对表释放锁。
mysql> UNLOCK TABLES;



_____________________________________________________

>grant all privileges on *.* to 'dba'@'%' identified by '123456';

rant all privileges on *.* to 'root'@'%' identified by 'syspassword';

_____________________________________________________

查询优化


mysql> show variables where variable_name like '%slow%' \G;



下面说下几个参数的含义

-a, --analyze 分析 [Analyze given tables]
-o, --optimize 优化 [Optimize table]
-A, --all-databases 所有的数据库 [Check all the database]
--auto-repair 自动修复 [If a checked table is corrupted, automatically fix it. Repairing will be done after all tables have been checked, if corrupted ones were found]

Linux VI 命令/opt/shell/mysql_opt.sh/usr/local/mysql/bin/mysqlcheck -Aao -auto-repair -uroot -p


加入 crontab

* * */1 * * /opt/shell/mysql_opt.sh

______________________________________________________

mysql> SHOW VARIABLES LIKE '%partition%';

mysql> SHOW PLUGINS;


______________________________________________________

root>show global variables like 'wait_timeout';

查看mysql5的手册,发现对wait_timeout的最大值分别是24天/365天(windows/linux)。以windows为例,假设我们要将其设为21天,我们只要修改mysql5的配置文件“my.ini”(mysql5 installation dir),增加一行:
wait_timeout=1814400

需要重新启动mysql5。

linux系统配置文件:/etc/my.cnf

测试显示问题解决了。
wait_timeout=1814400
__________________________________________________________________
lower_case_table_names=1
default-character-set=utf8
wait_timeout=1814400
skip-name-resolve
set-variable=max_connections=1000




create database if not exists wxwall default character set utf8;
grant all privileges on xiunobbs.* to bbsuser@'%' identified by 'bbspassw0rd';

>create database if not exists dcms default character set utf8;
>create database if not exists ocrs default character set utf8;
>create database if not exists cvms default character set utf8;

> create database if not exists xwiki default character set utf8;
> grant all privileges on xwiki.* to xwiki@'localhost' identified by 'Pa$$w0rd';

> grant all privileges on dcms.* to dcms@'localhost' identified by '123456';
>grant all privileges on ocrs.* to ocrs@'localhost' identified by '123456';
>grant all privileges on cvms.* to cvms@'localhost' identified by '123456';


_____________________________________________________________________

create database if not exists ire default character set utf8;

>create database if not exists nonstop default character set utf8;
>grant all privileges on nonstop.* to 'dba'@'%' identified by '123456';

create database if not exists phpcms default character set utf8;

grant all privileges on phpcms.* to 'phpcmsdb'@'%' identified by 'phpcmspassword';


____________________________________________________________________
# mysql -usvnuser -p123456 svnadmin < mysql5.sql
>create database if not exists svnadmin default character set utf8;
>grant all privileges on svnadmin.* to svnuser@'localhost' identified by '123456';
____________________________________________________________________

UPDATE user SET Password = password ( '123456' ) WHERE User = 'root' ;

grant all privileges on confluence.* to 'confluence'@'%' identified by 'Conf1uence@';




mysql> grant all privileges on *.* to 'root'@'%' identified by '123456';

mysql> grant all privileges on ocrs.* to ocrs identified by '123456';

mysql> grant all privileges on cvms.* to cvms identified by '123456';

mysql> grant all privileges on dcms.* to dcms identified by '123456';





grant all privileges on its.* to its@'localhost' identified by '08558de3f95dacef7eae69a072366633';



___________________________________________________________________
create database if not exists confluence default character set utf8;

ITS 库

mysql> show variables like 'character_set_%';

mysql> set character_set_results = utf8;

mysql> select * from perm_function_group;

mysql> select * from perm_role_function;

mysql> create database if not exists its3 default character set utf8;

mysql> create user its3;

mysql> grant all on *.* to its3@'%';

mysql> select user, host, password from user;

mysql> update user set password="*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9" where user='its3';

mysql> flush privileges;



___________________________________________________________________

MySQL Replication



________________________________

yum 使用本地源:

yum --disablerepo=\* --enablerepo=c6-media -y groupinstall "Virtualization Tools"

_________________________________

 想解决linux命令行乱码,只要打开/etc/sysconfig/i18n这个文件,在里面加上如下代码即可:
  #LANG="zh_CN.UTF-8"
  #SYSF
  LANG=zh_CN.GB18030
  LANGUAGE=en_US.UTF-8
  LC_CTYPE=zh_CN.GB18030
  LC_TIME=en_US.UTF-8
  以上是linux命令行乱码的解决方式,希望对您有所帮助。

__________________________________

Linuxiptables 开放Mysql端口允许远程访问

修改防火墙配置文件:
Linux VI 命令/etc/sysconfig/iptables

增加下面一行:

-A RH-Firewall-1-INPUT -m state –state NEW -m tcp -p tcp –dport 3306 -j ACCEPT
如果想开通21等端口,只需要将3306换成21等要开放的端口就可以了。
配置后,重新启动iptable
service iptables restart
这时就可以从其他机器访问Mysql了。


注意:
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
-A RH-Firewall-1-INPUT -j REJECT --reject-with icmp-host-prohibited

-A RH-Firewall-1-INPUT -j REJECT --reject-with icmp-host-prohibited
是拒绝icmp访问,对于其它的报文返回一个主机禁止访问的错误

开通3306 端口的行必须在icmp-host-prohibited前

__________________________________

主:192.168.128.10
从:192.168.128.11
VIP:192.168.128.20

1, mysql配置
主服务器:
[root@localhost ~]# yum install mysql-server mysql
[root@localhost ~]# /etc/init.d/mysqld start
mysql> grant replication slave,file on *.* to 'mysql1'@'192.168.128.11' identified by '123456';
mysql> grant all privileges on *.* to 'root'@'%' identified by '111111'; #开启远程连接
[root@localhost ~]# /etc/init.d/mysqld stop
[root@localhost ~]# Linux VI 命令/etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
old_passwords=1

###以下红色部分为新添加
log-bin=mysql-bin
server-id=1
binlog-do-db=test
binlog-ignore-db=mysql
replicate-do-db=test
replicate-ignore-db=mysql
log-slave-updates
slave-skip-errors=all
sync_binlog=1
auto_increment_increment=2
auto_increment_offset=1

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
[root@localhost ~]# /etc/init.d/mysqld start
[root@localhost ~]# mysql
mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 | 98 | test | mysql |
+------------------+----------+--------------+------------------+

从服务器:
[root@localhost ~]# yum install mysql-server mysql
[root@localhost ~]# /etc/init.d/mysqld start
mysql> grant replication slave,file on *.* to 'mysql2'@'192.168.128.10' identified by '123456';
mysql> grant all privileges on *.* to 'root'@'%' identified by '111111'; #开启远程连接
[root@localhost ~]# /etc/init.d/mysqld stop
[root@localhost ~]# Linux VI 命令/etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
old_passwords=1

###以下红色部分为新添加
log-bin=mysql-bin
server-id=2
binlog-do-db=test
binlog-ignore-db=mysql
replicate-do-db=test
replicate-ignore-db=mysql
log-slave-updates
slave-skip-errors=all
sync_binlog=1
auto_increment_increment=2
auto_increment_offset=2

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
[root@localhost ~]# /etc/init.d/mysqld start
[root@localhost ~]# mysql
mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 | 98 | test | mysql |
+------------------+----------+--------------+------------------+

主服务器:
[root@localhost ~]# /etc/init.d/mysqld start
mysql> flush tables with read lockG
mysql> show master statusG
mysql> change master to
->master_host='192.168.128.11',
->master_user='mysql2',
->master_password='123456',
->master_log_file='mysql-bin.000001' ,
### master_log_file需要在另一台上执行show master status查看
->master_log_pos=98;

mysql> start slave;
mysql> show slave statusG
以下两项为yes则成功
Slave_IO_Running: Yes
Slave_SQL_Running: Yes

从服务器:
[root@localhost ~]# /etc/init.d/mysqld start
mysql> flush tables with read lockG
mysql> show master statusG
mysql> change master to
->master_host='192.168.128.10',
->master_user='mysql1',
->master_password='123456',
->master_log_file='mysql-bin.000001',
### master_log_file需要在另一台上执行show master status查看
->master_log_pos=98;

mysql> start slave;
mysql> show slave statusG
以下两项为yes则成功
Slave_IO_Running: Yes
Slave_SQL_Running: Yes

2, mysql主主同步测试
1) 主服务器创建表,查看从服务器;
2) 主服务器插入数据,查看从服务器;
3) 从服务器插入数据,查看主服务器;
4) 远程root登陆测试;


3,keepalived安装(主从安装相同)
[root@localhost ~]# uname -r
2.6.18-238.el5
[root@localhost ~]# ln -s /usr/src/kernels/2.6.18-238.el5-i686//usr/src/linux
[root@localhost ~]# cd /usr/local/src/
[root@localhost src]# tar zxf keepalived-1.2.1.tar.gz
[root@localhost src]# cd keepalived-1.2.1
[root@localhost keepalived-1.2.1]# Linux VI 命令keepalived/libipvs-2.6/ip_vs.h
[root@localhost keepalived-1.2.1]# ./configure
[root@localhost keepalived-1.2.1]# make
[root@localhost keepalived-1.2.1]# make install

2,拷贝程序文件
[root@localhost keepalived-1.2.1]# cp /usr/local/etc/rc.d/init.d/keepalived/etc/init.d/
[root@localhost keepalived-1.2.1]# cp /usr/local/etc/sysconfig/keepalived/etc/sysconfig/
[root@localhost keepalived-1.2.1]# mkdir /etc/keepalived
[root@localhost keepalived-1.2.1]# cp /usr/local/etc/keepalived/keepalived.conf/etc/keepalived
[root@localhost keepalived-1.2.1]# cp /usr/local/sbin/keepalived/usr/sbin/

3,修改keepalived.conf配置文件
[root@localhost keepalived-1.2.1]# cd /etc/keepalived/
[root@localhost keepalived]# Linux VI 命令keepalived.conf
global_defs {
router_id Haweb_1
}

vrrp_sync_group VGM {
group {
Linux VI 命令_HA
}
}

vrrp_script chk_mysql {
script "killall -0 mysqld" #检测mysqld进程,也可以替换成检测脚本
interval 5
}

vrrp_instance Linux VI 命令_HA {
state MASTER ###从为BACKUP
interface eth0
virtual_router_id 51
priority 100 ###从为99
advert_int 5
authentication {
auth_type PASS
auth_pass 111
}

virtual_ipaddress {
192.168.128.20/24 dev eth0
}

track_script {
chk_mysql #执行上面的命令
}

}
[root@localhost ~]# /etc/init.d/keepalived start #主从同时启动
[root@localhost ~]# tail -f /var/log/messages #查看日志启动信息
[root@localhost ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
link/ether 00:0c:29:05:38:cd brd ff:ff:ff:ff:ff:ff
inet 192.168.128.10/24 brd 192.168.128.255 scope global eth0
inet 192.168.128.20/24 scope global secondary eth0

4,keepalived测试
1),停止主服务器keepalived,检查VIP是否切换到从服务器;
重启主服务器keepalived,VIP再次由主服务器接管;
2),停止主服务器mysql,检查VIP是否切换到从服务器;
重启主服务器mysql,VIP再次由主服务器接管


___________________________________

server.modules = (
"mod_rewrite",
"mod_redirect",
"mod_alias",
"mod_fastcgi",
"mod_simple_vhost",
"mod_evhost",
"mod_cgi",
"mod_accesslog" )
server.document-root = "/var/www/" #web访问的根目录
server.errorlog = "/usr/local/lighttpd/log/error.log" #错误日志
index-file.names = ( "index.php", "index.html",
"index.htm", "default.htm" ) #支持格式
mimetype.assign = (
".pdf" => "application/pdf",
".sig" => "application/pgp-signature",
".spl" => "application/futuresplash",
".class" => "application/octet-stream",
".ps" => "application/postscript",
".torrent" => "application/x-bittorrent",
".dvi" => "application/x-dvi",
".gz" => "application/x-gzip",
".pac" => "application/x-ns-proxy-autoconfig",
".swf" => "application/x-shockwave-flash",
".tar.gz" => "application/x-tgz",
".tgz" => "application/x-tgz",
".tar" => "application/x-tar",
".zip" => "application/zip",
".mp3" => "audio/mpeg",
".m3u" => "audio/x-mpegurl",
".wma" => "audio/x-ms-wma",
".wax" => "audio/x-ms-wax",
".ogg" => "application/ogg",
".wav" => "audio/x-wav",
".gif" => "image/gif",
".jar" => "application/x-java-archive",
".jpg" => "image/jpeg",
".jpeg" => "image/jpeg",
".png" => "image/png",
".xbm" => "image/x-xbitmap",
".xpm" => "image/x-xpixmap",
".xwd" => "image/x-xwindowdump",
".css" => "text/css",
".html" => "text/html",
".htm" => "text/html",
".js" => "text/javascript",
".asc" => "text/plain",
".c" => "text/plain",
".cpp" => "text/plain",
".log" => "text/plain",
".conf" => "text/plain",
".text" => "text/plain",
".txt" => "text/plain",
".dtd" => "text/xml",
".xml" => "text/xml",
".mpeg" => "video/mpeg",
".mpg" => "video/mpeg",
".mov" => "video/quicktime",
".qt" => "video/quicktime",
".avi" => "video/x-msvideo",
".asf" => "video/x-ms-asf",
".asx" => "video/x-ms-asf",
".wmv" => "video/x-ms-wmv",
".bz2" => "application/x-bzip",
".tbz" => "application/x-bzip-compressed-tar",
".tar.bz2" => "application/x-bzip-compressed-tar",
"" => "application/octet-stream",
)

url.access-deny = ( "~", ".inc" )
$HTTP["url"] =~ "\.pdf$" {
server.range-requests = "disable"
}
static-file.exclude-extensions = ( ".php", ".pl", ".fcgi" )
server.port = 80 #监听端口
server.bind = "192.168.6.106" #绑定IP
server.pid-file = "/var/run/lighttpd.pid"
server.username = "www-data" #运行服务用户
server.groupname = "www-data" #运行服务用户组
fastcgi.server = ( ".php" =>
( "localhost" =>
(
"socket" => "/usr/local/lighttpd/run/php-fastcgi.socket",
"bin-path" => "/usr/local/php/bin/php-cgi" #php-cgi
)
)
)
server.max-fds = 20480 # 文件描述符数目
server.max-keep-alive-requests = 0 #保持会话连接
server.network-backend = "linux-sendfile"
server.event-handler = "linux-sysepoll"



____________________________________


____________________________________________________________________

配置Master

例子【2-1】:将配置选项添加到my.cnf以配置Master

[mysqld]
user = mysql
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
port = 3306
basedir = /usr
datadir = /var/lib/mysql
tepdir = /tmp

log-bin = master-bin
log-bin-index = master-bin.index
server-id = 1

例子【2-2】:在Master上创建一个复制用户

master> create user repl_user;
Query OK, 0 rows affected (0.00 sec)

master> grant replication slave,file on *.*
-> to repl_user identified by 'password';
Query OK, 0 rows affected (0.00 sec)

gemfire

配置Slave

例子【2-3】:添加选项到my.cnf文件来配置Slave

[mysqld]
user = mysql
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
port = 3306
basedir = /usr
datadir = /var/lib/mysql
tmpdir = /tmp

server-id = 2
relay-log-index = slave-relay-bin.index
relay-log = slave-relay-bin

连接Master和Slave
需要知道master的4部分信息

  • 端口号
  • Master上拥有replication slave权限的用户名
  • 该用户的密码
  • 主机名



slave> change master to
-> master_host = 'master-1',
-> master_port = 3306,
-> master_user = 'repl_user',
-> master_password = 'password';

Query OK, 0 rows affected (0.00 sec)

slave> start slave;
Query OK, 0 rows affected (0.15 sec)


备注:
* 执行 flush logs 命令 (或任何 flush命令)需要 reload权限
* 执行 show master status 和 show slave status 命令需要super 或者replication client权限
* 执行change master to 命令需要super 权限

例如,赋予足够的权限给mats用户来执行以上过程,使用如下命令:
Server> grant repliction slave, reload, create user, super
-> on *.*
-> to mats@'192.168.3.%'
-> with grant option;


_____________________________________________________________________________________________

 1. 修改mysql最大连接数:cp support-files/my-medium.cnf ./my.cnf,vim my.cnf,增加或修改max_connections=1024

  关于my.cnf:mysql按照下列顺序搜索my.cnf:/etc,mysql安装目录,安装目录下的data。/etc下的是全局设置。

  2. 启动mysql:/usr/local/mysql/bin/mysqld_safe --user=mysql &

    查看mysql版本:mysqladmin -u root -p version

    注:网上安装或者二进制安装的可以直接使用如下命令启动和停止mysql: /etc/init.d/mysql start|stop|restart

  3. 停止mysql:mysqladmin -uroot -ppassw0rd shutdown 注意,u,p后没有空格

  4. 设置mysql自启动:把启动命令加入/etc/rc.local文件中

  5. 允许root远程登陆:

    1)本机登陆mysql:mysql -u root -p (-p一定要有);改变数据库:use mysql;

    2)从所有主机:grant all privileges on *.* to root@"%" identified by "passw0rd" with grant option;

    3)从指定主机:grant all privileges on *.* to root@"192.168.11.205" identified by "passw0rd" with grant option; flush privileges;

    4) 进mysql库查看host为%的数据是否添加:use mysql; select * from user;

  6. 创建数据库,创建user:

    1) 建库:create database test1;

    2) 建用户,赋权:grant all privileges on test1.* to user_test@"%" identified by "passw0rd" with grant option;

    3)删除数据库:drop database test1;

  7. 删除权限:

    1) revoke all privileges on test1.* from test1@"%";

    2) use mysql;

    3) delete from user where user="root" and host="%";

    4) flush privileges;

  8. 显示所有的数据库:show databases; 显示库中所有的表:show tables;

  9. 远程登录mysql:mysql -h ip -u user -p

  10. 设置字符集(以utf8为例):

    1) 查看当前的编码:show variables like 'character%';

    2) 修改my.cnf,在[client]下添加default-character-set=utf8

    3) 在[server]下添加default-character-set=utf8,init_connect='SET NAMES utf8;'

    4) 重启mysql。

    注:只有修改/etc下的my.cnf才能使client的设置起效,安装目录下的设置只能使server的设置有效。

      二进制安装的修改/etc/mysql/my.cnf即可

  11. 旧数据升级到utf8(旧数据以latin1为例):

    1) 导出旧数据:mysqldump --default-character-set=latin1 -hlocalhost -uroot -B dbname --tables old_table >old.sql

    2) 转换编码(Linux和UNIX):iconv -t utf-8 -f gb2312 -c old.sql > new.sql

      这里假定原表的数据为gb2312,也可以去掉-f,让iconv自动判断原来的字符集。

    3) 导入:修改new.sql,在插入或修改语句前加一句话:"SET NAMES utf8;",并修改所有的gb2312为utf8,保存。

      mysql -hlocalhost -uroot -p dbname < new.sql

      如果报max_allowed_packet的错误,是因为文件太大,mysql默认的这个参数是1M,修改my.cnf中的值即可(需要重启mysql)。

  12. 支持utf8的客户端:Mysql-Front,Navicat,PhpMyAdmin,LinuxShell(连接后执行SET NAMES utf8;后就可以读写utf8的数据了。10.4设置完毕后就不用再执行这句话了)

  13. 备份和恢复

    备份单个数据库:mysqldump -uroot -p -B dbname > dbname.sql

    备份全部数据库:mysqldump -uroot -p --all-databases > all.sql

    备份表: mysqldump -uroot -p -B dbname --table tablename > tablename.sql

    恢复数据库:mysql -uroot -p < name.sql

    恢复表:mysql -uroot -p dbname < name.sql (必须指定数据库)

  14. 复制

    Mysql支持单向的异步复制,即一个服务器做主服务器,其他的一个或多个服务器做从服务器。复制是通过二进制日志实现的,主服务器写入,从服务器读取。可以实现多个主    服务器,但是会碰到单个服务器不曾遇到的问题(不推荐)。

    1). 在主服务器上建立一个专门用来做复制的用户:grant replication slave on *.* to 'replicationuser'@'192.168.0.87' identified by 'iverson';

    2). 刷新主服务器上所有的表和块写入语句:flush tables with read lock; 然后读取主服务器上的二进制二进制文件名和分支:SHOW MASTER STATUS;将File和Position的值记录下来。记录后关闭主服务器:mysqladmin -uroot -ppassw0rd shutdown

      如果输出为空,说明服务器没有启用二进制日志,在my.cnf文件中[mysqld]下添加log-bin=mysql-bin,重启后即有。

    3). 为主服务器建立快照(snapshot)

      需要为主服务器上的需要复制的数据库建立快照,Windows可以使用zip格式,Linux和Unix最好使用tar命令。然后上传到从服务器mysql的数据目录,并解压。

      cd mysql-data-dir

      tar cvzf mysql-snapshot.tar ./mydb

      注意:快照中不应该包含任何日志文件或*.info文件,只应该包含要复制的数据库的数据文件(*.frm和*.opt)文件。

      可以用数据库备份(mysqldump)为从服务器做一次数据恢复,保证数据的一致性。

    4). 确认主服务器上my.cnf文件的[mysqld]section包含log-bin选项和server-id,并启动主服务器:

      [mysqld]

      log-bin=mysql-bin

      server-id=1

    5). 停止从服务器,加入server-id,然后启动从服务器:

      [mysqld]

      server-id=2

      注:这里的server-id是从服务器的id,必须与主服务器和其他从服务器不一样。

      可以在从服务器的配置文件中加入read-only选项,这样从服务器就只接受来自主服务器的SQL,确保数据不会被其他途经修改。

    6). 在从服务器上执行如下语句,用系统真实值代替选项:

      change master to MASTER_HOST='master_host', MASTER_USER='replication_user',MASTER_PASSWORD='replication_pwd',

        MASTER_LOG_FILE='recorded_log_file_name',MASTER_LOG_POS=log_position;

    7). 启动从线程:mysql> START SLAVE; 停止从线程:stop slave;(注意:主服务器的防火墙应该允许3306端口连接)

    验证:此时主服务器和从服务器上的数据应该是一致的,在主服务器上插入修改删除数据都会更新到从服务器上,建表,删表等也是一样的。

以下是几个有用的连接:


http://publish.it168.com/2006/0203/20060203001301.shtml?cChanNel=11&cpositioncode=296&hezuo=107

grant

___________________________________________________________________________________________________________________
1)、添加复制用户rep_user
mysql>grant replication slave,file on *.* to 'rep_user'@'%' identified by 'test1234';
mysql> flush privileges;

2)、修改主库配置文件
### addeb by maguirun.
log-bin=mysql-bin
server-id=1
binlog-do-db=test
binlog-ignore-db=mysql
replicate-do-table=test.t1
replicate-do-table=test.t3
replicate-ignore-db=mysql
log-slave-updates
slave-skip-errors=all
sync_binlog=1
auto_increment_increment=2
auto_increment_offset=1

3)、从库添加同步关联
change master to master_host='db1.ctvit.com.cn', master_user='rep_user', master_password='test1234', master_log_file='mysql-bin.000002', master_log_pos=98;


__________________________________________________________________________________________________________________________

Waiting for master to send event//已经连上
主服务器上的相关命令:
show master status
show slave hosts
show logs
show binlog events
purge logs to 'log_name'
purge logs before 'date'
reset master(老版本flush master)
set sql_log_bin=

从服务器上的相关命令:
slave start
slave stop
SLAVE STOP IO_THREAD //此线程把master段的日志写到本地
SLAVE start IO_THREAD
SLAVE STOP SQL_THREAD //此线程把写到本地的日志应用于数据库
SLAVE start SQL_THREAD
reset slave
SET GLOBAL SQL_SLAVE_SKIP_COUNTER
load data from master
show slave status(SUPER,REPLICATION CLIENT)
CHANGE MASTER TO MASTER_HOST=, MASTER_PORT=,MASTER_USER=, MASTER_PASSWORD= //动态改变master信息

__________________________________________________________________________________________________________________________

MySQL [(none)]>
MySQL [(none)]>
MySQL [(none)]>
MySQL [(none)]> GRANT ALL PRIVILEGES ON *.* TO k17m1@localhost IDENTIFIED BY '25SEW9GrV39f&3kBK^7#HBFeV';
Query OK, 0 rows affected (0.00 sec)

MySQL [(none)]> GRANT ALL PRIVILEGES ON *.* TO k17m1@127.0.0.1 IDENTIFIED BY '25SEW9GrV39f&3kBK^7#HBFeV';
Query OK, 0 rows affected (0.00 sec)

MySQL [(none)]> GRANT ALL PRIVILEGES ON *.* TO k17s1@localhost IDENTIFIED BY 'ytXKNVpsWyZSgqXW#z%&CYre1';
Query OK, 0 rows affected (0.00 sec)

MySQL [(none)]> GRANT ALL PRIVILEGES ON *.* TO k17s1@127.0.0.1 IDENTIFIED BY 'ytXKNVpsWyZSgqXW#z%&CYre1';
Query OK, 0 rows affected (0.00 sec)

MySQL [(none)]> GRANT ALL PRIVILEGES ON *.* TO devop@localhost IDENTIFIED BY 'trobURgcV4E%&LH#*viLCDZw0';
Query OK, 0 rows affected (0.00 sec)

MySQL [(none)]> GRANT ALL PRIVILEGES ON *.* TO devop@127.0.0.1 IDENTIFIED BY 'trobURgcV4E%&LH#*viLCDZw0';
Query OK, 0 rows affected (0.00 sec)

MySQL [(none)]>

___________________________________________________________________________________________________________________________

kubernetes yaml 文件定义
文档导航