> CentOS > CentOS服务器 > 环境配置 >

CentOS编译安装MariaDB 及系统初始化

MariaDB介绍    

    MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可。开发这个分支的原因之一是:甲骨文公司收购了MySQL后,有将MySQL闭源的潜在风险,因此社区采用分支的方式来避开这个风险。 MariaDB的目的是完全兼容MySQL,包括API和命令行,使之能轻松成为MySQL的代替品。在存储引擎方面,使用XtraDB(英语:XtraDB)来代替MySQL的InnoDB。

 

   为了将开源进行到底,接下来让我们编译安装并使用一个MariaDB

 

编译安装MariaDB

 

      注意:MariaDB的编译安装方式与MySQL相同

      1、准备生产环境中数据目录(逻辑卷)            

 
# fdisk /dev/sda 创建一个逻辑分区/dev/sda6

    

     wKiom1NKSlLgfwwaAAGKhCogXjM472.jpg    

   

 
创建逻辑卷
#pvcreate /dev/sda6
#vgcreate myvg /dev/sda6
#lvcreate -L 10G -n mydata /dev/myvg
格式化逻辑卷
#mke2fs -t ext4 /dev/myvg/mydata

 

   

 
创建数据目录
#mkdir -pv /mydata/data
挂载逻辑卷到数据目录,并添加到开机自动挂载

 

    wKiom1NKS_eBQkeQAAFnTjUmwVE170.jpg

 

  2、创建mysql用户mysql组(考虑到应用安全)    

 
#groupadd -r mysql     创建mysql组
#useradd -g mysql -r -d /mydata/data 创建mysql用户
#chown mysql;mysql /mydata/data 更改数据目录的属主属组

 

  3、安装编译器cmake    

 
#yum -y install cmake

 

     

 
安装特定的开发包(防止编译时出错)
#yum -y install readline-devel zlib-devel openssl-devel

       

 
cmake指定编译选项的方式不同于make,其实现方式对比如下:
./configure           cmake .
./configure --help    cmake . -LH or
                                                                                                                                                                                                                                                                                                                 
                                                                                                                                                                                                                                                                                                                 
指定安装文件的安装路径时常用的选项:
-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

  

  4、下载MariaDB源码包5.5.36

     

 
下载地址
#https://downloads.mariadb.org/interstitial/mariadb-5.5.36/kvm-tarbake-jaunty-x86/mariadb-5.5.36.tar.gz/from/http://mirrors.tuna.tsinghua.edu.cn/mariadb

    

   wKiom1NKTwjTzvkuAAAVOx5o9RI374.jpg

 

  5、编译安装  

 
#tar xf mariadb-5.5.36.tar.gz
#cd mariadb-5.5.36
#cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/mydata/data/ -DSYSCONFDIR=/etc -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STPRAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWIYH_READLINE=1 -DWIYH_SSL=system -DVITH_ZLIB=system -DWITH_LOBWRAP=0 -DMYSQL_UNIX_ADDR=/tmp/mysql.sock -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci
#make && make install

 

  6、准备MariaDB脚本及配置文件

 

 
初始化数据库
#cd /usr/local/mysql
#chown -R mysql:mysql *  更改属主属组
#scripts/mysql_install_db --datadir=/mydata/data --user=mysql 初始化库文件
#chown -R root * 更改属主为root

 

   

 
提供脚本
#cd /usr/local/mysql
#cp support-files/mysql.server  /etc/rc.d/init.d/mysqld 提供脚本
#chmod +x /etc/rc.d/init.d/mysqld   赋予执行权限
#chkconfig --add mysqld   添加mysqld为系统服务
#chkconfig mysqld on      添加为开机启动

   

 
提供配置文件
#cd /usr/local/mysql
#cp support-files/my-large.cnf  /etc/my.cnf
#vim /etc/my.cnf 编辑配置文件[mysqld]段填写如下内容
[mysqld]
datadir = /mydata/data  数据目录
thread_concurrency = 4  设置线程数=核心数x2

 

 7、提供二进制文件,库文件,头文件,man手册

   

 
提供二进制文件
# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
提供库文件
# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
提供头文件
# ln -sv /usr/local/include /usr/include/mysql
提供man手册
# echo 'MANPATH /usr/local/mysql' >> /etc/man.config
# man -M /usr/local/mysql/man mysqld 让man手册立刻生效为最新

      

 8、启动服务,连接MariaDB服务器

  

 
启动服务
#service mysqld start
#ss -ntl | grep :3306

   wKioL1NKV2Ch6JWcAABikagleSI802.jpg

   

   wKioL1NKWYuCOU0EAAGBQogMBDc233.jpg

 

初始化MariaDB

  1、初始化第一个操作

 
配置文件my.cnf
          集中式的配置:多个应用程序共用的配置文件
          [mysqld]
          [mysqld_safe]
          [client]
查看mysql服务器的所有配置信息
         #cd /usr/local/mysql/bin
         #. /mysqld --help --verbose | head -20
Default options are read from the following files in the given order:
         /etc/mysql/my.conf  /etc/my.cnf  ~/.my.conf
         使用配置文件的方式
         1、它一次查找每个需要查找的文件,结果是所有文件的并集
         2、如果某参数在多个文件中出现多次,后读取的最终生效
                                                                  
       # /user/local/mysql/bin/mysqld --help --verbose
         1、显示mysqld程序启动时可用的选项,通常都是长选项
         2、显示mysqld的配置文件中可用的服务变量
            mysql> SHOW GLOGAL VARIABLES
            mysql> SHOW SESSION VARIABLES

  

    2、初始化第二个操作

 

   

 
1、删除所有匿名用户
      mysql> DROP USER ''@'localhost';
      mysql> DROP  USER ''@'127.0.0.1'; 
2、给所有的root用户设定密码:
   第一种方式
     mysql> set password for usrName@hostName = password('your_passwd');
   第二种方式
     mysql> update user set password = password('your_passwd') where user = 'root'
     mysql> flush privileges;
   第三种方式 (shell命令)
    # mysqladmin -uUserName -hHost password 'new_passwd' -p
      Host为远程mysql服务器的ip地址
    # msyqladmin -uUserName -hHost -p flush-privileges;

   

    wKioL1NKXS6T1tXnAADwR6ZJ68U626.jpg

 

ps:水平有限,如有错误请指出。

让我们一起研究MariaDB的强大之处,将开源进行到底    

   

本文出自 “西风瘦猪” 博客,请务必保留此出处http://jungege.blog.51cto.com/4102814/1394924

(责任编辑:IT)