做者:北京運維
常見的 MySQL 安裝方式有以下三種:html
OS 版本 | MySQL 版本 |
---|---|
CentOS 7.5.1804 | 5.7.25 |
訪問 MySQL 官網,下載最新版 mysql5.7 的 rpm 包。node
下載 Bundle 包解壓之後,能夠看到包含了全部 MySQL 相關的 RPM 包:mysql
其中 client、common、libs、server 四個程序包是必須安裝的:linux
mysql-community-client-5.7.25-1.el7.x86_64.rpm mysql-community-common-5.7.25-1.el7.x86_64.rpm mysql-community-libs-5.7.25-1.el7.x86_64.rpm mysql-community-server-5.7.25-1.el7.x86_64.rpm
在執行安裝以前,先檢查是否已經安裝過(CentOS7 之後默認安裝的 mariadb)c++
$ rpm -qa|egrep "mariadb|mysql" mariadb-libs-5.5.60-1.el7_5.x86_64 # 我這裏存在 mariadb-libs 會形成衝突,因此卸載掉 rpm -e --nodeps mariadb-libs-5.5.60-1.el7_5.x86_64 # 卸載以後就能夠進行安裝使用 yum 或者 rpm -ivh $ yum -y install mysql-community-client-5.7.25-1.el7.x86_64.rpm mysql-community-common-5.7.25-1.el7.x86_64.rpm mysql-community-libs-5.7.25-1.el7.x86_64.rpm mysql-community-server-5.7.25-1.el7.x86_64.rpm
安裝完成後 MySQL 的默認配置文件爲 /etc/my.cnf
接下來咱們就能夠啓動 MySQL 啦sql
$ systemctl start mysqld.service $ systemctl enable mysqld.service $ systemctl status mysqld.service
MySQL 5.7 之後,不在容許使用空密碼進行登陸,默認會初始化一個密碼到 MySQL Error 日誌中,配置參數 log-error=
指定的文件。數據庫
$ cat /var/log/mysqld.log | grep password 2019-03-20T02:44:49.359004Z 1 [Note] A temporary password is generated for root@localhost: /qrsXHttL6Mr
鏈接實例並修改默認密碼vim
$ mysql -uroot -p mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.7.25 Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> set password for 'root'@'localhost'=password('MyNewPass4!'); Query OK, 0 rows affected, 1 warning (0.00 sec)
之後經過 update set 語句修改密碼:安全
mysql> use mysql; mysql> update user set authentication_string=PASSWORD('NewPass@2019') where user='root'; mysql> flush privileges;
注意:mysql 5.7 默認密碼檢查策略要求密碼必須包含:大小寫字母、數字和特殊符號,而且長度不能少於8位。不然會提示 ERROR 1819 (HY000): Your password does not satisfy the current policy requirements 錯誤。查看 MySQL 密碼策略bash
官方文檔:https://dev.mysql.com/doc/refman/5.7/en/binary-installation.html
選擇 Linux - generic 64 位安裝包
MySQL 依賴於 libaio 庫。 若是未在本地安裝此庫,則數據目錄初始化和後續服務器啓動步驟將失敗。 若有必要,請使用適當的包管理器進行安裝。 例如,在基於Yum 的系統上:
$ yum -y install libaio
建立 MySQL 用戶和組
$ groupadd mysql $ useradd -r -g mysql -s /bin/false mysql
解壓到指定目錄
$ tar xf mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz -C /usr/local/ $ cd /usr/local/ $ ln -sv mysql-5.7.25-linux-glibc2.12-x86_64/ mysql
修改解壓目錄下全部文件屬主及屬組
$ cd /usr/local/mysql $ chown -R root.mysql ./*
建立數據目錄,以 /data/mysql/data 爲例
$ mkdir -pv /data/mysql/{data,log} $ chown -R mysql.mysql /data/mysql
準備 MySQL 配置文件,我這裏用的是在線工具生成的 my.cnf 文件,工具連接
$ cat /etc/my.cnf [client] port = 3306 socket = /data/mysql/mysql.sock [mysqld] port = 3306 socket = /data/mysql/mysql.sock pid_file = /var/run/mysql.pid datadir = /data/mysql/data basedir = /usr/local/mysql default_storage_engine = InnoDB max_allowed_packet = 128M 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 = 128M innodb_log_file_size = 128M innodb_file_per_table = 1 innodb_flush_log_at_trx_commit = 0 key_buffer_size = 16M log-error = /data/mysql/log/mysql_error.log log-bin = /data/mysql/log/mysql_bin.log slow_query_log = 1 slow_query_log_file = /data/mysql/log/mysql_slow_query.log long_query_time = 5 tmp_table_size = 16M max_heap_table_size = 16M query_cache_type = 0 query_cache_size = 0 server-id=1
複製啓動腳本
$ cp support-files/mysql.server /etc/init.d/mysqld $ chmod +x /etc/init.d/mysqld $ chkconfig --add mysqld
初始化數據庫
$ ./bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql/data
此時會生成一個臨時密碼,能夠在 mysql_error.log 文件找到
$ grep password /data/mysql/log/mysql_error.log 2019-03-20T05:37:28.267207Z 1 [Note] A temporary password is generated for root@localhost: H_wgkXR&f1=t
生成 SSL
$ ./bin/mysql_ssl_rsa_setup --basedir=/usr/local/mysql --datadir=/data/mysql/data/
啓動 MySQL 服務
$ service mysqld start $ ss -tnlp | grep 3306
配置 MySQL 環境變量
$ vim /etc/profile.d/mysql.sh export PATH=/usr/local/mysql/bin:$PATH $ source /etc/profile.d/mysql.sh
$ 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 # 是否修改 root 密碼,剛纔已經新設置了,輸入 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 # 是否禁止 root 用戶遠程登陸 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 # 是否刪除 test 測試數據庫 - 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!
驗證 MySQL 安裝
mysqladmin version -u root -p
$ yum -y install gcc gcc-c++ ncurses ncurses-devel cmake bison
安裝 Boost 庫,獲取程序包請訪問 Boost 官網
$ cd /usr/local/src/ $ wget https://sourceforge.net/projects/boost/files/boost/1.59.0/boost_1_59_0.tar.gz $ tar xf boost_1_59_0.tar.gz -C /usr/local/
$ cd /usr/local/src/ $ wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.25.tar.gz
$ groupadd mysql $ useradd -r -g mysql -s /bin/false mysql
$ tar xf mysql-5.7.25.tar.gz $ cd mysql-5.7.25 $ cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \ -DMYSQL_DATADIR=/data/mysql/data \ -DENABLED_LOCAL_INFILE=1 \ -DWITH_BOOST=/usr/local/boost_1_59_0 \ -DDEFAULT_CHARSET=utf8 \ -DDEFAULT_COLLATION=utf8_general_ci \ -DEXTRA_CHARSETS=all \ -DWITH_MYISAM_STORAGE_ENGINE=1 \ -DWITH_INNOBASE_STORAGE_ENGINE=1 \ -DWITH_ARCHIVE_STORAGE_ENGINE=1 \ -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \ -DWITH_FEDERATED_STORAGE_ENGINE=1 \ -DWITH_PARTITION_STORAGE_ENGINE=1 \ -DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \ -DWITH_CSV_STORAGE_ENGINE=1 \ -DWITH_HEAP_STORAGE_ENGINE=1 \ -DWITH_MYISAMMRG_STORAGE_ENGINE=1 \ -DWITH_ZLIB=system \ -DWITH_EMBEDDED_SERVER=1
更多 cmake 指令參考官方文檔
$ make -j `grep processor /proc/cpuinfo | wc -l` $ make install
準備 MySQL 配置文件,我這裏用的是在線工具生成的 my.cnf 文件,工具連接
$ cat /etc/my.cnf [client] port = 3306 socket = /data/mysql/mysql.sock [mysqld] port = 3306 socket = /data/mysql/mysql.sock pid_file = /var/run/mysql.pid datadir = /data/mysql/data basedir = /usr/local/mysql default_storage_engine = InnoDB max_allowed_packet = 128M 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 = 128M innodb_log_file_size = 128M innodb_file_per_table = 1 innodb_flush_log_at_trx_commit = 0 key_buffer_size = 16M log-error = /data/mysql/log/mysql_error.log log-bin = /data/mysql/log/mysql_bin.log slow_query_log = 1 slow_query_log_file = /data/mysql/log/mysql_slow_query.log long_query_time = 5 tmp_table_size = 16M max_heap_table_size = 16M query_cache_type = 0 query_cache_size = 0 server-id=1
建立數據目錄
$ mkdir -pv /data/mysql/{log,data} $ chown -R mysql.mysql /data/mysql/
$ cd /usr/local/mysql/ $ ./bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql/data/
$ cp support-files/mysql.server /etc/init.d/mysqld $ chmod +x /etc/init.d/mysqld $ echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh source /etc/profile.d/mysql.sh
$ systemctl enable mysqld $ systemctl start mysqld $ systemctl status mysqld $ ss -tnlp|grep 3306 $ ps aux|grep mysql
與二進制方式同樣,初始密碼在錯誤日誌內。
$ mysql_secure_installation
$ mysqladmin version -uroot -p
以上就是 MySQL 5.7 版本的三種安裝方式,歡迎你們多留言交流。