安裝依賴
yum install -y libaio
下載
wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz -O mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz
創建目錄
mkdir -p /data/service/mysql
解壓
tar zxvf mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz
mv mysql-5.7.18-linux-glibc2.5-x86_64/* /data/service/mysql
創建用戶組
groupadd mysql
useradd -g mysql -s /sbin/nologin mysql
修改權限
chown -R mysql:mysql /data/service/mysql
配置環境變量
cat <<EOF > /etc/profile.d/mysql.sh export PATH=/data/service/mysql/bin:\$PATH EOF
. /etc/profile
目錄規划(由於mysql特殊,必須使用在/usr/local/mysql的路徑才行,那么采取的方法是軟鏈接,不建議直接安裝到/usr/local/mysql的路徑,遷移是個很麻煩的問題)
# 數據datadir /usr/local/mysql/data # 參數文件my.cnf /usr/local/mysql/etc/my.cnf # 錯誤日志log-error /usr/local/mysql/log/mysql_error.log # 二進制日志log-bin /usr/local/mysql/binlogs/mysql-bin # 慢查詢日志slow_query_log_file /usr/local/mysql/log/mysql_slow_query.log # 套接字socket文件 /usr/local/mysql/run/mysql.sock # pid文件 /usr/local/mysql/run/mysql.pid
mkdir -p /data/service/mysql/{binlogs,log,etc,run} mkdir -p /data/database ln -s /data/service/mysql /usr/local/mysql ln -s /data/database /usr/local/mysql/data chown -R mysql.mysql /data/service/mysql/ chown -R mysql.mysql /usr/local/mysql/{data,binlogs,log,etc,run}
設置配置文件
rm -rf /etc/my.cnf #原文件是mariadb客戶端留下的,不需要使用
cat <<EOF > /usr/local/mysql/etc/my.cnf [client] port = 3306 socket = /usr/local/mysql/run/mysql.sock [mysqld] port = 3306 socket = /usr/local/mysql/run/mysql.sock pid_file = /usr/local/mysql/run/mysql.pid datadir = /usr/local/mysql/data default_storage_engine = InnoDB max_allowed_packet = 512M max_connections = 2048 open_files_limit = 65535 skip-name-resolve lower_case_table_names=1 character-set-server = utf8mb4 collation-server = utf8mb4_unicode_ci init_connect='SET NAMES utf8mb4' innodb_buffer_pool_size = 1024M innodb_log_file_size = 2048M innodb_file_per_table = 1 innodb_flush_log_at_trx_commit = 0 key_buffer_size = 64M log-error = /usr/local/mysql/log/mysql_error.log log-bin = /usr/local/mysql/binlogs/mysql-bin slow_query_log = 1 slow_query_log_file = /usr/local/mysql/log/mysql_slow_query.log long_query_time = 5 tmp_table_size = 32M max_heap_table_size = 32M query_cache_type = 0 query_cache_size = 0 server-id=1 EOF
初始化
mysqld --initialize --user=mysql --datadir=/data/database --basedir=/data/service/mysql # 此時會輸出臨時密碼,一定要記住 echo "請記住這個臨時root密碼!!!"
生成ssl
mysql_ssl_rsa_setup --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data/
設置啟動項目
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql.server chkconfig --add mysql.server chkconfig mysql.server on
啟動
service mysql.server start
重置root密碼,此時關閉
mysql_secure_installation
類似以下信息:
Securing the MySQL server deployment. Enter password for user root: The existing password for the user account root has expired. Please set a new password. New password: Re-enter new password: VALIDATE PASSWORD PLUGIN can be used to test passwords and improve security. It checks the strength of password and allows the users to set only those passwords which are secure enough. Would you like to setup VALIDATE PASSWORD plugin? Press y|Y for Yes, any other key for No: Y There are three levels of password validation policy: LOW Length >= 8 MEDIUM Length >= 8, numeric, mixed case, and special characters STRONG Length >= 8, numeric, mixed case, special characters and dictionary file Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 2 Using existing password for root. Estimated strength of the password: 100 Change the password for root ? ((Press y|Y for Yes, any other key for No) : N ... skipping. By default, a MySQL installation has an anonymous user, allowing anyone to log into MySQL without having to have a user account created for them. This is intended only for testing, and to make the installation go a bit smoother. You should remove them before moving into a production environment. Remove anonymous users? (Press y|Y for Yes, any other key for No) : Y Success. Normally, root should only be allowed to connect from 'localhost'. This ensures that someone cannot guess at the root password from the network. Disallow root login remotely? (Press y|Y for Yes, any other key for No) : Y Success. By default, MySQL comes with a database named 'test' that anyone can access. This is also intended only for testing, and should be removed before moving into a production environment. Remove test database and access to it? (Press y|Y for Yes, any other key for No) : Y - Dropping test database... Success. - Removing privileges on test database... Success. Reloading the privilege tables will ensure that all changes made so far will take effect immediately. Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y Success. All done!
目的是:
- 重置密碼
- 刪除匿名用戶
- 關閉root用戶的遠程登錄
- 刪除測試數據庫
導入時區
mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root -p mysql
全自動腳本:
https://github.com/easonjim/centos-shell/blob/master/mysql/install-mysql_5.7.18.sh
參考:
https://www.jianshu.com/p/0d628b2f7476(這哥們的教程算是整個網上最全的)
