實驗環境html
OS: debian_version_8.11 64位 CPU: Intel(R) Xeon(R) CPU E5-2630 v4 @ 2.20GHz Mem: 8GB Kernel: 3.16.0-10-amd64 Mariadb: mariadb-10.2.30
簡要說明mysql
一、安裝包內網中已下載好,此文檔中不演示 二、全部的源碼包都在/apps/apps_src/ 三、全部的服務安裝路徑都在/apps/xxxxx 四、官網下載:https://downloads.mariadb.org/
編譯安裝過程
一、安裝相關依賴包sql
apt update && apt install cmake g++ bison libncurses5 libncurses5-dev libevent-dev openssl libssl-dev autoconf
二、解壓縮下載包數據庫
tar zxvf mariadb-10.2.30.tar.gz && cd mariadb-10.2.30/
三、建立運行mysql的用戶vim
groupadd -r -g 306 mysql useradd -r -g 306 -u 306 -s /bin/false -d /dev/null -M mysql
四、查看可編譯的選項
官方參考連接:安全
https://dev.mysql.com/doc/internals/en/installation-layout-options.html
五、配置選項服務器
cmake . \ -DCMAKE_INSTALL_PREFIX=/apps/mysql \ -DMYSQL_DATADIR=/apps/mysql/data \ -DMYSQL_UNIX_ADDR=/apps/mysql/lock/mysql.sock \ -DSYSCONFDIR=/etc/mysql \ -DWITHOUT_TOKUDB=1 \ -DMYSQL_USER=mysql \ -DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 \ -DWITHOUT_FEDERATED_STORAGE_ENGINE=1 \ -DWITHOUT_PARTITION_STORAGE_ENGINE=1 \ -DWITH_MYISAM_STORAGE_ENGINE=1 \ -DWITH_MEMORY_STORAGE_ENGINE=1 \ -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_TCP_PORT=3306 \ -DENABLED_LOCAL_INFILE=1 \ -DDEFAULT_CHARSET=utf8 \ -DWITH_EXTRA_CHARSETS=all \ -DDEFAULT_COLLATION=utf8_general_ci \ -DWITH_DEBUG=0 \ -DENABLE_PROFILING=1 2> ../mariadb-10.2.30-configure.err
備註: 若是編譯失敗請先刪除目錄下CMakeCache.txt,而後從新編譯執行。網絡
配置選項說明: -DCMAKE_INSTALL_PREFIX=/apps/mysql #指定安裝mysql根目錄 -DMYSQL_DATADIR=/apps/mysql/data #指定mysql數據庫存放數據的目錄 -DMYSQL_UNIX_ADDR=/apps/mysql/lock/mysql.sock #UNIX套接字文件,MySQL的通信目錄 -DSYSCONFDIR=/etc/mysql #指定mysql配置文件目錄 -DMYSQL_USER=mysql #mysql用戶名 -DWITHOUT_TOKUDB=1 #禁用 -DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 #禁用 -DWITHOUT_FEDERATED_STORAGE_ENGINE=1 #禁用 -DWITHOUT_PARTITION_STORAGE_ENGINE=1 #禁用 -DWITH_MYISAM_STORAGE_ENGINE=1 #啓用Myisam存儲引擎 -DWITH_MEMORY_STORAGE_ENGINE=1 #啓用Memory存儲引擎 -DWITH_INNOBASE_STORAGE_ENGINE=1 #啓用innobase數據庫引擎 -DWITH_ARCHIVE_STORAGE_ENGINE=1 #啓用archive數據庫引擎 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 #啓用blackhole數據庫引擎 -DWITH_READLINE=1 #支持mysql的readline庫,提供可編輯的命令行 -DWITH_SSL=system #mysql通信時支持ssl協議,(安全套接層) -DWITH_ZLIB=system #支持容許使用zlib庫,(安全套接層) -DWITH_LIBWRAP=0 #關閉libwrap庫,(實現了通用TCP包裝的功能,爲網絡服務守護進程使用) -DMYSQL_TCP_PORT=3306 #指定mysql的監聽的端口 -DENABLED_LOCAL_INFILE=1 #容許從本地導入數據,啓用加載本地數據 -DDEFAULT_CHARSET=utf8 #設置mysql默認使用utf8字符集,如不指定默認使用latin1西歐字符集 -DWITH_EXTRA_CHARSETS=all #指定mysql擴展字符集支持全部的字符集。默認mysql支持全部字符集 -DDEFAULT_COLLATION=utf8_general_ci #設定默認字符校對排序規則 -DWITH_DEBUG=0 #禁用debug,默認爲禁用 -DENABLE_PROFILING=1 #啓用Profiling分析,默認爲開啓
7.編譯執行app
make -j 4 2> ../mariadb-10.2.30-make.err
8.安裝執行socket
make -j 4 install 2> ../mariadb-10.2.30-install.err
9.配置全局變量
echo "export PATH=$PATH:/apps/mysql/bin" >> /etc/profile.d/mysql.sh source /etc/profile.d/mysql.sh
10.進入安裝目錄檢查目錄並建立建立缺失的目錄
mkdir -pv /apps/mysql/{lock,logs,run}
11.從安裝目錄中複製一份配置模板到/etc目錄下
mkdir /etc/mysql cp /apps/mysql/support-files/my-huge.cnf /etc/mysql/my.cnf
12.根據生產環境設定配置文件(內存8G的my.cnf配置)
vim /etc/mysql/my.cnf
#The following options will be passed to all MySQL clients clients [client] port = 3306 socket = /apps/mysql/lock/mysql.sock #The MySQL server [mysqld] port = 3306 socket = /apps/mysql/lock/mysql.sock basedir = /apps/mysql datadir = /apps/mysql/data pid-file = /apps/mysql/run/mysql.pid log-error = /apps/mysql/logs/mysql-error.log user = mysql bind-address=192.168.1.146 skip_name_resolve = 1 skip-external-locking key_buffer_size = 384M max_allowed_packet = 16M table_open_cache = 512 back_log = 600 sort_buffer_size = 8M read_buffer_size = 2M read_rnd_buffer_size = 8M myisam_sort_buffer_size = 64M myisam_max_sort_file_size = 10G myisam_repair_threads = 1 thread_cache_size = 64 query_cache_size = 32M query_cache_limit = 2M tmp_table_size = 64M join_buffer_size = 64M thread_concurrency = 4 ft_min_word_len = 4 explicit_defaults_for_timestamp = true #skip-networking max_connections = 1000 max_connect_errors = 1000 open_files_limit = 65535 transaction_isolation = REPEATABLE-READ log-bin=mysql-bin binlog_format=mixed binlog_cache_size = 64M max_heap_table_size = 8M server-id = 1 expire_logs_days = 30 default_storage_engine = InnoDB innodb_file_per_table = 1 innodb_data_home_dir = /apps/mysql/data innodb_data_file_path = ibdata1:10M:autoextend innodb_log_group_home_dir = /apps/mysql/logs innodb_buffer_pool_size = 4G innodb_write_io_threads = 4 innodb_read_io_threads = 4 innodb_thread_concurrency = 0 innodb_purge_threads = 1 innodb_log_file_size = 1G innodb_log_files_in_group = 3 innodb_max_dirty_pages_pct = 90 innodb_strict_mode = 0 innodb_log_buffer_size = 64M innodb_flush_log_at_trx_commit = 1 innodb_lock_wait_timeout = 50 interactive_timeout = 28800 wait_timeout = 28800 [mysqldump] quick max_allowed_packet = 16M [mysql] no-auto-rehash [myisamchk] key_buffer_size = 384M sort_buffer_size = 256M read_buffer = 128M write_buffer = 128M [mysqlhotcopy] interactive-timeout
配置文件說明:
詳情請看:MariaDB/MySQL配置文件my.cnf詳解
13.根據my.cnf配置文件中目錄設定mysql目錄權限
chown mysql:mysql /apps/mysql/{data,logs,run,lock}
14.執行用戶執行腳本, 安裝數據庫到數據庫存放目錄
/apps/mysql/scripts/mysql_install_db --user=mysql --datadir=/apps/mysql/data
15.啓動mysqld服務
/apps/mysql/support-files/mysql.server start ps aux | grep mysql
備註:若是啓動有報錯,能夠看下日誌報錯信息。
tail -n20 /apps/mysql/logs/mysql-error.log
16.執行該命令提升數據庫安全性,要先啓動mysql服務,主要是用來設定root密碼、刪除匿名用戶、取消遠程連接、刪除測試數據庫等。
/apps/mysql/bin/mysql_secure_installation
17.登陸mysql服務器
mysql -uroot -ptest123
18.編寫mysql服務腳本加入systemd服務
vim /etc/systemd/system/mysqld.service
[Unit] Description=MariaDB Server After=network.target remote-fs.target nss-lookup.target [Service] Type=forking User=mysql Group=mysql PIDFile=/apps/mysql/run/mysql.pid ExecStart=/apps/mysql/support-files/mysql.server start ExecReload=/apps/mysql/support-files/mysql.server reload ExecStop=/apps/mysql/support-files/mysql.server stop [Install] WantedBy=multi-user.target #按 Esc 鍵退出編輯模式,輸入 :wq 保存並關閉mysqld.service文件。
19.賦予權限設定開機自啓動
chmod 754 /etc/systemd/system/mysqld.service systemctl start mysqld && systemctl enable mysqld