MySQL学习笔记之二:源码编译安装和二进制格式安装MariaDB

一、二进制格式安装

 1、添加mysql用户和mysql组

     mariadb(mysql)是以系统用户mysql的身份运行的,因此这里首先创建一个系统用户mysql

       useradd -r -s /sbin/nologin mysql

[root@node1 ~]# useradd -r -s /sbin/nologin mysql
[root@node1 ~]# id mysql
uid=496(mysql) gid=493(mysql) groups=493(mysql)

 2、下载二进制格式安装包

     mariadb 二进制分发版(类似于windows上的下载解压后可直接运行的绿色软件)的文件名格式为 mariadb-VERSION-OS.tar.gz,其中 VERSION 是版本号,OS表示分发版安装的操作系统类型

     可到官网下载:

        https://downloads.mariadb.org

 3、解压到指定目录,更改安装目录属主属组,并为其创建一个软链接

     tar xf mariadb-5.5.36-linux2.6-x86_64.tar.gz -C /usr/local/     

     cd /usr/local

     mv mariadb-5.5.36-linux-x86_64 mariadb-5.5.36

     chown -R root.mysql mariadb-5.5.36

     ln -s mariadb-5.5.36 mysql

[root@node1 ~]# tar -xf mariadb-5.5.36-linux-x86_64.tar.gz -C /usr/local
[root@node1 ~]# cd /usr/local
[root@node1 local]# ls
apache  apr  apr-util  bin  etc  games  include  lib  lib64  libexec  mariadb-5.5.36-linux-x86_64  sbin  share  src
[root@node1 local]# mv mariadb-5.5.36-linux-x86_64 mariadb-5.5.36
[root@node1 local]# chown -R root.mysql mariadb-5.5.36
[root@node1 local]# ln -s mariadb-5.5.36 mysql
[root@node1 local]# cd mysql
[root@node1 mysql]# ls
bin  COPYING  COPYING.LESSER  data  docs  include  INSTALL-BINARY  lib  man  mysql-test  README  scripts  share  sql-bench  support-files
[root@node1 mysql]# less INSTALL-BINARY    #查看安装说明
...
   The basic commands that you must execute to install and use a
   MariaDB binary distribution are:

shell> groupadd mysql
shell> useradd -g mysql mysql
shell> cd /usr/local
shell> gunzip < /path/to/mysql-VERSION-OS.tar.gz | tar xvf -
shell> ln -s full-path-to-mysql-VERSION-OS mysql
shell> cd mysql
shell> chown -R mysql .
shell> chgrp -R mysql .
shell> scripts/mysql_install_db --user=mysql
shell> chown -R root .
shell> chown -R mysql data
shell> bin/mysqld_safe --user=mysql &

Note
...

 4、通常应将mariadb(mysql)数据存储于逻辑卷上,易于扩展

     先创建两个分区/dev/sda3,4

     创建pv:

          pvcreate /dev/sda{3,4}

     创建vg:

          vgcreate myvg /dev/sda{3,4}

     创建lv:

          lvcreate -L 20G -n mydata myvg

     格式化:

          mke2fs -t ext4 -L MYDATA -b 4096 -m 3 /dev/myvg/mydata

     修改/etc/fstab文件,使其能够自动挂载

          LABEL=MYDATA(或UUID或/dev/mapper/myvg-lvdata)    /mydata   ext4   defaults  0  0

     创建挂载点

          mkdir /mydata

          mount -a     

     在/mydata下创建data目录,并修改其属主属组

          mkdir /mydata/data

          chown -R mysql.mysql /mydata/data

          这样,mariadb的数据目录就创建好了

[root@node1 mysql]# fdisk /dev/sda

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help): p

Disk /dev/sda: 32.2 GB, 32212254720 bytes
255 heads, 63 sectors/track, 3916 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000f3804

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          26      204800   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              26        1332    10485760   83  Linux
/dev/sda3            1332        1593     2097152   82  Linux swap / Solaris

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
e
Selected partition 4
First cylinder (1593-3916, default 1593): 
Using default value 1593
Last cylinder, +cylinders or +size{K,M,G} (1593-3916, default 3916): 
Using default value 3916

Command (m for help): n
First cylinder (1593-3916, default 1593): 
Using default value 1593
Last cylinder, +cylinders or +size{K,M,G} (1593-3916, default 3916): +10G

Command (m for help): t
Partition number (1-5): 5
Hex code (type L to list codes): 8e
Changed system type of partition 5 to 8e (Linux LVM)

Command (m for help): p

