mysql學習4:第二章:mysql安裝啟動和關閉——mysql5.7安裝

studywell發表於2018-11-05

接上一節,本次系統環境為centos7.4 x64


 

1.1.   mysql5.7的安裝

安裝方式和mysql5.6一樣,區別就是廢棄了mysql_install_db的初始化命令。

1.1.1.   建立mysql使用者並指定mysql使用者組。

建立使用者並指定使用者組

groupadd mysql

useradd -g mysql mysql -s /sblin/nologin

centos7.4 已預設建立mysql使用者

1.1.2.   解壓安裝包

軟體包家目錄統一放在/usr/local

# cd / usr/local

解壓mysql軟體包

 [root@localhost local]# tar -zxvf /orasoft/mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz注意:書中示例版本為mysql5.7.14,本次實驗環境為mysql5.7.24

重新命名解壓目錄為mysql;

[root@localhost local]# mv mysql-5.7.24-linux-glibc2.12-x86_64 mysql

給mysql目錄授權

chown mysql:mysql -R mysql

1.1.3.   建立資料庫目錄

建立mysql資料庫的資料目錄datadir,建立在/data/mysql下面;

mkdir -p /data/mysql

chown mysql:mysql -R /data/mysql

1.1.4.   配置檔案

由於是二進位制安裝方式 ,資料庫配置檔案需要自己配置好。配置檔案如下:

 

[root@localhost mysql]# cat /etc/my.cnf

[client]

port = 3306

socket = /tmp/mysql.sock

default-character-set=utf8

[mysql]

prompt="\u@db \R:m:\s [\d]>"

#no-auto-rehash

auto-rehash

[mysqld]

port=3306

basedir=/usr/local/mysql

socket=/tmp/mysql.sock

datadir=/data/mysql

character-set-server=utf8mb4

skip_name_resolve=1

open_files_limit=65535

back_log=1024

max_connections=512

max_connect_errors=100000

table_open_cache=1024

table_definition_cache=1024

table_open_cache_instances=64

thread_stack=512K

external-locking=FALSE

max_allowed_packet=128M

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=/data/mysql/slow.log

log-error=/data/mysql/error.log

long_query_time=0.5

server-id=3306100

log-bin=/data/mysql/mysql-binlog

sync_binlog=1

binlog_cache_size=4M

max_binlog_cache_size=1G

max_binlog_size=1024M

expire_logs_days=7

master_info_repository=TABLE

relay_log_info_repository=TABLE

gtid_mode=on

enforce_gtid_consistency=1

log_slave_updates=1

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

default-storage-engine=InnoDB

interactive_timeout=300

wait_timeout=300

transaction_isolation=REPEATABLE-READ

innodb_thread_concurrency=0

innodb_sync_spin_loops=100

innodb_spin_wait_delay=30

innodb_buffer_pool_size=1434M

innodb_buffer_pool_instances=8

innodb_buffer_pool_load_at_startup=1

innodb_buffer_pool_dump_at_shutdown=1

innodb_data_file_path=ibdata1:1024M: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_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_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"

# 後增加

pid-file=/data/mysql/mysql.pid

[mysqldump]

quick

max_allowed_packet=32M

 

 

備註

命令自動補全功能

關閉:no-auto-rehash

開啟:auto-rehash

1.1.5.   初始化資料庫

[root@localhost ~]# cd /usr/local/mysql/bin

# ./mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --user=mysql --initialize

 

不需要引數:--defaults-file,預設引數檔案就是/etc/my.cnf

初始化過程中加-initialize引數,生成隨機密碼寫入log-error中;

如加上--initialize-insecure引數,則無密碼寫入。

 

在log-err日誌中,記錄生成的隨機密碼

[root@localhost mysql]# cat error.log |grep password

2018-11-05T01:53:36.883537Z 1 [Note] A temporary password is generated for root@localhost: Z/22rKZWIGvh

 

1.1.6.   新增mysql啟動指令碼到系統服務

新增到MySQL 啟動指令碼到系統服務

