一、源码编译安装gcc-5.1.0
1、下载gcc源码包
-
Download (HTTP): http://ftpmirror.gnu.org/gcc/gcc-5.2.0/gcc-5.2.0.tar.bz2
-
Download (FTP): ftp://ftp.gnu.org/gnu/gcc/gcc-5.2.0/gcc-5.2.0.tar.bz2
2、解压压缩包
1
|
[root@example.com ~] # tar -xf gcc-5.1.0.tar.gz |
3、下载编译所需的依赖包
这个步骤有两种方式完成:
a) 如果Linux有网络连接,直接这样:
1
2
|
[root@example.com ~] # cd gcc-5.1.0 [root@example.com gcc-5.1.0] # ./contrib/download_prerequisites |
b) 如果Linux没有网络连接(我主机和虚拟机是Host-only,不能联网,所以另外想办法),则用Windows上网下载这几个包:
ftp://ftp.gnu.org/gnu/gmp/gmp-4.3.2.tar.bz2
http://www.mpfr.org/mpfr-2.4.2/mpfr-2.4.2.tar.bz2
http://www.multiprecision.org/mpc/download/mpc-0.8.1.tar.gz
然后解压并移动到gcc-4.8.1下面:
tar -xjf gmp-4.3.2.tar.bz2
tar -xjf mpfr-2.4.2.tar.bz2
tar -xzf mpc-0.8.1.tar.gz
mv gmp-4.3.2 gcc-4.8.1/gmp
mv mpfr-2.4.2 gcc-4.8.1/mpfr
mv mpc-0.8.1 gcc-4.8.1/mpc
这样的做法好处是,不用单独编译gmp、mpfr和mpc三个包,放在gcc源码下面一起编译(事实上这也是gcc-4.8.1/contrib/download_prerequisites脚本的做法,个人感觉更简洁些)。我在编译是选择了第一种方法,第二种方法到底咋样本人没有实验过。
4、编译安装gcc
1
2
3
4
|
[root@example.com gcc-5.1.0] # yum install -y gcc-c++ glibc-static gcc //为避免出错建议安装此包 [root@example.com gcc-5.1.0] # ./configure --prefix=/usr/local/gcc --enable-bootstrap --enable-checking=release --enable-languages=c,c++ --disable-multilib [root@example.com gcc-5.1.0] # make [root@example.com gcc-5.1.0] # make install |
编译参数说明:
-
--prefix=/usr/local/ 指定安装路径
-
--enable-bootstrap 这里引用网上一些文献对该参数的解释:用第一次编译生成的程序进行第二次编译,然后用再次生成的程序进行第三次编译,并且检查比较第二次和第三次结果的正确性,也就是进行冗余的编译检查工作。 非交叉编译环境下,默认已经将该值设为 enable,可以不用显示指定;交叉编译环境下,需要显示将其值设为 disable。
-
--enable-checking=release 以软件发布版的标准来对编译时生成的代码进行一致性检查;设置该选项为 enable并不会改变编译器生成的二进制结果,但是会导致编译的时间增加;该选项仅支持gcc编译器; 总体而言,对于上面这个选项,机器的硬件配置较低,以及不愿等待太久编译时间的童鞋,可以设置为 disable;但是这会增加产生未预期的错误的风险,所以应该慎用。 可以同时设置 --disable-bootstrap 与 --disable-checking,这对编译过程的提速很有帮助。
-
--enable-threads=posix 顾名思义,启用posix标准的线程支持 ,要让程序能在符合POSIX规范的linux发布版上正确运行,就应该启用该选项,取决于宿主或目标操作系统的类型,其它可用值有:aix,dec,solaris,win32等,如果你是其它的类UNIX系统,就需要设置相应的值。
-
--enable-languages=c,c++ 支持的高级语言类型和运行时库,可以设置的所有语言包括 ada,c,c++,Fortran,java,objc,obj-c++,GO 等语言。这里只开启了c和c++,因为支持的语言越多,就需要安装越多的相应静态与动态库,还有五花八门的依赖库,这会让管理变得困难,体积也会变得庞大。
-
--disable-multilib 如果你的操作系统是32位,默认就已经设置为 disable,这意味着gcc仅能生成32位的可执行程序;如果你的操作系统是64位,默认就已经设置为 enable,这意味着用gcc编译其它源文件时可以通过 -m32 选项来决定是否生成32位机器代码。如果在64位系统上,要禁止生成32位代码, 设置 --disable-multilib。
-
--enable-gather-detailed-mem-stats 允许收集详细的内存使用信息,如果设置该参数为 enable,则将来编译好的gcc可执行程序,可以通过 -fmem-report 选项来输出编译其它程序时的实时内存使用情况。
-
--with-long-double-128 指定 long double 类型为128位(16字节!);设置为 without,则 long double类型将为64位(8字节),这将与普通的 double 类型一样。 基于 Glib 2.4以上版本编译时,默认已经是128位。
5、后续操作
导出环境变量:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
[root@example.com ~] # gcc --version gcc (GCC) 4.4.7 20120313 (Red Hat 4.4.7-16) Copyright 2010 Free Software Foundation, Inc. 本程序是自由软件;请参看源代码的版权声明。本软件没有任何担保; 包括没有适销性和某一专用目的下的适用性担保。 [root@example.com ~] # vim /etc/profile.d/gcc.sh export PATH= /usr/local/gcc/bin :$PATH [root@example.com ~] # source /etc/profile.d/gcc.sh [root@example.com ~] # gcc --version gcc (GCC) 5.1.0 Copyright (C) 2015 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
导出头文件:
1
2
|
[root@example.com ~] # ln -sv /usr/local/gcc/include/ /usr/include/gcc "/usr/include/gcc" -> "/usr/local/gcc/include/" |
导出库文件:
1
2
3
4
|
[root@example.com ~] # vim /etc/ld.so.conf.d/gcc.conf /usr/local/gcc/lib64 [root@example.com ~] # ldconfig -v [root@example.com ~] # ldconfig -p |grep gcc //验证是否导出 |
二、源码编译MariaDB
为啥开头要安装gcc-5.1,就是因为我在第一次编译mariadb时,到make的时候报错,在网上找到的这么一句哈:
http://www.linuxfromscratch.org/blfs/view/svn/server/mariadb.html
MariaDB is a community-developed fork and a drop-in replacement for the MySQL relational database management system.
This package is known to build and work properly using an LFS-7.7 platform.
This package is known to build and work properly using the gcc-5 compiler.
MariaDB是一个由社区开发的叉和MySQL的替代关系数据库管理系统。
这个包是构建和使用lfs - 7.7平台正常工作。
这个包是构建和使用gcc-5编译器正常工作。
1、创建数据存放目录
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
[root@example.com mysql] # yum install -y lvm2 [root@example.com ~] # fdisk /dev/sdb n p 1 +10G t 8e w [root@example.com ~] # pvcreate /dev/sdb1 Physical volume "/dev/sdb1" successfully created [root@example.com ~] # vgcreate data /dev/sdb1 Volume group "data" successfully created [root@example.com ~] # lvcreate -L 2G -n mydata data Logical volume "mydata" created. [root@example.com ~] # mke2fs -t ext4 -L MYDATA -b 4096 -m 3 /dev/data/mydata [root@example.com ~] # vim /etc/fstab LABEL=MYDATA /data/mydata ext4 defaults,noatime 0 0 [root@example.com ~] # mount -a [root@example.com ~] # chown -R mysql.mysql /data/mydata/ |
2、创建mysql用户
1
2
3
4
5
|
[root@example.com ~] # groupadd mysql [root@example.com ~] # useradd -s /sbin/nologin -g mysql -M mysql [root@example.com ~] # id mysql uid=1001(mysql) gid=1001(mysql) 组=1001(mysql) [root@example.com ~] # mkdir -pv /data/mydata |
3、安装mariadb
1
2
3
4
5
6
|
[root@example.com ~] # tar -xf mariadb-10.0.21.tar.gz [root@example.com ~] # yum install -y ncurses-devel openssl-devel openssl gcc-c++ cmake [root@example.com ~] # cd mariadb-10.0.21 [root@example.com mariadb-10.0.21] # cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/data/mydata -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 [root@example.com mariadb-10.0.21] #make [root@example.com mariadb-10.0.21] #make install |
4、参数说明
# 安装根目录
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql
# 数据存储目录
-DMYSQL_DATADIR=/data/mydata
# UNIX socket文件
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock
# 配置文件(my.cnf)目录
-DSYSCONFDIR=/etc
# 默认字符集
-DDEFAULT_CHARSET=utf8
# 默认字符校对
-DDEFAULT_COLLATION=utf8_general_ci
# TCP/IP端口
-DMYSQL_TCP_PORT=3306
# * ARCHIVE 引擎支持
-DWITH_ARCHIVE_STORAGE_ENGINE=1
# * ARIA 引擎支持
-DWITH_ARIA_STORAGE_ENGINE=1
# * BLACKHOLE 引擎支持
-DWITH_BLACKHOLE_STORAGE_ENGINE=1
# * FEDERATEDX 引擎支持
-DWITH_FEDERATEDX_STORAGE_ENGINE=1
# * PARTITION 引擎支持
-DWITH_PARTITION_STORAGE_ENGINE=1
# * PERFSCHEMA 引擎支持
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1
# * SPHINX 引擎支持
-DWITH_SPHINX_STORAGE_ENGINE=1
# * XTRADB 支持
-DWITH_XTRADB_STORAGE_ENGINE=1
# * innoDB 引擎支持
-DWITH_INNOBASE_STORAGE_ENGINE=1
# * Myisam 引擎支持
-DWITH_MYISAM_STORAGE_ENGINE=1
# readline库
-DWITH_READLINE=1
# 启用加载本地数据
-DENABLED_LOCAL_INFILE=1
# 扩展支持编码 ( all | utf8,gbk,gb2312 | none )
-DWITH_EXTRA_CHARSETS=all
# 扩展字符支持
-DEXTRA_CHARSETS=all
# 系统传输使用SSL加密
-DWITH_SSL=system
# 系统传输使用zlib压缩,节约带宽
-DWITH_ZLIB=system
# libwrap库
-DWITH_LIBWRAP=0
# 运行用户
-DMYSQL_USER=mysql
# 调试模式
-DWITH_DEBUG=0
5、编译引擎选项说明
默认编译的存储引擎包括: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
6、编译时错误解决方法
重新编译时,需要清除旧的对象文件和缓存信息。
#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 ...
一般都可以顺利安装的。
7、配置MariaDB
初始化数据库:
1
2
3
4
5
|
[root@example.com mariadb-10.0.21] # cd /usr/local/mysql/ [root@example.com mysql] # scripts/mysql_install_db --user=mysql --datadir=/data/mydata/ [root@example.com mysql] # ls /data/mydata/ aria_log.00000001 ibdata1 ib_logfile1 mysql testaria_log_control ib_logfile0 lost+found performance_schema |
设置配置文件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
[root@example.com mysql] # vim /etc/my.cnf [mysqld] port = 3306 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 = 4 datadir= /data/mydata // 需要添加此项 |
设置启动脚本:
1
2
3
4
|
[root@example.com mysql] # cp support-files/mysql.server /etc/init.d/mysqld [root@example.com mysql] # chkconfig --add mysqld [root@example.com mysql] # chkconfig --list mysqld mysqld 0:关闭1:关闭2:启用3:启用4:启用5:启用6:关闭 |
启动服务:
1
2
3
4
|
[root@example.com mysql] # service mysqld start Starting MySQL. SUCCESS! [root@example.com mysql] # ss -tunl |grep 3306 tcp LISTEN 0 128 :::3306 :::* |
导出环境变量:
1
2
3
|
[root@example.com mysql] # vim /etc/profile.d/mysql.sh export PATH= /usr/local/mysql/bin :$PATH [root@example.com mysql] # source /etc/profile.d/mysql.sh |
导出头文件:
1
2
|
[root@example.com mysql] # ln -sv /usr/local/mysql/include/ /usr/local/include/mysql "/usr/local/include/mysql" -> "/usr/local/mysql/include/" |
导出库文件:
1
2
3
4
5
6
7
8
|
[root@example.com mysql] # vim /etc/ld.so.conf.d/mysql.conf /usr/local/mysql/lib [root@example.com mysql] # ldconfig -v [root@example.com mysql] # ldconfig -p |grep mysql libmysqlclient_r.so.16 (libc6,x86-64) => /usr/lib64/mysql/libmysqlclient_r .so.16 libmysqlclient.so.18 (libc6,x86-64) => /usr/local/mysql/lib/libmysqlclient .so.18 libmysqlclient.so.16 (libc6,x86-64) => /usr/lib64/mysql/libmysqlclient .so.16 libmysqlclient.so (libc6,x86-64) => /usr/local/mysql/lib/libmysqlclient .so |
设置数据库用户密码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
[root@example.com mysql] # mysql Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 4 Server version: 10.0.21-MariaDB-log Source distribution Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> GRANT ALL PRIVILEGES on *.* to root@ 'localhost' IDENTIFIED BY 'redhat' ; Query OK, 0 rows affected (0.00 sec) MariaDB [(none)]> GRANT ALL PRIVILEGES on *.* to root@ '127.0.0.1' IDENTIFIED BY 'redhat' ; Query OK, 0 rows affected (0.00 sec) MariaDB [(none)]> use mysql Database changed MariaDB [mysql]> SET PASSWORD FOR root@ 'example.com' =PASSWORD( 'redhat' ); Query OK, 0 rows affected (0.00 sec) MariaDB [mysql]> SELECT Host,User,Password FROM user ; +-------------+------+-------------------------------------------+ | Host | User | Password | +-------------+------+-------------------------------------------+ | localhost | root | *84BB5DF4823DA319BBF86C99624479A198E6EEE9 | | example.com | root | *84BB5DF4823DA319BBF86C99624479A198E6EEE9 | | 127.0.0.1 | root | *84BB5DF4823DA319BBF86C99624479A198E6EEE9 | | ::1 | root | | | localhost | | | | example.com | | | +-------------+------+-------------------------------------------+ 6 rows in set (0.00 sec) |
使用mysql_secure_installation这个脚本来进行安全配置:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
[root@example.com 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! |
现mysql不输入用户名密码直接登录数据库
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
[root@example.com ~] # vim .my.cnf [mysql] user = root password = redhat host = localhost [root@example.com ~] # mysql Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 14 Server version: 10.0.21-MariaDB-log Source distribution Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> |
8、小技巧
由于make时间较久,一般可以使用-j 选项在加CPU核心说就能加速,下面就是自动查看当前系统CPU核心并根据核心数进行编译。
[ "`cat /proc/cpuinfo |grep 'processor'|wc -l`" = "1" ] && make
[ "`cat /proc/cpuinfo |grep 'processor'|wc -l`" != "1" ] && make -j`cat /proc/cpuinfo |grep 'processor'|wc -l`