1. 程式人生 > 實用技巧 >mysql8.0.22在centos7.6下的簡單安裝

mysql8.0.22在centos7.6下的簡單安裝

如果想把mysql安裝得好一些,則嚴重推薦使用壓縮包來安裝。

一般情況下,現在大部分的伺服器都是x86-64,少數是arm架構的。

選擇合適的版本,下載即可。

本文中,使用的是 mysql-8.0.22-el7-x86_64.tar.gz (這對centos7的)

為了便於管理,個人推薦的目錄

/soft/mysql/program --放myql程式檔案

/soft/mysql/config --放mysql的配置檔案,配置檔案可以隨意命名,例如my.ini,my.cnf,my.config之類都無所謂

當然也可以直接使用預設的/etc/my.cnf 路徑

下面是大概的步驟:

1.解壓檔案

2.寫好my.cnf

3.初始化

4.啟動服務,修改root密碼

5.把mysql配置為服務,以便自動啟動

這裡稍微說下2,3.

步驟2:寫my.cnf,重點幾個:

1)字符集

2)資料目錄

3)埠

4)是否使用root執行(很多時候,我們就用root執行了)

5)密碼驗證方式

6)是否忽略大小寫

7)是否關比bin_log(如果不復制,這個可以節約時間)

8)日誌目錄

下面是my.cnf的配置例子:

[client]
port=3318

[mysql]
no-beep
default-character-set=UTF8MB4

[mysqld]
user=root
skip-log-bin
port=3318
#不區分大小寫(linux設定,windows不設定。linux必須初始化的時候設定)
lower_case_table_names=1
# 程式路徑
basedir="/soft/mysql-8.0.22-el7-x86_64"

# 資料庫例項根資料路徑
datadir=/data/mysql

# 建立資料庫和表的預設字符集
character-set-server=UTF8MB4

# 預設儲存引擎
default-storage-engine=INNODB


# General and Slow logging.
log-output=FILE
slow-query-log=0
bulk_insert_buffer_size=32M

# Error 日誌
log-error="mysql.err"
 
# 最大併發會話
max_connections=500

# 所有執行緒可以允許開啟的表數量
table_open_cache=1200

# Maximum size for internal (in-memory) temporary tables. If a table
# grows larger than this value, it is automatically converted to disk
# based table This limitation is for a single table. There can be many
# of them.
# 記憶體臨時表大小--如果有許多需要臨時表的查詢,而且這些臨時表都挺大的,可以考慮設定大一些
# 當然前提,是您相對比較闊綽,可以有許多記憶體
tmp_table_size=32M

# How many threads we should keep in a cache for reuse. When a client
# disconnects, the client's threads are put in the cache if there aren't
# more than thread_cache_size threads from before.  This greatly reduces
# the amount of thread creations needed if you have a lot of new
# connections. (Normally this doesn't give a notable performance
# improvement if you have a good thread implementation.)
thread_cache_size=10


# Size of the buffer used for doing full table scans of MyISAM tables.
# Allocated per thread, if a full scan is needed.
read_buffer_size=0

read_rnd_buffer_size=0

#*** INNODB Specific options ***
# innodb_data_home_dir=0.0

# Use this option if you have a MySQL server with InnoDB support enabled
# but you do not plan to use it. This will save memory and disk space
# and speed up some things.
# skip-innodb

# If set to 1, InnoDB will flush (fsync) the transaction logs to the
# disk at each commit, which offers full ACID behavior. If you are
# willing to compromise this safety, and you are running small
# transactions, you may set this to 0 or 2 to reduce disk I/O to the
# logs. Value 0 means that the log is only written to the log file and
# the log file flushed to disk approximately once per second. Value 2
# means the log is written to the log file at each commit, but the log
# file is only flushed to disk approximately once per second.
#謹慎修改這個引數
innodb_flush_log_at_trx_commit=1

# The size of the buffer InnoDB uses for buffering log data. As soon as
# it is full, InnoDB will have to flush it to disk. As it is flushed
# once per second anyway, it does not make sense to have it very large
# (even with long transactions).
innodb_log_buffer_size=128m

#建議開啟嚴謹模式
innodb_strict_mode=on


#密碼驗證方式
default_authentication_plugin=mysql_native_password

步驟3:初始化

如果使用專有的mysql使用者啟動配置(這是推薦的)

bin/mysqld --defaults-file=/opt/mysql/mysql/etc/my.cnf --initialize --user=mysql

如果想用root且修改配置檔案路徑,則可以修改為

bin/mysqld --defaults-file=/data/mysql/config/my.cnf --initialize --user=root

安裝為服務

有時候不一定按照下面就可以安裝為服務,要看系統的情況。

注意:如果修改了路徑則必須修改mysql.server檔案

mysql.server一般在support-files下

以下是一般需要修改的片段:

# If you change base dir, you must also change datadir. These may get
# overwritten by settings in the MySQL configuration files.

basedir=/soft/mysql-8.0.22-el7-x86_64
datadir=/data/mysql

複製啟動檔案 cp mysql.server /etc/init.d/mysqld

賦予可執行許可權:# chmod +x /etc/init.d/mysqld

新增為服務:# chkconfig --add mysqld

之後可以使用 systemctl start/stop/status mysqld

也可以直接使用mysql.server start/stop等

最後,申明下,以上的配置僅僅能夠滿足一般的開發,要想滿足高效能的配置,遠遠不夠。

作為一個mysql dba,必須掌握以下知識:

1)mysql 開發

2) linux配置

3) mysql 配置

4)作業系統、網路