mariadb编译安装

from:
原文阅读


源码安装mariadb-10.0.12



1.获取源码包

mariadb-10.0.12.tar.gz



2.编译环境准备

1

2


# yum groupinstall -y Development Tools

# yum install -y 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-10.0.12

解压源码包:

# tar xf mariadb-10.0.12.tar.gz 

cmake编译指令介绍:

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

1

2

3


-DCMAKE_INSTALL_PREFIX=/usr/local/mysql

-DMYSQL_DATADIR=/mydata/data

-DSYSCONFDIR=/etc


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

1

2

3

4


-DWITH_INNOBASE_STORAGE_ENGINE=1

-DWITH_ARCHIVE_STORAGE_ENGINE=1

-DWITH_BLACKHOLE_STORAGE_ENGINE=1

-DWITH_FEDERATED_STORAGE_ENGINE=1



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

1

2

3

4

5


-DWITHOUT_<ENGINE>_STORAGE_ENGINE=1

比如:

-DWITHOUT_EXAMPLE_STORAGE_ENGINE=1

-DWITHOUT_FEDERATED_STORAGE_ENGINE=1

-DWITHOUT_PARTITION_STORAGE_ENGINE=1


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

1

2

3

4


-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


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

1

2


#make clean

#rm CMakeCache.txt


编译安装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




或者:

1


# 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


或者:

#./BUILD/compile-pentium64-max



编译完成后安装数据库:

1

2


# make 

# make install


备注:


-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                     //允许从本地导入数据



注意事项:


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

#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 ... 

一般都可以顺利安装的。



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

# cp support-files/my-large.cnf /etc/mysql/my.cnf

# 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


创建服务脚本:

1

2

3


# cp support-files/mysql.server /etc/rc.d/init.d/mysqld 

# chkconfig --list mysqld

# chkconfig --add mysqld


启动mysqld服务,测试启动

1

2


# service mysqld start

Starting MySQL.                                            [  OK  ]


7.设置环境变量:

1

2

3


]# vim /etc/profile.d/mysqld.sh

MYSQL_HOME=/usr/local/mysql

export PATH=$MYSQL_HOME/bin:$PATH


加载环境变量:

# source /etc/profile.d/mysqld.sh

连接MySQL

1

2

3

4

5

6

7

8


# mysql

Welcome to the MariaDB monitor.  Commands end with ; or \g.

Your MariaDB connection id is 4

Server version: 10.0.12-MariaDB-log Source distribution

Copyright (c) 2000, 2014, Oracle, SkySQL Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> quit

Bye


8.修改Mysql的root用户密码以及打开远程连接


[root@tom2 mysql]# mysql

Welcome to the MariaDB monitor.  Commands end with ; or \g.

Your MariaDB connection id is 5

Server version: 10.0.12-MariaDB-log Source distribution

Copyright (c) 2000, 2014, Oracle, SkySQL Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

#授权本地主机localhost登录,并设置密码;

MariaDB [(none)]> grant all privileges on *.* to root@'localhost' identified by 'oracle';

Query OK, 0 rows affected (0.00 sec)

#授权本地ip地址127.0.0.1登录,并设置密码;

MariaDB [(none)]> grant all privileges on *.* to root@'127.0.0.1' identified by 'oracle';

Query OK, 0 rows affected (0.00 sec)

#切换数据库到mysql;

MariaDB [(none)]> use mysql

Database changed

#更新root用户端管理密码;

MariaDB [mysql]> update user set password = password('oracle') where User='root';

Query OK, 2 rows affected (0.00 sec)

Rows matched: 4  Changed: 2  Warnings: 0

#查看root用户授权的主机;

MariaDB [mysql]> select Host,User,Password from user where User='root';

+----------------+------+-------------------------------------------+

| Host           | User | Password                                  |

+----------------+------+-------------------------------------------+

| localhost      | root | *2447D497B9A6A15F2776055CB2D1E9F86758182F |

| tom2.stu31.com | root | *2447D497B9A6A15F2776055CB2D1E9F86758182F |

| 127.0.0.1      | root | *2447D497B9A6A15F2776055CB2D1E9F86758182F |

| ::1            | root | *2447D497B9A6A15F2776055CB2D1E9F86758182F |

+----------------+------+-------------------------------------------+

4 rows in set (0.00 sec)

#查看数据库中的所有主机;可以发现有两个匿名主机,为了安全我们需要删除尼玛主机;

MariaDB [mysql]> select user,host,password from user;    

+------+----------------+-------------------------------------------+

| user | host           | password                                  |

+------+----------------+-------------------------------------------+

| root | localhost      | *2447D497B9A6A15F2776055CB2D1E9F86758182F |

| root | tom2.stu31.com | *2447D497B9A6A15F2776055CB2D1E9F86758182F |

| root | 127.0.0.1      | *2447D497B9A6A15F2776055CB2D1E9F86758182F |

| root | ::1            | *2447D497B9A6A15F2776055CB2D1E9F86758182F |

|      | localhost      |                                           |

|      | tom2.stu31.com |                                           |

+------+----------------+-------------------------------------------+

6 rows in set (0.00 sec)

MariaDB [mysql]> \q

Bye




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

MariaDB提供了一些配置脚本在


[root@tom2 mysql]# cd $MYSQL_HOME/bin

[root@tom2 bin]# ls

aria_chk           mysqlbinlog                 mysql_plugin

aria_dump_log      mysqlbug                    mysql_secure_installation

aria_ftdump        mysqlcheck                  mysql_setpermission

aria_pack          mysql_client_test           mysqlshow

aria_read_log      mysql_config                mysqlslap

innochecksum       mysql_convert_table_format  mysqltest

msql2mysql         mysqld                      mysql_tzinfo_to_sql

myisamchk          mysqld_multi                mysql_upgrade

myisam_ftdump      mysqld_safe                 mysql_waitpid

myisamlog          mysqldump                   mysql_zap

myisampack         mysqldumpslow               mytop

my_print_defaults  mysql_find_rows             perror

mysql              mysql_fix_extensions        replace

mysqlaccess        mysqlhotcopy                resolveip

mysqladmin         mysqlimport                 resolve_stack_dump


我们使用mysql_secure_installation这个脚本来进行安全配置:


[root@tom2 bin]# mysql_secure_installation 

/usr/local/mysql/bin/mysql_secure_installation: line 379: find_mysql_client: command not found

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.

You already have a root password set, so you can safely answer 'n'.

#改变root用户的密码;

Change the root password? [Y/n] Y

New password: 

Re-enter new password: 

Password updated successfully!

Reloading privilege tables..

 ... Success!

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.

#禁止root用户远程登录;

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.

#不移除test数据库;

Remove test database and access to it? [Y/n] n

 ... skipping.

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!








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

设置密码后使用mysql指令是禁止登录了:

1

2


# mysql

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)


创建个隐藏文件在root家目录:


[root@tom2 ~]# vim .my.cnf

[mysql]

user = root

password = oracle

host =  127.0.0.1



这样我们就能实现mysql不输入用户名密码直接登录数据库了,方便操作。



至此,源码编译安装MariaDB数据库就完成了。



本文出自 “飞雪连天射白鹿” 博客,请务必保留此出处http://sohudrgon.blog.51cto.com/3088108/1607202