源码编译安装mariadb数据库

➡️源码安装CMake

➡️1.获取CMake源码包

cmake-3.12.0.tar.gz

➡️2.解压、编译、安装Cmake

tar -xzvf cmake-3.12.0.tar.gz
cd cmake-3.12.0
./bootstrap
make
make install

➡️备注

cmake 会默认安装在 usr/local/bin 下面 ./bootstrap 需要gcc,gcc-c++环境时

yum install -y gcc gcc-c++

➡️源码安装mariadb

➡️1.获取源码包

mariadb-10.3.8.tar.gz

➡️2.编译环境准备

yum groupinstall -y Development Tools ncurses-devel openssl-devel openssl

➡️3.创建mysql用户

groupadd mysql
useradd -s /sbin/nologin -g mysql -M mysql
id mysql
# uid=500(mysql) gid=500(mysql) groups=500(mysql)
#创建数据库数据存放目录;
mkdir /mydata/data -pv
chown mysql:mysql /mydata/data/ -R

➡️4.编译安装MariaDB

cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
          -DMYSQL_DATADIR=/mydata/data \
          -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
#编译完成后安装数据库:
make
make install

➡️5.配置安装MariaDB

cd /usr/local/mysql/
scripts/mysql_install_db --user=mysql --datadir=/mydata/data
ls /mydata/data/
    aria_log.00000001  ib_logfile0  mysql-bin.000001  mysql-bin.state
    aria_log_control   ib_logfile1  mysql-bin.000002  performance_schema
    ibdata1            mysql        mysql-bin.index   test

➡️6.mariadb配置文件创建及更改,没有模版

mkdir /etc/mysql
vim /etc/mysql/my.cnf
    [mysqld]
    port            = 3306
    datadir         = /mydata/data
    socket          = /tmp/mysql.sock
    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
    # Try number of CPU's*2 for thread_concurrency
    thread_concurrency = 8
    innodb_file_per_table = on
    skip_name_resolve = on

创建服务脚本:

cp support-files/mysql.server /etc/rc.d/init.d/mysqld
chkconfig --list mysqld
chkconfig --add mysqld

启动mysqld服务,测试启动

service mysqld start
    Starting MySQL.                                            [  OK  ]

➡️7.设置环境变量

vim /etc/profile.d/mysqld.sh
    MYSQL_HOME=/usr/local/mysql
    export PATH=$MYSQL_HOME/bin:$PATH

➡️加载环境变量:

source /etc/profile.d/mysqld.sh

➡️8.修改Mysql的root用户密码以及打开远程连接(可选)

mysql
    Welcome to the MariaDB monitor.  Commands end with ; or \g.
    Your MariaDB connection id is 9
    Server version: 10.3.8-MariaDB Source distribution

    Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
grant all privileges on *.* to root@'localhost' identified by '密码';
grant all privileges on *.* to root@'127.0.0.1' identified by '密码';
use mysql;
    Database changed
update user set password = password('密码') where User='root';
select Host,User,Password from user where User='root';
    +----------------+------+-------------------------------------------+
    | Host           | User | Password                                  |
    +----------------+------+-------------------------------------------+
    | localhost      | root | *密码                                     |
    | tom2.stu31.com | root | *密码                                     |
    | 127.0.0.1      | root | *密码                                     |
    | ::1            | root | *密码                                     |
    +----------------+------+-------------------------------------------+
    4 rows in set (0.00 sec)

➡️9.我们使用MariaDB提供的安全设置脚本来进行数据库安全配置

➡️MariaDB提供了一些配置脚本在

