源碼編譯安裝mariadb-10.0.12數據庫

源碼安裝mariadb-10.0.12


1.獲取源碼包

mariadb-10.0.12.tar.gz


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編譯指令介紹:

指定安裝文件的安裝路徑時常用的選項:

-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



編譯安裝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

或者:

# 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


編譯完成後安裝數據庫:

# 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


創建服務腳本:

# 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

連接MySQL

# 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指令是禁止登錄了:

# 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數據庫就完成了。


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