使用的是阿里雲服務器
創建存放的文件夾
mkdir mysql
進入mysql文件夾下並下載指定版本壓縮包
此處我下載的是5.7.29這是官網最新的GC版本
解壓
tar -zxvf mysql-5.7.29-linux-glibc2.12-x86_64.tar.gz
修改剛剛解壓後的文件夾爲mysql5729(文件夾名自己選定,本人只是不想文件夾名稱太長,強迫症)
在當前文件夾下創建data文件夾,我的數據庫解壓後的內容所在目錄是/develop/mysql/mysql5729
mkdir data
執行命令後創建了目錄/develop/mysql/data
添加組
groupadd mysql
添加用戶
useradd -r -g mysql mysql
權限
chown -R mysql:mysql /develop/mysql/mysql5729
初始化
/develop/mysql/mysql5729/bin/mysqld --initialize --user=mysql --basedir=/develop/mysql/mysql5729/ --datadir=/develop/mysql/data/
執行後,此處會產生臨時密碼需要記錄下來,在更改密碼時需要使用
開啓SSL
/develop/mysql/mysql5729/bin/mysql_ssl_rsa_setup --datadir=/develop/mysql/data/
授權
chown -R mysql:mysql /develop/mysql/data/
配置my.cnf
進入配置文件所在目錄:cd /etc
執行命令:vim my.cnf
後的文件原樣如下:
我的配置如下:
[mysqld]
#設置mysql的安裝目錄
basedir=/develop/mysql/mysql5729
#設置mysql數據庫的數據存放目錄
datadir=/develop/mysql/data
socket=/develop/mysql/data/mysql.sock
pid-file=/develop/mysql/data/mysql.pid
port = 3306
user = mysql
bind-address = 0.0.0.0
server-id = 1
#character config
character-set-client-handshake = FALSE
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
init_connect='SET NAMES utf8mb4'
skip-name-resolve
#skip-networking
back_log = 300
max_connections = 1277
max_connect_errors = 6000
open_files_limit = 65535
table_open_cache = 1024
max_allowed_packet = 500M
binlog_cache_size = 1M
max_heap_table_size = 8M
tmp_table_size = 128M
read_buffer_size = 2M
read_rnd_buffer_size = 8M
sort_buffer_size = 8M
join_buffer_size = 8M
key_buffer_size = 256M
thread_cache_size = 64
query_cache_type = 1
query_cache_size = 64M
query_cache_limit = 2M
ft_min_word_len = 4
log_bin = mysql-bin
binlog_format = mixed
expire_logs_days = 7
log_error = /data/mysql/mysql-error.log
#slow_query_log = 1
#long_query_time = 1
#slow_query_log_file = /data/mysql/mysql-slow.log
performance_schema = 0
explicit_defaults_for_timestamp
#lower_case_table_names = 1
default_storage_engine = InnoDB
innodb_file_per_table = 1
innodb_open_files = 500
innodb_buffer_pool_size = 1024M
innodb_write_io_threads = 4
innodb_read_io_threads = 4
innodb_thread_concurrency = 0
innodb_purge_threads = 1
innodb_flush_log_at_trx_commit = 2
innodb_log_buffer_size = 2M
innodb_log_file_size = 32M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 90
innodb_lock_wait_timeout = 120
bulk_insert_buffer_size = 8M
myisam_sort_buffer_size = 64M
myisam_max_sort_file_size = 10G
myisam_repair_threads = 1
interactive_timeout = 28800
wait_timeout = 28800
[mysqldump]
max_allowed_packet = 500M
[mysqld]
secure_file_priv=''
[myisamchk]
key_buffer_size = 256M
sort_buffer_size = 8M
read_buffer = 4M
[client]
default-character-set = utf8mb4
[mysql]
#設置默認字符集
default-character-set = utf8mb4
# Disabling symbolic-links is recommended to prevent assorted security risks
# #禁用符號連接
symbolic-links=0
skip_symbolic_links=yes
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
[mysqld_safe]
log-error=/develop/mysql/log/mariadb.log
pid-file=/develop/mysql/data/mysql.pid
啓動mysql服務
service mysqld start
出現錯誤:Failed to start mysqld.service: Unit not found.
然後瞭解到MariaDB代替了mysql數據庫,安裝MariaDB
# yum install mariadb -y //如果已安裝可以省略
# systemctl start mariadb.service //啓動服務
執行systemctl start mariadb.service時又出現另外一個問題:
Job for mariadb.service failed because the control process exited with error code. See "systemctl status mariadb.service" and "journalctl -xe" for details.
解決辦法:執行命令cp /usr/share/mysql/my-huge.cnf /etc/my.cnf
cp: overwrite ‘/etc/my.cnf’? y
#systemctl start mariadb.service
發現mariadb服務已經啓動起來了
# systemctl enable mariadb.service //開機啓動服務
然後啓動MySQL,發現還是不行
接着,查詢/etc/init.d/下是否存在mysql:
ll /etc/init.d/ | grep mysql
發現該目錄下並沒有mysql的文件
查詢mysql.server所在處
find / -name mysql.server
mysql.server一般在你安裝的mysql目錄下,自己是編譯安裝,所以知道目錄在哪裏,不知道的這樣看一下
執行復制操作,/develop/mysql/mysql5729/是我自己的mysql安裝目錄,
cp /develop/mysql/mysql5729/support-files/mysql.server /etc/init.d/mysql
然後執行命令:service mysql start
出現了另外一個錯誤:Starting MySQL...The server quit without updating PID file [FAILED]p/mysql/mysql5729/data/mysql.pid).
這時候,很明顯是配置文件配錯了。
最後修改配置文件如下:
[mysqld]
#設置mysql的安裝目錄
basedir=/develop/mysql/mysql5729
#設置mysql數據庫的數據存放目錄
[mysqld]
datadir=/develop/mysql/data
port = 3306
user = mysql
bind-address = 0.0.0.0
server-id = 1
#character config
character-set-client-handshake = FALSE
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
init_connect='SET NAMES utf8mb4'
[mysqld]
socket=/var/lib/mysql/mysql.sock
[client]
socket=/var/lib/mysql/mysql.sock
[client]
default-character-set = utf8mb4
[mysql]
default-character-set = utf8mb4
# Disabling symbolic-links is recommended to prevent assorted security risks
# #禁用符號連接
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
#pid-file=/var/run/mariadb/mariadb.pid
pid-file=/develop/mysql/data/mysql.pid
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
再次執行命令:service mysql start,mysql啓動成功!
最後記錄一下如果啓動數據庫出現Starting MySQL...The server quit without updating PID file [FAILED]p/mysql/mysql5729/data/mysql.pid).時不要慌,仔細檢查配置文件my.cnf,大多數時候都是因爲配置文件有問題了。本人曾經就因爲這個問題死活無法啓動服務器的mysql,差點重裝。兩次都是因爲配置文件出錯了。