MySQL 的安裝方式通常分爲三種,二進制版本、編譯版本、RPM 包。比較常見的是二進制版本安裝,方便簡單,相對於編譯安裝,若是不是追求極致性能,使用起來差異不大。本次教程以二進制版本爲例,系統爲 centos6.8,MySQL 版本爲5.7.20。mysql
先去官網下載二進制安裝包,進入官網下載頁面 mysql下載頁面,點擊下載 5.7.20 二進制 64 位版本linux
下載完成,把安裝包移到目標 Linux 下sql
yum install libaio numactl -y
groupadd mysql useradd -m -r -g mysql mysql
咱們通常使用 mysql 用戶啓動數據庫,若是不設置文件打開數和進程數,後期會有問題數據庫
打開 /etc/security/limits.conf
文件,在文件末尾寫上vim
mysql soft nproc 65536 mysql hard nproc 65536 mysql soft nofile 65536 mysql hard nofile 65536
打開 /etc/security/limits.d/90-nproc.conf
把參數調整爲centos
* soft nproc 65536 root soft nproc unlimited * soft nofile 65536 root soft nofile unlimited
若是不設置 hosts 本機解析,偶然會出現一些詭異的問題,因此仍是加上比較好安全
host_ip=$(ifconfig | grep inet | grep cast | awk '{print $2}' | awk -F: '{print $NF}' | head -1) echo "$host_ip `hostname`" >> /etc/hosts
時間同步對於數據庫來講很是重要,請保證全部數據庫節點時間是一致的服務器
yum install -y ntp service ntpd start
數據庫通常不容許通外網,若是對防火牆不是很熟悉,能夠關掉; 若是網絡環境不夠安全,能夠開啓網絡
service iptables stop chkconfig iptables off
selinux 可能會形成沒法寫入數據,若是對它不熟悉,建議關掉socket
查看 selinux 狀態
getenforce
臨時關閉
setenforce 0
永久關閉
打開 /etc/sysconfig/selinux
SELINUX=enforcing 改成 SELINUX=disabled
此次安裝數據目錄爲 /data/mysql_db/
,日誌目錄爲 /data/mysql_log/
,實例名爲 mysql_test
,啓動用戶爲 mysql
把二進制文件解壓出來,放到 /user/local/
目錄下
tar -xvzf mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz mv mysql-5.7.20-linux-glibc2.12-x86_64 /usr/local/mysql57
若是想要全局訪問到 mysql 客戶端,能夠把 /usr/local/mysql57/bin
加入系統路徑,我這裏採起直接建立軟鏈接的方式
ln -s /usr/local/mysql57/bin/mysql /usr/bin/mysql ln -s /usr/local/mysql57/bin/mysqlbinlog /usr/bin/mysqlbinlog
mkdir -p /data/mysql_db/mysql_test mkdir -p /data/mysql_log/mysql_test
/usr/local/mysql57/bin/mysqld --initialize-insecure --basedir=/usr/local/mysql57 --datadir=/data/mysql_db/mysql_test --user=mysql /usr/local/mysql57/bin/mysql_ssl_rsa_setup --basedir=/usr/local/mysql57 --datadir=/data/mysql_db/mysql_test
正常輸出
[root@mysql-test ~]# /usr/local/mysql57/bin/mysqld --initialize-insecure --basedir=/usr/local/mysql57 --datadir=/data/mysql_db/mysql_test --user=mysql 2018-06-03T05:19:31.469841Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). 2018-06-03T05:19:32.525091Z 0 [Warning] InnoDB: New log files created, LSN=45790 2018-06-03T05:19:32.751099Z 0 [Warning] InnoDB: Creating foreign key constraint system tables. 2018-06-03T05:19:32.861647Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: b2d883be-66ed-11e8-bd72-90b11c1a653a. 2018-06-03T05:19:32.877177Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened. 2018-06-03T05:19:32.877705Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option. [root@mysql-test ~]# /usr/local/mysql57/bin/mysql_ssl_rsa_setup --basedir=/usr/local/mysql57 --datadir=/data/mysql_db/mysql_test Generating a 2048 bit RSA private key ............................................................................+++ ....+++ writing new private key to 'ca-key.pem' ----- Generating a 2048 bit RSA private key ......................................+++ ...........................+++ writing new private key to 'server-key.pem' ----- Generating a 2048 bit RSA private key ..............................................................................+++ ..................................................+++ writing new private key to 'client-key.pem' -----
一臺機器上有可能會存在多個 MySQL 實例,咱們能夠把 my.cnf 文件放到對應實例的數據目錄下,因爲 MySQL 會受到默認配置文件的影響,因此咱們要把 /etc/my.cnf
移走, 而後建立新的 my.cnf
文件
rm -f /etc/my.cnf vim /data/mysql_db/mysql_test/my.cnf
本次安裝參考配置,具體參數結合服務器狀況調整
# my.cnf [client] port = 3306 default-character-set =utf8 socket = /data/mysql_db/mysql_test/mysql.sock [mysqld] datadir = /data/mysql_db/mysql_test basedir = /usr/local/mysql57 tmpdir = /tmp socket = /data/mysql_db/mysql_test/mysql.sock pid-file = /data/mysql_db/mysql_test/mysql.pid skip-external-locking = 1 skip-name-resolve = 1 port = 3306 server_id = 1 default-storage-engine = InnoDB character-set-server = utf8 default_password_lifetime=0 #### log #### binlog_cache_size = 16M log_bin = /data/mysql_log/mysql_test/mysql-bin log_bin_index = /data/mysql_log/mysql_test/mysql-bin.index binlog_format = row expire_logs_days = 15 relay_log_recovery=ON relay_log=/data/mysql_log/mysql_test/mysql-relay-bin relay_log_index=/data/mysql_log/mysql_test/mysql-relay-bin.index log_error = /data/mysql_log/mysql_test/mysql-error.log log_queries_not_using_indexes = /data/mysql_log/mysql_test/nouseindex.log slow_query_log = 1 long_query_time = 1 slow_query_log_file = /data/mysql_log/mysql_test/mysql-slow.log #### innodb #### innodb_buffer_pool_size = 4G innodb_buffer_pool_instances = 8 innodb_log_group_home_dir = /data/mysql_log/mysql_test/ innodb_undo_directory = /data/mysql_log/mysql_test/ innodb_undo_logs = 128 innodb_flush_neighbors = 1 innodb_log_file_size = 1G innodb_file_per_table = on bulk_insert_buffer_size = 64M myisam_sort_buffer_size = 64M myisam_max_sort_file_size = 1G myisam_repair_threads = 1 log_timestamps=system [mysqldump] quick max_allowed_packet = 64M [myisamchk] key_buffer_size = 32M sort_buffer_size = 32M read_buffer = 16M write_buffer = 16M
cp /usr/local/mysql57/support-files/mysql.server /etc/init.d/mysqld-test vim /etc/init.d/mysqld-test
把
basedir= datadir= lock_file_path="$lockdir/mysql" $bindir/mysqld_safe --datadir="$datadir" --pid-file="$mysqld_pid_file_path" $other_args >/dev/null &
改成, 手動指定配置文件和數據目錄
basedir=/usr/local/mysql57 datadir=/data/mysql_db/mysql_test default_file=/data/mysql_db/mysql_test/my.cnf lock_file_path="$lockdir/mysql-test" $bindir/mysqld_safe --defaults-file="$default_file" --datadir="$datadir" --pid-file="$mysqld_pid_file_path" $other_args >/dev/null 2>&1 &
把數據目錄的全部者改成 mysql
chown mysql.mysql /etc/init.d/mysqld-test chown mysql.mysql -R /data/mysql*
們以 mysql 用戶來啓動
su - mysql [mysql@chengqm ~]$ /etc/init.d/mysqld-test start Starting MySQL.. SUCCESS!
進入 mysql 客戶端
mysql -S /data/mysql_db/mysql_test/mysql.sock
到此,MySQL 已經成功安裝並啓動, 後續能夠修改密碼. 若是有報錯,查看錯誤日誌,根據具體報錯信息進行修改。若是報錯日誌裏面什麼也沒有,多是配置文件有問題,或者讀到了默認配置。