Linux安裝MySQL標準教程

導讀:
本文主要介紹 CentOS 系統二進制安裝 MySQL 5.7.23 版本的安裝步驟,其餘版本安裝過程類似。mysql

1.前置準備

  • 卸載舊版MySQLlinux

    • 查看rpm包c++

      rpm -qa|grep mysql 如有可用rpm -e卸載sql

    • 查找mysql殘留包,有則刪除,沒有則忽略shell

      find / -name mysqlsocket

  • 安裝相關依賴ide

    yum -y install make gcc-c++ cmake bison-devel ncurses-devel numactl libaiocode

  • 建立用戶和用戶組orm

    groupadd mysql
    useradd -s /sbin/nologin -g mysql -M mysqlserver

2.下載二進制安裝包並解壓

cd /usr/local/
# wget下載或者本地下載後上傳
wget https://downloads.mysql.com/archives/get/file/mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz
# 解壓安裝包
tar -zxvf mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz
# 解壓後爲了方便後面操做可把解壓後文件名修改成mysql
mv mysql-5.7.23-linux-glibc2.12-x86_64 mysql
# 更改文件夾所屬
chown -R mysql.mysql /usr/local/mysql/

3.建立mysql相關目錄

mkdir -p /data/mysql/{data,logs,tmp}
# 更改文件夾所屬
chown -R mysql.mysql /data/mysql/

4.建立mysql配置文件my.cnf

vi /etc/my.cnf
# 簡單模板以下:
[client]
port            = 3306
socket          = /data/mysql/tmp/mysql.sock

[mysqld]
user = mysql
basedir = /usr/local/mysql        
datadir = /data/mysql/data  
port = 3306               

socket = /data/mysql/tmp/mysql.sock
pid-file  = /data/mysql/tmp/mysqld.pid
tmpdir = /data/mysql/tmp    
skip_name_resolve = 1
symbolic-links=0
max_connections = 2000
group_concat_max_len = 1024000
sql_mode = NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
lower_case_table_names = 1
log_timestamps=SYSTEM
character-set-server = utf8
interactive_timeout = 1800  
wait_timeout = 1800
max_allowed_packet = 32M
binlog_cache_size = 4M
sort_buffer_size = 2M
read_buffer_size = 4M
join_buffer_size = 4M
tmp_table_size = 96M
max_heap_table_size = 96M
max_length_for_sort_data = 8096

#logs
server-id = 1003306
log-error = /data/mysql/logs/error.log
slow_query_log = 1
slow_query_log_file = /data/mysql/logs/slow.log
long_query_time = 3
log-bin = /data/mysql/logs/binlog
binlog_format = row
expire_logs_days = 15
log_bin_trust_function_creators = 1
relay-log = /data/mysql/logs/relay-bin
relay-log-recovery = 1  
relay_log_purge = 1  

#innodb  
innodb_file_per_table = 1
innodb_log_buffer_size = 16M
innodb_log_file_size = 256M
innodb_log_files_in_group = 2
innodb_io_capacity = 2000
innodb_io_capacity_max = 4000
innodb_flush_neighbors = 0
innodb_flush_method = O_DIRECT
innodb_autoinc_lock_mode = 2
innodb_read_io_threads = 8
innodb_write_io_threads = 8
innodb_buffer_pool_size = 2G

5.配置mysql.server

cd /usr/local/mysql/support-files
cp mysql.server /etc/init.d/mysql
vi /etc/init.d/mysql
# 修改目錄位置
basedir=/usr/local/mysql
datadir=/data/mysql/data

# 註冊開機啓動服務
chkconfig --add mysql
chkconfig --list

6.添加環境變量

echo "PATH=$PATH:/usr/local/mysql/bin  " >> /etc/profile  
source /etc/profile

7.初始化mysql

/usr/local/mysql/bin/mysqld  --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql/data
# 臨時密碼保存在errlog中 
# 獲取臨時密碼
more /data/mysql/logs/error.log |grep password

8.啓動mysql服務 並修改密碼

# 啓動mysql服務
service mysql start
# 使用初始密碼登陸mysql服務 並修改密碼
mysql -uroot -p
alter user 'root'@'localhost' identified by 'root';
flush privileges;

歡迎關注

相關文章
相關標籤/搜索