MySQL下載地址與Centos7安裝MySQL以及啓動問題排查

啓動問題(如The server quit without updating PID file)請查看最後的附錄進行解決mysql

做者:晨星1032linux

1、MySQL國內鏡像下載

2、國內鏡像相關站點

只列取部分(其餘相關軟件可進入站點進行下載)sql

3、Centos7安裝MySQL5.7

1. 下載並解壓至/usr/local

鏡像站下載(以版本mysql-5.7.31-linux-glibc2.12-x86_64爲例)

wget http://mirrors.ustc.edu.cn/mysql-ftp/Downloads/MySQL-5.7/mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz

解壓至/usr/local

tar -xvf mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz -C /usr/local/

重命名

cd /usr/local
mv mysql-5.7.31-linux-glibc2.12-x86_64/ mysql

2. 配置信息

新建my.cnf

mkdir -p /usr/local/mysql/etc
vi /usr/local/mysql/etc/my.cnf

在my.cnf中填入如下內容

[mysqld]
user=mysql
server-id=1
port = 3306
character_set_server=utf8mb4

datadir=/mnt/mysql/mysql_data
tmpdir=/mnt/mysql/mysql_tmp

# 日誌
log-error=/mnt/mysql/log-error.txt

default-storage-engine=INNODB

join_buffer_size = 512M
tmp_table_size = 1G
max_allowed_packet = 64M
# 365
interactive_timeout = 31536000
# 24.86
wait_timeout = 2147483
read_buffer_size = 128M
read_rnd_buffer_size = 256M
sort_buffer_size = 256M
key_buffer_size=512M
back_log=500
flush_time=0
open_files_limit=4161
table_definition_cache=1400
binlog_row_event_max_size=16M

# 最大鏈接數
max_connections=1000
max_connect_errors = 1000

# 線程緩存大小
thread_cache_size=500

# 日誌緩衝刷新的頻繁程度
innodb_flush_log_at_trx_commit=2
innodb_buffer_pool_size=2G
innodb_log_file_size=512M
innodb_log_buffer_size=256M
innodb_thread_concurrency=32
innodb_autoextend_increment=64
innodb_buffer_pool_instances=8
innodb_concurrency_tickets=5000
innodb_old_blocks_time=1000
innodb_open_files=300
innodb_stats_on_metadata=0
innodb_file_per_table=1
innodb_checksum_algorithm=0
innodb_lock_wait_timeout = 50

# 瓶頸位
query_cache_size = 0

slow-query-log=1
long_query_time=10

lower_case_table_names=1
table_open_cache=4096

autocommit = 1
skip_name_resolve = 1
transaction_isolation = READ-COMMITTED
explicit_defaults_for_timestamp = 1

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

注:

  1. my.cnf配置文件的位置在:/usr/local/mysql/etc/

  2. 啓動錯誤日誌在:/mnt/mysql/log-error.txt

  3. MySQL的wait_timeout鏈接已經修改成最大值,不會出現相關超時問題

3. 用戶及用戶組管理(提升安全)

新建組和用戶

groupadd mysql
useradd -g mysql mysql -s /usr/sbin/nologin

建立數據目錄(與my.cnf一致)

mkdir -p /mnt/mysql/mysql_data
mkdir -p /mnt/mysql/mysql_tmp

添加權限

chmod 644 /usr/local/mysql/etc/my.cnf
chmod 750 /mnt/mysql/mysql_data
chmod 750 /mnt/mysql/mysql_tmp

chown -R mysql:mysql /usr/local/mysql
chown -R mysql:mysql /mnt/mysql/mysql_data
chown -R mysql:mysql /mnt/mysql/mysql_tmp

其餘權限設置

touch /var/log/mariadb/mariadb.log
chown -R mysql:mysql /var/lib/mysql
chown -R mysql:mysql  /var/log/mariadb/mariadb.log

4. 初始化數據庫

進入目錄

cd /usr/local/mysql/

初始化數據

./bin/mysqld --initialize

如有錯誤發生

  1. message:error while loading shared libraries: libnuma.so.1: cannot open shared object file: No such file or directory

    solution:yum -y install numactl.x86_64

  2. message: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory

    solution:yum -y install libaio

5. 啓動數據庫

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

中止
/usr/local/mysql/support-files/mysql.server stop

重啓
/usr/local/mysql/support-files/mysql.server restart

若啓動有錯誤發生
如:The server quit without updating PID file (/mnt/mysql/mysql_data/xx.pid)

!!首先查看啓動錯誤日誌信息:/mnt/mysql/log-error.txt

根據啓動日誌進行排查問題(直接百度通常都沒啥用,切記根據日誌進行定點查問題)

以及/var/log/mariadb/mariadb.log的日誌

6. 默認密碼修改

(1)先中止mysql服務

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

# 並查看進程,如有則kill
ps -ef|grep mysqld

​ 結果如圖所示

(2)啓動 mysql 進入無需受權模式

./bin/mysqld --defaults-file=/usr/local/mysql/etc/my.cnf --skip-grant-tables --console

(3)新開一個窗口,並進入mysql目錄下,執行如下命令連上數據庫

./bin/mysql

(4)執行相關sql命令

-- 切換庫
use mysql;
-- 更新密碼爲123456
update user set authentication_string=password("123456"),host = '%', password_expired='N'  where user="root";

-- 查詢結果
select user, authentication_string, host, password_expired from user;

FLUSH PRIVILEGES;
exit

7. 防火牆開放端口設置

開放端口

firewall-cmd --zone=public --add-port=3306/tcp --permanent
firewall-cmd --reload

或者直接關閉防火牆(本地推薦)

# 啓動
systemctl start firewalld
# 關閉
systemctl stop firewalld
# 查看狀態
systemctl status firewalld

# 開機禁用
systemctl disable firewalld
# 開機啓用
systemctl enable firewalld

做者:晨星1032

附:啓動問題排查

如:The server quit without updating PID file (/mnt/mysql/mysql_data/xx.pid)

!!首先查看啓動錯誤日誌信息:/mnt/mysql/log-error.txt

  1. 添加日誌方法:
# 修改my.cnf配置文件,增長如下內容
log-error=/mnt/mysql/log-error.txt
  1. 同時對如下文件增長權限
touch /var/log/mariadb/mariadb.log
chown -R mysql:mysql /var/lib/mysql
chown -R mysql:mysql  /var/log/mariadb/mariadb.log

而後根據啓動日誌進行排查問題(直接百度通常都沒啥用,切記根據日誌進行定點查問題)

或者/var/log/mariadb/mariadb.log的日誌

若仍解決不了,可根據本文過程從新安裝

重裝MySQL過程數據庫遷移注意點以及數據恢復,請看下篇內容!

相關文章
相關標籤/搜索