一. 準備工作 1 刪除本地centos7中的mariadb: 查看系統中是否已安裝 mariadb 服務: rpm -qa | grep mariadb 或 yum list installed | grep mariadb 如果已安裝則刪除 mariadb及其依賴的包: yum -y remov ...
一. 準備工作
1 刪除本地centos7中的mariadb:
查看系統中是否已安裝 mariadb 服務:
rpm -qa | grep mariadb
或
yum list installed | grep mariadb
如果已安裝則刪除 mariadb及其依賴的包:
yum -y remove mariadb-libs-5.5.44-2.el7.centos.x86_64
關閉防火牆:
firewall-cmd --state #查看防火牆狀態
systemctl stop firewalld.service #關閉防火牆
systemctl is-enabled firewalld.service;echo $? #查看防火牆是否開機自啟
systemctl disable firewalld.service #禁止防火牆開機啟動
MySQL依賴於libaio 庫,安裝libaio
yum search libaio
yum install libaio
2.下載安裝包
mkdir /data
cd /data/
yum -y install wget
wget https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz
3. 解壓、重命名
tar -zxvf mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz -C /data
mv /data/mysql-5.7.20-linux-glibc2.12-x86_64/ /data/mysql57
mkdir mysql #數據目錄與安裝目錄分離,便於以後MySQL升級
二. 安裝MySQL
1.添加用戶和組
groupadd mysql
useradd mysql -g mysql
2.配置文件
/etc/my.cnf
主要參數及取值如下:
(相關取值,請根據實際環境做調整,如記憶體配置參數等)
####### [client] socket = /data/mysql/data/mysql.sock default-character-set=utf8mb4 #loose-local-infile=0 [mysqld] log_bin_trust_function_creators=1 secure_file_priv='/tmp' ########dir settings######## basedir = /data/mysql57 datadir = /data/mysql/data pid-file = /data/mysql/data/mysql.pid socket = /data/mysql/data/mysql.sock tmpdir = /data/mysql/tmp log-bin = /data/mysql/mysql_log/mysql-bin log-error = /data/mysql/mysql_log/mysql.err general_log_file=/data/mysql/mysql_log/mysql.genlog slow_query_log_file = /data/mysql/mysql_log/mysql_slow_new.log relay_log = /data/mysql/mysql_log/relay.log ########server setting####### #sql_mode = "ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION" server-id = 2022 port = 3306 character-set-server=utf8mb4 collation_server=utf8mb4_unicode_ci lower_case_table_names = 0 skip_name_resolve = 1 #max_connect_errors = 1000 max_connections = 2000 thread_cache_size=256 #thread_stack= 262144 #back_log=80 max_allowed_packet = 134217728 event_scheduler = 1 local-infile=0 # lower_case_table_names = 1 explicit_defaults_for_timestamp = 1 expire_logs_days = 15 log_bin_trust_function_creators =1 ####.frm/.ibd files qty related open_files_limit=65535 innodb_open_files=65535 table_open_cache=65535 table_definition_cache=65535 #### seesion buffer related read_buffer_size = 262144 read_rnd_buffer_size = 524288 sort_buffer_size = 8388608 join_buffer_size = 8388608 ####memory table size tmp_table_size =67108864 max_heap_table_size=67108864 ####timeout interactive_timeout = 1800 wait_timeout = 1800 # connect_timeout=10 ########slow query ######## slow_query_log = 1 log_slow_slave_statements = 1 log_queries_not_using_indexes = 1 log_throttle_queries_not_using_indexes = 10 long_query_time = 1 #min_examined_row_limit = 10000 ########innodb settings######## innodb_buffer_pool_size = 193273528320 innodb_buffer_pool_instances = 16 innodb_buffer_pool_dump_pct = 40 innodb_lru_scan_depth = 2048 innodb_page_cleaners = 16 #innodb_purge_threads = 4 innodb_sort_buffer_size = 67108864 #innodb_file_per_table = 1 #innodb_flush_log_at_trx_commit = 1 innodb_undo_log_truncate = 1 innodb_undo_tablespaces = 3 innodb_max_undo_log_size = 2147483648 innodb_purge_rseg_truncate_frequency = 128 innodb_log_file_size = 1073741824 innodb_log_files_in_group = 3 innodb_log_buffer_size = 16777216 innodb_flush_method = O_DIRECT innodb_flush_neighbors = 0 innodb_print_all_deadlocks = 1 innodb_strict_mode = 1 #innodb_lock_wait_timeout = 50 innodb_io_capacity = 32768 innodb_io_capacity_max = 65536 innodb_thread_concurrency = 32 innodb_write_io_threads = 8 innodb_read_io_threads = 8 ########replication settings######## master_info_repository = TABLE relay_log_info_repository = TABLE gtid_mode = on enforce_gtid_consistency = 1 binlog_gtid_simple_recovery=1 relay_log_recovery = 1 slave-parallel-type = LOGICAL_CLOCK slave-parallel-workers = 16 slave_transaction_retries=128 slave_preserve_commit_order=1 log_slave_updates=1 binlog_format = ROW log_timestamps=system binlog_rows_query_log_events = 1 binlog_row_image='full' slave_skip_errors = ddl_exist_errors ########semi sync replication settings######## ##plugin_dir=/data/mysql/plugin/ #plugin_load = "rpl_semi_sync_master=semisync_master.so;rpl_semi_sync_slave=semisync_slave.so" #rpl_semi_sync_master_enabled = 1 #rpl_semi_sync_master_timeout = 5000 #rpl_semi_sync_slave_enabled = 1 [mysqldump] quick
說明:
請一定記得上面 配置模板文件中的server-id 參數一定要修改。
否則在配置 主從複製,或者MHA時會報錯。
例如錯誤信息 :
2018-10-09T16:13:53.205115+08:00 11 [ERROR] [MY-013117] [Repl] Slave I/O for channel '': Fatal error: The slave I/O thread stops because master and slave have equal MySQL server ids; these ids must be different for replication to work (or the --replicate-same-server-id option must be used on slave but this does not always make sense; please check the manual before using it). Error_code: MY-013117
3. 創建目錄並授權
cd /data/mysql
mkdir mysql_log
chown -R mysql.mysql mysql_log/
mkdir tmp
chown -R mysql.mysql tmp/
說明:
有時候,可能我們只是將MYSQL的二進位文件從其它的伺服器Copy至本地。
例如我們在安裝MYSQL8.0 時,安裝文件從其它的服務,Copy到本地,文件命名為mysql80。如下圖:
此時還需要執行以下許可權,所以 我手動增加了一下許可權,執行代碼如下:
chmod -R 755 /data/mysql80/bin/
4.初始化MySQL
/data/mysql57/bin/mysqld --defaults-file=/etc/my.cnf --initialize --user=mysql --basedir=/data/mysql57 --datadir=/data/mysql/data
說明:
主參數--defaults-file=/etc/my.cnf必須緊接在mysqld後面,如果做第二個以後的參數加入時,則會出現報錯:
[ERROR] unknown variable 'defaults-file=/etc/my.cnf'!
註意密碼:5.7初始化後會生成一個臨時密碼,存在mysql.err
[Note] A temporary password is generated for root@localhost: XXXXXXXX
也就是root@localhost:後的字元串。
5.添加到MySQL啟動腳本到系統服務,並且設置開啟啟動
cp -a /data/mysql57/support-files/mysql.server /etc/init.d/mysqld
設置開機啟動和檢查是否成功
chkconfig mysqld on
chkconfig --list | grep mysqld
6. 創建mysql軟鏈接至/usr/bin下
ln -s /data/mysql57/bin/mysql /usr/bin
7.啟動MySQL服務
service mysqld start
說明:
如果還是使用的Copy文件的方式,例如我們前面 Copy的mysql80 二進位文件, 此處就會報錯:
此時 還要執行以下賦權命令 :
chmod -R 755 ./mysql.server (這個文件是 /data/mysql80/support-files)
和
chmod -R 755 /etc/init.d/mysqld
8. 登錄MySQL,修改root密碼
mysql -uroot -pXXXXXXX mysql> set password=password('XXXXXXX2');
說明 :
MySQL 8.0 是報錯的:
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'password('XXXXX')' at line 1
在登錄MySQL,修改root密碼時,使用就方法 SET PASSWORD=PASSWORD(‘[新密碼]’) 報錯。
查看網上的相關解釋為:
以前版本的MySQL的密碼認證插件是“mysql_native_password”,而現在使用的是“caching_sha2_password”。
解決方案:
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '新密碼';
FLUSH PRIVILEGES;