mysql 操作命令 master-to-master

1.apt-get install mysql-client libmysqlclient-dev
2.apt-get install libxml2-dev libxslt1-dev imagemagick libmagickwand-dev

按中文拼音排序:

select  * from product order by CONVERT(NAME USING gbk) DESC 
 
1.重启服务
service mysql start
/etc/init.d/mysql restart
2.mysql路径
/var/log/mysql 错误日志存放路径
/etc/mysql   mysql配置文件存放路径
/var/lib/mysql  数据存放路径

 

 

1.select语句中把两个字符串字段合并成一个字段:

select concat(a, b) as c from tables

 

2.按日期分组查询

select DATE_FORMAT( created_at, '%Y-%m-%d %H' )  as t from tables group by t

 

 

3.mysql创建用户/添加远程/修改用户密码

先进入mysql命令:mysql -uroot -proot

->create user username@'192.168.39.%' IDENTIFIED BY 'password';

配置远程

->GRANT REPLICATION SLAVE,RELOAD,SUPER ON *.* TO username@'192.168.39.%' IDENTIFIED BY 'password';

修改密码

->update mysql.user set password=PASSWORD('xxx') where user='username';

->flush privileges;

 

4.master-to-master

先配置远程:

GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO username@'192.168.39.%' IDENTIFIED BY 'password';

建立master:

->slave stop;

->change master to master_host='192.168.0.2',

->master_user='backup',

->master_password='back',

->master_log_file='mysql-bin.000001',

->master_log_pos=106;

 

->slave start;

->show slave status\G

 

4.查看server_id,注意master-to-master时,两个服务器的server_id不能一样,需要修改。

->show variables like 'server_id';

修改server_id

->set global server_id=10;

 

 

5.查看auto_increment_increment与auto_increment_offset值:

->SHOW VARIABLES LIKE 'auto_inc%';

 

6.两个服务器master-to-master,必须设置auto_increment_offset值为不同,如下:

第一台:

auto_increment_increment =2  #为开始值

auto_increment_offset = 1

第二台:

auto_increment_increment =2  #为开始值

auto_increment_offset = 2

 

7.mysql远程链接权限有以下14个:

select,insert,update,delete,create,drop,index,alter,grant,references,reload,shutdown,process,file

 

8.重起mysql服务

 

1、使用 service 启动:service mysqld start

 

2、使用 mysqld 脚本启动:/etc/init.d/mysql start

 重启 : /etc/init.d/mysql restart

3、使用 safe_mysqld 启动:safe_mysqld&

 

 

 

mysql5.5配置 master-slave

master:

//至少要有server-id、与log-bin两项
server-id=1 
log-bin=/var/lib/mysql/mysql-bin   
datadir=/var/lib/mysql
//为了使用事务的InnoDB在复制中最大的持久性和一致性,你应该指定innodb_flush_log_at_trx_commit=1,sync_binlog=1选项
innodb_flush_log_at_trx_commit=1 
sync_binlog=1

 

在主机Master创建一个有复制权限的用户

mysql>create user [email protected];

mysql>grant replication slave on *.* to [email protected] identified by '123456';

锁住主机,记录二进制日志的位置(后面设置从机的时候会用到)

mysql>flush tables with read lock;

mysal>show master status;

File:mysql-bin.000013  Position:7863951

复制主机数据库(test)到从机

shell>/usr/bin/mysqldump test -uroot  -padmin --opt | mysql test -uroot -padmin -h 192.168.1.3

重新打开主机Master写操作功能

mysql>unlock tables;

 

 

slave:

server-id=2 //必须是1到2的32次方之间的一个整数,且必须和主机Master的server-id不一样
relay-log-index=slave-relay-bin.index
relay-log=slave-relay-bin
replicate-do-db=test //指定复制数据库
datadir=/var/lib/mysql

 

删除用户

drop user 用户名@'%';

drop user 用户名@localhost;

你可能感兴趣的:(master)