文檔結構圖以下:mysql
1、前期規劃linux
操做系統:RedHat Linux 6.7 64位c++
數 據 庫:MYSQL5.6.38/5.7.20sql
MySQL 5.6:初始化數據時須要進到家目錄的 script 目錄下shell
執行:數據庫
/usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql/ --datadir=/data/mysql --defaults-file=/etc/my3306.cnf --user=mysqlvim
個人環境執行以下:windows
./mysql_install_db --basedir=/usr/local/mysql5.6 --datadir=/mydata/mysql/mysql3306/data --defaults-file=/etc/my3306.cnf --user=mysql安全
此時數據庫密碼爲空。服務器
MySQL 5.7:初始化數據時須要進到家目錄的 bin 目錄下
執行:
/usr/local/mysql/bin/mysqld --user=mysql --datadir=/data/mysql --basedir=/usr/local/mysql/ --initialize
已然已經廢棄了使用 mysql_install_db 這個命令進行初始化數據的操做了。
個人環境執行以下:
/usr/local/mysql5.7/bin/mysqld --defaults-file=/etc/my3307.cnf --basedir=/usr/local/mysql5.7 --datadir=/mydata/mysql/mysql3307/data/ --user=mysql --initialize
注:--initialize 會自動生成密碼在 error log 裏面。若是加 --initialize-insecure 密碼爲空
service iptables stop
chkconfig iptables off
vi /etc/sysconfig/selinux
setenforce 0
查看IPTABLES 當前狀態與關閉過程
chkconfig --list|grep iptables
也能夠根據須要開放端口。
先查看是否安裝sendmail的相關組件。
rpm -qa | grep sendmail
若是已安裝sendmail插件:
service sendmail stop
service sendmail stop
chkconfig sendmail off
查看IO 調度文件:
cat /sys/block/sda/queue/scheduler
修改I/O調度器;須要在 /etc/grub.conf 加入elevator=deadline,保證永久有效。
Deadline(截止時間調度程序)
特色:
經過時間以及硬盤區域進行分類,這個分類和合並要求相似於noop的調度程序.
Deadline確保了在一個截止時間內服務請求,這個截止時間是可調整的,而默認讀期限短於寫期限.這樣就防止了寫操做由於不能被讀取而餓死的現象.
Deadline對數據庫環境(ORACLE RAC,MYSQL等)是最好的選擇.
Swappiness值大小對如何使用swap分區有着很大的影響。
它有0和100兩個極限值,0表明最大限度的使用物理內存,而後才使用swap分區,這種行爲有可能致使系統內存溢出,出現OOM的錯誤,從而致使mysql被意外KILL掉,因此須要謹慎設置。
100則是積極的使用swap分區,而且把內存上面的數據及時搬到swap分區裏(不建議)。這裏建議你們不分配swap,或者分配4GB的空間就足夠了。
查看swappiness文件
cat /proc/sys/vm/swappiness
vm.swappiness默認爲60,設置爲0表示儘可能少使用swap,100表示儘可能將inactive的內存頁交換到swap裏或者釋放cache。inactive內存的意思是程序映射着,可是」長時間」不用的內存。咱們能夠利用vmstat查看系統裏面有多少inactive的內存。
# vmstat -a 1
這個值推薦設置爲5,設置方法以下,在/etc/sysctl.conf文件中增長一行。
vm.swappiness = 5
sysctl -p生效。
推薦使用xfs文件系統,相比ext4,它更方便管理,支持動態擴容,刪除文件也方便。(redhat 7默認xfs)
查看操做系統的限制狀況:
ulimit -a
open files 若是設置不合理,當前服務器的鏈接過多或者表過多時,就有可能出現打不來表或者訪問不了表的現象,默認狀況下,Linux的最大句柄數爲1024個,表示單個進程最多能夠訪問1024個文件句柄。若是超過默認值,就會出現文件句柄超限的錯誤「too much open files」。
max user processes 參數的用途是,有時候咱們可能會跑對個實例,可是發現建立不了新的鏈接,報出「resource temprarily unavaliable」的錯誤,表示沒有足夠的資源。
爲防止以上兩種報錯狀況,咱們能夠修改系統的軟件限制。
vi /etc/security/limits.conf
* soft nproc 65535
* hard nproc 65535
* soft nofile 65535
* hard nofile 65535
關閉numa功能能夠更好地分配內存,不須要採用swap的方式來獲取內存;有經驗的系統管理員和DBA都知道使用swap致使的數據庫性能降低影響很大。
語句以下:
具體狀況具體修改
方法一:
numactl --interleave=all /usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf &
個人環境是:
numactl --interleave=all /usr/local/mysql5.6/bin/mysqld_safe --defaults-file=/etc/my3306.cnf &
numactl --interleave=all /usr/local/mysql5.7/bin/mysqld_safe --defaults-file=/etc/my3307.cnf &
方法二:
vi /etc/grub.conf
numa=off
保存,重啓服務器。
yum -y install make gcc-c++ cmake bison-devel ncurses-devel glibc
先把軟件包傳到 /usr/local/ 目錄下
groupadd mysql
useradd -g mysql mysql -s /sbin/nologin
設置mysql登入密碼:passwd mysql
-s 指定它的shell
-d 指定用戶主目錄
-g 用戶組 指定用戶所屬的用戶組。
-G 用戶組,用戶組 指定用戶所屬的附加組。
-M:不要自動創建用戶的登入目錄。
passwd mysql --能夠不用設置密碼,由於MYSQL用戶不登陸。
cd /usr/local/
tar -zxvf mysql-5.6.38-linux-glibc2.12-x86_64.tar.gz
ln -s mysql-5.6.38-linux-glibc2.12-x86_64 mysql5.6 目錄能夠本身設置
創建目錄:
mkdir -p /mydata/mysql/mysql3306/{data,logs} --生產中建議用項目名+端口號
修改權限:
chown -R mysql:mysql /mydata/mysql/mysql3306/
chown -R mysql:mysql /usr/local/mysql5.6
chown -R mysql:mysql /mydata
設置環境變量:
修改方法一:
echo $PATH
export PATH=$PATH:/usr/local/mysql5.6/bin
修改方法二:不推薦用此方法
echo "export PATH=$PATH:/usr/local/mysql5.6/bin" >> /etc/profile
或者用
vim /etc/profile
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/mysql5.6/bin
source /etc/profile
準備my3306.cnf 文件,能夠本身命名成想要的文件名稱。
阿里雲是5.6.16版本
cpu 8核 內存32G innodb_buffer_pool_size 24G;具體設置看狀況修改參數,沒有分離出undo,tmp,測試環境能夠自定義修改innodb_data_file_path,innodb_buffer_pool_size等參數的大小;也可參照姜老師的文件。
[client]
port = 3306
socket = /tmp/mysqlclient.sock
default-character-set=utf8
[mysql]
default-character-set=utf8
[mysqld]
port = 3306
socket = /tmp/mysql3306.sock
basedir = /usr/local/mysql5.6
datadir = /mydata/mysql/mysql3306/data
open_files_limit = 3072
back_log = 103
max_connections = 512
max_connect_errors = 100000
table_open_cache = 512
external-locking = FALSE
max_allowed_packet = 128M
sort_buffer_size = 2M
join_buffer_size = 2M
thread_cache_size = 51
query_cache_size = 32M
tmp_table_size = 96M
max_heap_table_size = 96M
slow_query_log = 1
slow_query_log_file = /mydata/mysql/mysql3306/logs/slow.log
log-error = /mydata/mysql/mysql3306/logs/error.log
log_bin_trust_function_creators=1
long_query_time = 0.05
server-id = 1323306
log-bin = /mydata/mysql/mysql3306/logs/mysql-bin
sync_binlog = 1
binlog_cache_size = 4M
max_binlog_cache_size = 128M
max_binlog_size = 1024M
expire_logs_days = 7
key_buffer_size = 32M
read_buffer_size = 1M
read_rnd_buffer_size = 16M
bulk_insert_buffer_size = 64M
character-set-server=utf8
default-storage-engine=InnoDB
binlog_format=row
#gtid_mode=on
#log_slave_updates=1
#enforce_gtid_consistency=1
interactive_timeout=100
wait_timeout=100
transaction_isolation = REPEATABLE-READ
innodb_additional_mem_pool_size = 16M
innodb_buffer_pool_size = 1434M
innodb_data_file_path = ibdata1:1024M:autoextend
innodb_flush_log_at_trx_commit = 1
innodb_log_buffer_size = 16M
innodb_log_file_size = 256M
innodb_log_files_in_group = 2
innodb_max_dirty_pages_pct = 50
innodb_file_per_table = 1
innodb_locks_unsafe_for_binlog = 0
[mysqldump]
quick
max_allowed_packet = 32M
cd /usr/local/mysql5.6/scripts/
./mysql_install_db --basedir=/usr/local/mysql5.6 --datadir=/mydata/mysql/mysql3306/data --defaults-file=/etc/my3306.cnf --user=mysql
以下:
兩次OK
啓動mysql5.6
/usr/local/mysql5.6/bin/mysqld_safe --defaults-file=/etc/my3306.cnf &
查看mysql進程,驗證是否啓動成功:
注意:
Mysql 讀取配置文件的順序是
/etc/my.cnf /etc/mysql/my.cnf /usr/local/mysql/etc/my.cnf ~/.my.cnf
MYSQL關閉:
/usr/local/mysql5.6/bin/mysqladmin -S /tmp/mysql3306.sock shutdown
本地登錄:
mysql -S /tmp/mysql3306.sock
一、修改數據庫root密碼
[root@laodeng5 mysql-5.6.15]# mysqladmin -u root password 'mysql'
或者
方法二:
mysql>set password for 'root'@'localhost'=password('mysql');
mysql>flush privileges;
登錄方式:
mysql -S /tmp/mysql3306.sock -uroot -p'mysql'
關閉數據庫:
mysqladmin -S /tmp/mysql3306.sock shutdown -uroot –p’mysql’
二、5.7以前須要安全加固
show databases;
select user,host,password from mysql.user; --實際就須要user localhost,其它均可以刪掉
delete from mysql.user where user != 'root' or host != 'localhost';
mysql> grant all privileges on *.* to root@'%' identified by "password"; --windows 只容許本地訪問,須要給root賦權
Query OK, 0 rows affected (0.00 sec)
三、刪除測試庫(可選)
select * from mysql.db\G
truncate table mysql.db;
drop database test;
flush privileges;
怎麼確認mysql是否啓動起來的?
A.查看進程是否存在
ps aux|grep mysqld
B.查看端口是否是監聽正確
netstat -nlap|grep mysqld
ss -lnpt | grep mysqld
C.查看錯誤日誌有無異常
vim
tail
less
設置大小寫不敏感:
mysql> show variables like '%case%';
+------------------------+-------+
| Variable_name | Value |
+------------------------+-------+
| lower_case_file_system | OFF |
| lower_case_table_names | 1 |
+------------------------+-------+
注意:lower_case_table_names=1,參數爲靜態參數,須要在my.cnf配置後進去重啓生效。
MYSQL5.7已經廢棄了mysql_install_db 這個初始化命令了。
先把軟件包傳到 /usr/local/ 目錄下
groupadd mysql
useradd -g mysql mysql -s /sbin/nologin
設置mysql登入密碼:passwd mysql --能夠不用設置密碼,由於MYSQL用戶不登陸。
-s 指定它的shell
-d 指定用戶主目錄
-g 用戶組 指定用戶所屬的用戶組。
-G 用戶組,用戶組 指定用戶所屬的附加組。
-M:不要自動創建用戶的登入目錄。
passwd mysql
cd /usr/local/
tar -zxvf mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz
ln -s mysql-5.7.20-linux-glibc2.12-x86_64 mysql5.7
mkdir -p /mydata/mysql/mysql3307/{data,logs}
修改權限:
chown -R mysql:mysql /mydata/mysql/mysql3307/
chown -R mysql:mysql /usr/local/mysql5.7
chown -R mysql:mysql /mydata
設置環境變量:
修改方法一:
echo $PATH
export PATH=$PATH:/usr/local/mysql5.7/bin
修改方法二:不推薦用此方法
echo "export PATH=$PATH:/usr/local/mysql5.7/bin" >> /etc/profile
或者用
vim /etc/profile
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/mysql5.7/bin
source /etc/profile
準備my3307.cnf 文件,能夠本身命名成想要的文件名稱。參數按照實際狀況進行修改。
[client]
port = 3307
socket = /tmp/mysql5.7.sock
default-character-set=utf8
[mysql]
prompt="\u@db \R:\m:\s [\d]> "
no-auto-rehash
[mysqld]
user = mysql
port = 3307
basedir = /usr/local/mysql5.7
datadir = /mydata/mysql/mysql3307/data
socket = /tmp/mysql3307.sock
log_bin_trust_function_creators=1
character-set-server = utf8mb4
event_scheduler=1
sql_mode=STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
skip_name_resolve = 1
open_files_limit = 65535
back_log = 1024
max_connections = 500
max_connect_errors = 1000000
table_open_cache = 1024
table_definition_cache = 1024
table_open_cache_instances = 64
thread_stack = 512K
external-locking = FALSE
max_allowed_packet = 32M
sort_buffer_size = 4M
join_buffer_size = 4M
thread_cache_size = 768
query_cache_size = 0
query_cache_type = 0
interactive_timeout = 600
wait_timeout = 600
tmp_table_size = 32M
max_heap_table_size = 32M
slow_query_log = 1
slow_query_log_file = /mydata/mysql/mysql3307/logs/slow.log
log-error = /mydata/mysql/mysql3307/logs/error.log
long_query_time = 0.1
server-id = 3307101
log-bin = /mydata/mysql/mysql3307/logs/mysql-binlog
sync_binlog = 1
binlog_cache_size = 4M
max_binlog_cache_size = 1G
max_binlog_size = 1G
expire_logs_days = 7
master_info_repository = TABLE
relay_log_info_repository = TABLE
gtid_mode = on
enforce_gtid_consistency = 1
log_slave_updates
binlog_format = row
relay_log_recovery = 1
relay-log-purge = 1
key_buffer_size = 32M
read_buffer_size = 8M
read_rnd_buffer_size = 4M
bulk_insert_buffer_size = 64M
lock_wait_timeout = 3600
explicit_defaults_for_timestamp = 1
innodb_thread_concurrency = 0
innodb_sync_spin_loops = 100
innodb_spin_wait_delay = 30
transaction_isolation = REPEATABLE-READ
innodb_buffer_pool_size = 1024M
innodb_buffer_pool_instances = 8
innodb_buffer_pool_load_at_startup = 1
innodb_buffer_pool_dump_at_shutdown = 1
innodb_data_file_path = ibdata1:1G:autoextend
innodb_flush_log_at_trx_commit = 1
innodb_log_buffer_size = 32M
innodb_log_file_size = 2G
innodb_log_files_in_group = 2
innodb_max_undo_log_size = 4G
innodb_io_capacity = 2000
innodb_io_capacity_max = 4000
innodb_flush_neighbors = 0
innodb_write_io_threads = 8
innodb_read_io_threads = 8
innodb_purge_threads = 4
innodb_page_cleaners = 4
innodb_open_files = 65535
innodb_max_dirty_pages_pct = 50
innodb_flush_method = O_DIRECT
innodb_lru_scan_depth = 4000
innodb_checksum_algorithm = crc32
innodb_lock_wait_timeout = 10
innodb_rollback_on_timeout = 1
innodb_print_all_deadlocks = 1
innodb_file_per_table = 1
innodb_online_alter_log_max_size = 4G
internal_tmp_disk_storage_engine = InnoDB
innodb_stats_on_metadata = 0
innodb_status_file = 1
innodb_status_output = 0
innodb_status_output_locks = 0
performance_schema = 1
performance_schema_instrument = '%=on'
#innodb monitor
innodb_monitor_enable="module_innodb"
innodb_monitor_enable="module_server"
innodb_monitor_enable="module_dml"
innodb_monitor_enable="module_ddl"
innodb_monitor_enable="module_trx"
innodb_monitor_enable="module_os"
innodb_monitor_enable="module_purge"
innodb_monitor_enable="module_log"
innodb_monitor_enable="module_lock"
innodb_monitor_enable="module_buffer"
innodb_monitor_enable="module_index"
innodb_monitor_enable="module_ibuf_system"
innodb_monitor_enable="module_buffer_page"
innodb_monitor_enable="module_adaptive_hash"
[mysqldump]
quick
max_allowed_packet = 32M
cd /usr/local/mysql5.7/bin/
./mysqld --defaults-file=/etc/my3307.cnf --basedir=/usr/local/mysql5.7 --datadir=/mydata/mysql/mysql3307/data/ --user=mysql --initialize
初始化加上initialize參數,表示會生成一個臨時的數據庫初始化密碼,記錄在log-error(錯誤日誌)裏面,若是加上--initialize-insecure參數,表明無密碼進入。建議使用生成的初始化密碼方式。
啓動mysql5.7
/usr/local/mysql5.7/bin/mysqld_safe --defaults-file=/etc/my3307.cnf &
查看生成的密碼文件:
cat /mydata/mysql/mysql3307/logs/error.log |grep password
登錄並修改密碼:
/usr/local/mysql5.7/bin/mysql -S /tmp/mysql3307.sock -uroot -p'ogdf8ru<r,nI'
設置ROOT密碼永不過時:
alter user 'root'@'localhost' password expire never;
設置ROOT 能夠從其餘客戶端或者服務器登錄:
mysql> update mysql.user set Host='%' where User='root';
Query OK, 0 rows affected (0.00 sec)
Rows matched: 1 Changed: 0 Warnings: 0
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
mysql> select Host,User from mysql.user;
+-----------+---------------+
| Host | User |
+-----------+---------------+
| % | root |
| localhost | mysql.session |
| localhost | mysql.sys |
+-----------+---------------+
3 rows in set (0.00 sec)
設置大小寫不敏感:
mysql> show variables like '%case%';
+------------------------+-------+
| Variable_name | Value |
+------------------------+-------+
| lower_case_file_system | OFF |
| lower_case_table_names | 1 |
+------------------------+-------+
注意:lower_case_table_names=1,參數爲靜態參數,須要在my.cnf配置後進去重啓生效。
mysql> grant all privileges on *.* to root@'%' identified by "password"; --windows 只容許本地訪問,須要給root賦權
Query OK, 0 rows affected (0.00 sec)
binlog2sql 工具的安裝。