cd $MYSQL_HOME/bin
ls
    aria_chk       innochecksum    myisamchk          myrocks_hotbackup  mysqlcheck                  mysqld_multi        mysql_find_rows       mysql_plugin               mysqltest            perror              tokuftdump             wsrep_sst_rsync
    aria_dump_log  mariabackup     myisam_ftdump      mysql              mysql_client_test           mysqld_safe         mysql_fix_extensions  mysql_secure_installation  mysql_tzinfo_to_sql  replace             tokuft_logprint        wsrep_sst_rsync_wan
    aria_ftdump    mariadb_config  myisamlog          mysqlaccess        mysql_config                mysqld_safe_helper  mysqlhotcopy          mysql_setpermission        mysql_upgrade        resolveip           wsrep_sst_common       wsrep_sst_xtrabackup
    aria_pack      mbstream        myisampack         mysqladmin         mysql_convert_table_format  mysqldump           mysqlimport           mysqlshow                  mysql_waitpid        resolve_stack_dump  wsrep_sst_mariabackup  wsrep_sst_xtrabackup-v2
    aria_read_log  msql2mysql      my_print_defaults  mysqlbinlog        mysqld                      mysqldumpslow       mysql_ldb             mysqlslap                  mytop                sst_dump            wsrep_sst_mysqldump
#我们使用mysql_secure_installation这个脚本来进行安全配置:
mysql_secure_installation
    NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
          SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

    In order to log into MariaDB to secure it, we'll need the current
    password for the root user.  If you've just installed MariaDB, and
    you haven't set the root password yet, the password will be blank,
    so you should just press enter here.

    Enter current password for root (enter for none): 
    OK, successfully used password, moving on...

    Setting the root password ensures that nobody can log into the MariaDB
    root user without the proper authorisation.

    Set root password? [Y/n] n    
     ... skipping.

    By default, a MariaDB installation has an anonymous user, allowing anyone
    to log into MariaDB without having to have a user account created for
    them.  This is intended only for testing, and to make the installation
    go a bit smoother.  You should remove them before moving into a
    production environment.

    Remove anonymous users? [Y/n] Y
     ... Success!

    Normally, root should only be allowed to connect from 'localhost'.  This
    ensures that someone cannot guess at the root password from the network.

    Disallow root login remotely? [Y/n] Y
     ... Success!

    By default, MariaDB comes with a database named 'test' that anyone can
    access.  This is also intended only for testing, and should be removed
    before moving into a production environment.

    Remove test database and access to it? [Y/n] Y
     - Dropping test database...
     ... Success!
     - Removing privileges on test database...
     ... Success!

    Reloading the privilege tables will ensure that all changes made so far
    will take effect immediately.

    Reload privilege tables now? [Y/n] Y
     ... Success!

    Cleaning up...

    All done!  If you've completed all of the above steps, your MariaDB
    installation should now be secure.

    Thanks for using MariaDB!
#安全设置数据库完成后我们就不能使用mysql登录数据库了;但是平常我们操作时使用mysql -u root -p这样太麻烦了,为了方便,我们可以在用户家目录创建一个拥有mysql用户名和密码的.my.cnf隐藏配置文件进行登录数据库;
#创建个隐藏文件在root家目录:
vim .my.cnf
    [mysql]
    user = root
    password = 密码
    host =  127.0.0.1

➡️备注

➡️注意事项

重新编译时,需要清除旧的对象文件和缓存信息。

make clean
rm -f CMakeCache.txt
rm -rf /etc/my.cnf

错误:Curses library not found. Please install appropriate package 解决方案: 先安装 ncurses-devel

yum install ncurses-devel

再删除刚才编译生成的 CMakeCache.txt 文件

rm CMakeCache.txt

再次执行一次cmake

➡️编译选项

-DCMAKE_INSTALL_PREFIX=/usr/local/mysql       //安装目录
-DINSTALL_DATADIR=/mydata/data                //数据库存放目录
-DDEFAULT_CHARSET=utf8                      //使用utf8字符
-DDEFAULT_COLLATION=utf8_general_ci           //校验字符
-DEXTRA_CHARSETS=all                         //安装所有扩展字符集
-DENABLED_LOCAL_INFILE=1                     //允许从本地导入数据

➡️cmake编译指令介绍

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

-DCMAKE_INSTALL_PREFIX=/usr/local/mysql
-DMYSQL_DATADIR=/mydata/data
-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