天天看点

mysql二进制版本安装以及主从服务的搭建

简单的笔记:  去mysql官网下载5.5的二进制版本,wget -c +url下载tar.gz,我这里下载的是64位5.5版本的包,先简单的说下安装,其实这些在mysql文档上都有,只不过每个人安装的时候可能遇到不同的问题,看下.err文件,会有问题提示。为了方便搭建主从,我在同一台机器上,安装两个mysql。假设这里的路径分别为:/usr/local/mysql01 ,而slave的库路径为/usr/slave,安装如下,将已经下载的tar.gz包cp到上述目录下。 以mysql01为例,   1.tar zxvf 解压缩        2.建立软连接        执行ln -s *.tar.gz mysql在当前文件夹建立软连接(不需要完全按照mysql文档上的路径来,甚至运行时文件路径等,都可以指定,不使用默认)

mysql二进制版本安装以及主从服务的搭建

如图,浅色的即为mysql软连接 3.添加msyql用户用户组,指令如下:    shell> groupadd mysql    shell> useradd -r -g mysql -s /bin/false mysql   如果提示已存在 说明装过mysql,已经添过,可以继续进行后面的步骤 4.之后cd到我们的mysqll目录,进行权限的修改     chown -R mysql .           //修改所属用户为mysql     chgrp -R mysql .             //修改所属用户组为mysql  5 调用./bin/scripts/mysql_install_db --user=mysql 进行初始化 6.再次进行权限的修改    shell> chown -R root .    shell> chown -R mysql data  7.拷贝配置文件(如果需要,copy数据库data文件)   

mysql二进制版本安装以及主从服务的搭建

   如图,在support-files文件夹下有很多文件,其中my-small.cnf是用于小型库,mysql-large.cnf是大型库,文档上拷贝的是my-medium.cnf,执行以下命令京配置文件拷贝当安装的当前目录 cp ./support-files/my-medium.cnf my.cnf 当需要修改配置文件的时候,可与su 到root权限,或者直接chmod 777 my.cnf修改文件权限,但是之后需要修改为644 不然mysql不会启动。   注:当mysql安装的时候,data的数据库文件是在/var/lib/mysql/下,如果一台机器上安装多个实例,建议mv 此文件夹到安装目录(rm掉以前的data文件夹) 8.启动 ./bin/mysqld_safe --user=mysql & 下面是一些自定义的选项:

[client]
#password       = your_password
port            = 3306
socket          = /tmp/mysql.sock

# Here follows entries for some specific programs

# The MySQL server
[mysqld]
port            = 3306
socket          = /tmp/mysql.sock
skip-external-locking
key_buffer_size = 16M
max_allowed_packet = 1M
table_open_cache = 64
sort_buffer_size = 512K
net_buffer_length = 8K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M
           

对于port,一台机器上有多个实例的时候,需要修改为不同的端口号,还有scok文件,也需要区别。

basedir = /usr/local/mysql01/mysql
datadir = /usr/local/mysql01/mysql/data
# Uncomment the following if you are using InnoDB tables
innodb_data_home_dir = /usr/local/mysql01/mysql/data
innodb_data_file_path = ibdata1:10M:autoextend
innodb_log_group_home_dir = /usr/local/mysql01/mysql/data
# You can set .._buffer_pool_size up to 50 - 80 %
# of RAM but beware of setting memory usage too high
innodb_buffer_pool_size = 16M
innodb_additional_mem_pool_size = 2M
# Set .._log_file_size to 25 % of buffer pool size
innodb_log_file_size = 5M
innodb_log_buffer_size = 8M
innodb_flush_log_at_trx_commit = 1  //redo log配置项 0表示事务提交时不写文件  1表示事务提交时写文件且fsync到磁盘  2表示写但不调用fsync落盘 
innodb_lock_wait_timeout = 50

           