# cp support-files/mysql.server /etc/init.d/mysqld

# chmod 755 /etc/init.d/mysqld

 

(*注意,mysql二進位制安裝預設的路徑為/usr/local/mysql,啟動指令碼里/usr/local/mysql需要替換,如安裝位置變換為/data/mysql56

>sed -i 's#/usr/local/mysql#/data/mysql56#g' /data/mysql56/bin/mysqld_safe /etc/init.d/mysqld  )

 

配置環境變數

# echo 'export PATH=/usr/local/mysql/bin:$PATH' >> /etc/profile

# source /etc/profile

 

新增到系統服務,可用系統啟動

# service mysqld start

新增自動啟動

> chkconfig --add mysqld

> chkconfig mysqld on

> chkconfig --list mysqld

 

1.1.7.   啟動MySQL服務

啟動指令碼有兩個分別是:

/usr/local/mysql/bin/mysqld_safe &

/usr/local/mysql/support-files/mysql.server(即/etc/init.d/mysqld)

 

當啟動mysqld時,mysqld_safe同時啟動

mysqld_safe監控mysqld服務,記錄錯誤日誌,並在mysqld因故障停止時將其重啟

 

啟動方式一

$ bin/mysqld_safe --user=mysql &

啟動方式二

$ service mysql.server start

或者

/usr/local/mysql/support-files/mysql.server start

 

1.1.8.   設定MySQL密碼

用初始密碼登陸,修改root密碼並修改為永不過期。

[root@localhost etc]# mysql -uroot -pZ/22rKZWIGvh

mysql> alter user 'root'@'localhost' identified by 'root123';

mysql> SET PASSWORD ='root123';

mysql> alter user 'root'@'localhost' password expire never;

mysql>flush privileges;

mysql> exit;

檢視mysql.user表中存在哪些帳戶 以及它們的密碼是否為空:

MySQL 5.7.6起,使用這個語句:

mysql> SELECT User, Host, HEX(authentication_string) FROM mysql.user;

+---------------+-----------+-------------------------------------------+

| User          | Host      | HEX(authentication_string)                                                         |

+---------------+-----------+------------------------------------------------------------------------------------+

| root          | localhost | 2A39383730334637413534333934344644333831383037373636394637344436303631364442324338 |

| mysql.session | localhost | 2A5448495349534E4F544156414C494450415353574F52445448415443414E42455553454448455245 |

| mysql.sys     | localhost | 2A5448495349534E4F544156414C494450415353574F52445448415443414E42455553454448455245 |

+---------------+-----------+------------------------------------------------------------------------------------+

 

1.1.9.   開啟遠端登入

以許可權使用者root登入

# mysql -uroot -proot123mysql> use mysql;

mysql> use mysql;

mysql> update user set host = '%' where user ='root';

或者

mysql> grant all privileges on *.*  to  'root'@'%'  identified by 'mima'  with grant option;

mysql> flush privileges;

 

[mysql]>SELECT User, Host, HEX(authentication_string) FROM mysql.user;

+---------------+-----------+------------------------------------------------------------------------------------+

| User          | Host      | HEX(authentication_string)                                              |

+---------------+-----------+------------------------------------------------------------------------------------+

| root          | %         | 2A46414146464536343445393031434641464145433735363234313545354641454332343342384232 |

| mysql.session | localhost | 2A5448495349534E4F544156414C494450415353574F52445448415443414E42455553454448455245 |

| mysql.sys     | localhost | 2A5448495349534E4F544156414C494450415353574F52445448415443414E42455553454448455245 |

+---------------+-----------+------------------------------------------------------------------------------------+

 

1.1.10.        生成RSA私鑰

可以跳過此步驟,可選,以後研究。

mysql_ssl_rsa_setup需要openssl支援,用於啟用資料量ssl連線,需要進一步配置。

參考-MySQL 使用 SSL 連線:

$ bin/mysql_ssl_rsa_setup

 


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29519108/viewspace-2218717/,如需轉載,請註明出處,否則將追究法律責任。

相關文章