Disk /dev/sda: 32.2 GB, 32212254720 bytes
255 heads, 63 sectors/track, 3916 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000f3804

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          26      204800   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              26        1332    10485760   83  Linux
/dev/sda3            1332        1593     2097152   82  Linux swap / Solaris
/dev/sda4            1593        3916    18666534    5  Extended
/dev/sda5            1593        2898    10489417+  8e  Linux LVM

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.

[root@node1 mysql]# partx -a /dev/sda
BLKPG: Device or resource busy
error adding partition 1
BLKPG: Device or resource busy
error adding partition 2
BLKPG: Device or resource busy
error adding partition 3
[root@node1 mysql]# partx -a /dev/sda
BLKPG: Device or resource busy
error adding partition 1
BLKPG: Device or resource busy
error adding partition 2
BLKPG: Device or resource busy
error adding partition 3
BLKPG: Device or resource busy
error adding partition 4
BLKPG: Device or resource busy
error adding partition 5
[root@node1 mysql]# pvcreate /dev/sda5
  Physical volume "/dev/sda5" successfully created
[root@node1 mysql]# vgcreate myvg /dev/sda5
  Volume group "myvg" successfully created
[root@node1 mysql]# lvcreate -L 5G -n mydata myvg
  Logical volume "mydata" created