重点配置basedir指定我们的安装目录,datadir指定数据库文件路径,如果需要,可以指定下sock和pid文件,还有err日志。在一台机器上陪多个节点,一定要把sock修改一下,不然每个节点使用的都是/tmp/mysql.sock文件,可以简单的重命名为mysql01.sock和mysql02.sock,当连接的时候i,加上-S /tmp/mysql02.sock即可,不然会提示找不到/tmp/mysql.sock错误,需要注意的还有端口号,master默认为3306,slave在一台机器上一定要修改一下。 之后./bin/mysqld_safe --defaults-file=./my.cnf --user=mysql & 按照相同的步骤安装slave节点。

集群的配置:

[mysqld]
log-bin=mysql-bin //开启bin日志,slave可关闭
server-id=1 //server-id,master和slave必须不同
           

之后启动master,正在master上注册slave用户和password,命令如下:

mysql> CREATE USER 'repl'@'%.mydomain.com' IDENTIFIED BY 'slavepass';
mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%.mydomain.com';
           

其中域名改为localhost(我在一台机器上配置的),设置好name和password,同时将slave的server-id改为2,关闭slave的二进制日志。这里先贴一点my.cnf上的注释

# required unique id between 1 and 2^32 - 1
# defaults to 1 if master-host is not set
# but will not function as a master if omitted
server-id       = 2

# Replication Slave (comment out master section to use this)
#
# To configure this host as a replication slave, you can choose between
# two methods :
#
# 1) Use the CHANGE MASTER TO command (fully described in our manual) -
#    the syntax is:
#
#    CHANGE MASTER TO MASTER_HOST=<host>, MASTER_PORT=<port>,
#    MASTER_USER=<user>, MASTER_PASSWORD=<password> ;
#
#    where you replace <host>, <user>, <password> by quoted strings and
#    <port> by the master's port number (3306 by default).
#
#    Example:
#
#    CHANGE MASTER TO MASTER_HOST='125.564.12.1', MASTER_PORT=3306,
#    MASTER_USER='joe', MASTER_PASSWORD='secret';
#
# OR
#
# 2) Set the variables below. However, in case you choose this method, then
#    start replication for the first time (even unsuccessfully, for example
#    if you mistyped the password in master-password and the slave fails to
#    connect), the slave will create a master.info file, and any later
#    change in this file to the variables' values below will be ignored and
#    overridden by the content of the master.info file, unless you shutdown
#    the slave server, delete master.info and restart the slaver server.
#    For that reason, you may want to leave the lines below untouched
#    (commented) and instead use CHANGE MASTER TO (see above)
#
# required unique id between 2 and 2^32 - 1
# (and different from the master)
# defaults to 2 if master-host is set
# but will not function as a slave if omitted
#server-id       = 2
#
# The replication master for this slave - required
#master-host     =   localhost
#
# The username the slave will use for authentication when connecting
# to the master - required
#master-user     =   slave01
#
# The password the slave will authenticate with when connecting to
# the master - required
#master-password =   123456
#
# The port the master is listening on.
# optional - defaults to 3306
#master-port     = 3306
#
           

从这里我们知道有两种配置方式,可以在slave的mysql>下直接唱歌 master to ,也可以配置在配置文件中,这里使用第一种

mysql> CHANGE MASTER TO
    ->     MASTER_HOST='master_host_name',
    ->     MASTER_USER='replication_user_name',
    ->     MASTER_PASSWORD='replication_password',
    ->     MASTER_LOG_FILE='recorded_log_file_name',
    ->     MASTER_LOG_POS=recorded_log_position;
           

然后 start slave即可。 这里我在master和slave配置replication之前,是保证了两个库的数据一致的,如果不一样,还要进行一些lock和转储快照等的操作,mysql文档上有说。开了两个窗口测试了一下,master的更改slave都会体现,so nice。 我只是做了个简单的步骤记录。没贴太多过程图,其实是配好了,才写的记录。

继续阅读