[root@node1 mysql]# mke2fs -t ext4 -L MYDATA -b 4096 -m 3 /dev/myvg/mydata 
mke2fs 1.41.12 (17-May-2010)
Filesystem label=MYDATA
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
327680 inodes, 1310720 blocks
39321 blocks (3.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=1342177280
40 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks: 
	32768, 98304, 163840, 229376, 294912, 819200, 884736

Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 34 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
[root@node1 mysql]# vim /etc/fstab
...
LABEL=MYDATA            /mydata                 ext4    defaults        0 0
[root@node1 mysql]# mkdir -p /mydata/data
[root@node1 mysql]# mount -a
[root@node1 mysql]# mount
/dev/sda2 on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw)
/dev/sda1 on /boot type ext4 (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
/dev/mapper/myvg-mydata on /mydata type ext4 (rw)
[root@node1 mysql]# chown -R mysql.mysql /mydata/data
[root@node1 mysql]# ls -ld /mydata/data
drwxr-xr-x 2 mysql mysql 4096 Jan  3 11:36 /mydata/data

 5、配置文件

     mv /etc/my.cnf /etc/my.cnf.bak(若存在其它版本的配置文件,则将其屏蔽)

     cp /usr/local/mysql/support-files/my-large.cnf /etc/my.cnf

[root@node1 mysql]# ls support-files/    #support-files目录下存放着适用于各种内存大小的配置文件及服务脚本等
binary-configure  config.medium.ini  magic        my-innodb-heavy-4G.cnf  my-medium.cnf  mysqld_multi.server  mysql.server           SELinux
config.huge.ini   config.small.ini   my-huge.cnf  my-large.cnf            my-small.cnf   mysql-log-rotate     ndb-config-2-node.ini  solaris
[root@node1 mysql]# cp support-files/my-large.cnf /etc/my.cnf

 6、修改数据目录和socket路径

     vim /etc/my.cnf

       添加datadir = /mydata/data(你所指定的数据目录)

       socket = /var/lib/mysql/mysql.sock    #某些程序或模块通过unix socket连接同机的mysql服务时默认指向的socket路径是/var/lib/mysql/mysql.sock

[root@node1 mysql]# vim /etc/my.cnf 
...
[client]
#password       = your_password
port            = 3306
#socket          = /tmp/mysql.sock
socket          = /var/lib/mysql/mysql.sock    #宜修改socket路径为该值

# Here follows entries for some specific programs
# The MariaDB server
[mysqld]
port            = 3306
#socket          = /tmp/mysql.sock
socket          = /var/lib/mysql/mysql.sock   #宜修改socket路径为该值
skip-external-locking
key_buffer_size = 256M
max_allowed_packet = 1M
table_open_cache = 256
sort_buffer_size = 1M
read_buffer_size = 1M
read_rnd_buffer_size = 4M
myisam_sort_buffer_size = 64M
thread_cache_size = 8
query_cache_size= 16M
datadir = /mydata/data     #添加这一项

[root@node2 ~]# vim /usr/local/mysql/bin/mysql_config
...
socket='/var/lib/mysql/mysql.sock'
...

 7、初始化数据库      

     cd /usr/local/mysql

     scripts/mysql_install_db --user=mysql --datadir=/mydata/data/

[root@node1 mysql]# ls scripts/
mysql_install_db
[root@node1 mysql]# scripts/mysql_install_db --user=mysql --datadir=/mydata/data
Installing MariaDB/MySQL system tables in '/mydata/data' ...
OK
Filling help tables...
OK

To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system

PLEASE REMEMBER TO SET A PASSWORD FOR THE MariaDB root USER !
To do so, start the server, then issue the following commands:

'./bin/mysqladmin' -u root password 'new-password'
'./bin/mysqladmin' -u root -h node1 password 'new-password'

Alternatively you can run:
'./bin/mysql_secure_installation'

which will also give you the option of removing the test
databases and anonymous user created by default.  This is
strongly recommended for production servers.

See the MariaDB Knowledgebase at http://mariadb.com/kb or the
MySQL manual for more instructions.

You can start the MariaDB daemon with:
cd '.' ; ./bin/mysqld_safe --datadir='/mydata/data'

You can test the MariaDB daemon with mysql-test-run.pl
cd './mysql-test' ; perl mysql-test-run.pl

Please report any problems at http://mariadb.org/jira

The latest information about MariaDB is available at http://mariadb.org/.
You can find additional information about the MySQL part at:
http://dev.mysql.com
Support MariaDB development by buying support/new features from
SkySQL Ab. You can contact us about this at [email protected].
Alternatively consider joining our community based development effort:
http://mariadb.com/kb/en/contributing-to-the-mariadb-project/

[root@node1 mysql]# ls /mydata/data/
aria_log.00000001  aria_log_control  mysql  mysql-bin.000001  mysql-bin.000002  mysql-bin.index  performance_schema  test

 8、服务脚本

     cp support-files/mysql.server /etc/rc.d/init.d/mysqld (服务脚本)

     chkconfig --add mysqld

     chkconfig mysqld on

[root@node1 mysql]# cp support-files/mysql.server /etc/rc.d/init.d/mysqld
[root@node1 mysql]# chkconfig --add mysqld
[root@node1 mysql]# chkconfig mysqld on
[root@node1 mysql]# chkconfig --list mysqld
mysql          	0:off	1:off	2:on	3:on	4:on	5:on	6:off

 9、启动mysqld服务,并查看3306端口是否已处于监听状态

     service mysqld start

     ss -tnl

[root@node1 mysql]# service mysqld start
Starting MySQL... SUCCESS! 
[root@node1 mysql]# ss -tnl
State       Recv-Q Send-Q                                                     Local Address:Port                                                       Peer Address:Port 
LISTEN      0      128                                                                   :::58351                                                                :::*     
LISTEN      0      128                                                                   :::111                                                                  :::*     
LISTEN      0      128                                                                    *:111                                                                   *:*     
LISTEN      0      128                                                                   :::22                                                                   :::*     
LISTEN      0      128                                                                    *:22                                                                    *:*     
LISTEN      0      128                                                            127.0.0.1:631                                                                   *:*     
LISTEN      0      128                                                                  ::1:631                                                                  :::*     
LISTEN      0      128                                                                    *:50903                                                                 *:*     
LISTEN      0      10                                                             127.0.0.1:25                                                                    *:*     
LISTEN      0      128                                                            127.0.0.1:6011                                                                  *:*     
LISTEN      0      128                                                                  ::1:6011                                                                 :::*     
LISTEN      0      50                                                                     *:3306                                                                  *:*

 10、设置PATH环境变量

     vim /etc/profile.d/mysql.sh

        export PATH=/usr/local/mysql/bin:$PATH

     source /etc/profile

[root@node1 mysql]# vim /etc/profile.d/mysql.sh

export PATH=/usr/local/mysql/bin:$PATH
[root@node1 mysql]# source !$
source /etc/profile.d/mysql.sh

 11、导出帮助文件   

     vim /etc/man.config 

     MANPATH /usr/local/mysql/man

[root@node1 mysql]# vim /etc/man.config

MANPATH /usr/local/mysql/man

 12、导出头文件

     ln -sv /usr/local/mysql/include /usr/include/mysql

[root@node1 mysql]# ln -s /usr/local/mysql/include/ /usr/include/mysql

 13、导出lib库

     vim /etc/ld.so.conf.d/mysql.conf 

       /usr/local/mysql/lib

[root@node1 mysql]# vim /etc/ld.so.conf.d/mysql.conf

/usr/local/mysql/lib


二、源码编译安装

  1、安装cmake及其它一些编译所依赖的包

     yum -y install cmake gcc gcc-c++ ncurses-devel libxml2-devel openssl-devel [bison] [libaio]

     编译mysql/mariadb需要使用cmake,cmake的重要特性之一是其独立于源码(out-of-source)的编译功能,即编译工作可以在另一个指定的目录中而非源码目录中进行,这可以保证源码目录不受任何一次编译的影响,因此在同一个源码树上可以进行多次不同的编译,如针对于不同平台编译。


  2、编译安装mariadb-10.0.13

    ⑴下载源码并解压

     tar xf mariadb-10.0.13.tar.gz

     cd mariadb_10.0.13

    ⑵使用cmake配置需要编译的功能

     cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/mydata/data -DSYSCONFDIR=/etc -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DWITH_SSL=system -DWITH_ZLIB=system -DWITH_LIBWRAP=0 -DMYSQL_UNIX_ADDR=/tmp/mysql.sock -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci

     说明

       ①cmake指定编译选项的方式不同于make,其实现方式对比如下:

         cmake .-LH 或 ccmake .  #查看可编译的选项,相当于./configure --help

         cmake .            #配置需要编译的功能,相当于./configure   

       ②指定安装文件的安装路径时常用的选项:

         -DCMAKE_INSTALL_PREFIX=/usr/local/mysql

         -DMYSQL_DATADIR=/data/mysql

         -DSYSCONFDIR=/etc

       ③默认编译的存储引擎包括:csv、myisam、myisammrg和heap。若要安装其它存储引擎,可以使用类似如下编译选项:

         -DWITH_INNOBASE_STORAGE_ENGINE=1

         -DWITH_ARCHIVE_STORAGE_ENGINE=1

         -DWITH_BLACKHOLE_STORAGE_ENGINE=1

         -DWITH_FEDERATED_STORAGE_ENGINE=1

       ④若要明确指定不编译某存储引擎,可以使用类似如下的选项:

         -DWITHOUT_<ENGINE>_STORAGE_ENGINE=1

         比如:

           -DWITHOUT_EXAMPLE_STORAGE_ENGINE=1

           -DWITHOUT_FEDERATED_STORAGE_ENGINE=1

           -DWITHOUT_PARTITION_STORAGE_ENGINE=1

       ⑤如若要编译进其它功能,如SSL等,则可使用类似如下选项来实现编译时使用某库或不使用某库:

         -DWITH_READLINE=1

         -DWITH_SSL=system

         -DWITH_ZLIB=system

         -DWITH_LIBWRAP=0

       ⑥其它常用的选项:

         -DMYSQL_TCP_PORT=3306

         -DMYSQL_UNIX_ADDR=/tmp/mysql.sock

         -DENABLED_LOCAL_INFILE=1

         -DEXTRA_CHARSETS=all

         -DDEFAULT_CHARSET=utf8

         -DDEFAULT_COLLATION=utf8_general_ci   #默认的排序规则

         -DWITH_DEBUG=0

         -DENABLE_PROFILING=1   #启用性能剖析功能

        ⑦如果想清理此前的编译所生成的文件,则需要使用如下命令:

          make clean

          rm CMakeCache.txt


    ⑶make && make install

    后续步骤同上,不再赘述

[root@node3 ~]# yum -y install cmake gcc gcc-c++ ncurses-devel libxml2-devel openssl-devel bison
...
[root@node3 ~]# tar xf mariadb-10.0.13.tar.gz 
[root@node3 ~]# cd mariadb-10.0.13
[root@node3 mariadb-10.0.13]# ls
BUILD        CMakeLists.txt   COPYING         debian             include             libmysql     mysql-test  pcre     scripts     storage        TODO      win
BUILD-CMAKE  cmd-line-utils   COPYING.LESSER  Docs               INSTALL-SOURCE      libmysqld    mysys       plugin   sql         strings        unittest  zlib
client       config.h.cmake   CREDITS         EXCEPTIONS-CLIENT  INSTALL-WIN-SOURCE  libservices  mysys_ssl   randgen  sql-bench   support-files  VERSION
cmake        configure.cmake  dbug            extra              KNOWN_BUGS.txt      man          packaging   README   sql-common  tests          vio

[root@node3 mariadb-10.0.13]# cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/mydata/data \
-DSYSCONFDIR=/etc -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_READLINE=1 -DWITH_SSL=system -DWITH_ZLIB=system -DWITH_LIBWRAP=0 -DMYSQL_UNIX_ADDR=/tmp/mysql.sock \
-DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci
...
...
-- Configuring done
-- Generating done
CMake Warning:
  Manually-specified variables were not used by the project:

    SYSCONFDIR


-- Build files have been written to: /root/mariadb-10.0.13
[root@node3 mariadb-10.0.13]# ls | grep 'Cache'
CMakeCache.txt    #经过cmake处理后会生成该文件
[root@node3 mariadb-10.0.13]# make && make install
...


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章