CentOS7.x編譯安裝mysql 5.7
mysql 5.7下載地址
社區版下載地址:https://dev.mysql.com/downloads/mysql/ 可能會有變動
找到5.7版本,
註:源碼安裝需要用到下面的包,可以先忽略,我安裝是提示用到的不是這個版本的包,而且無法解壓此包,安裝提示中用到的是此包boost_1_59_0.tar.gz,可以連接外網的的情況下,能自動下載安裝,沒有也不要擔心,會在報錯頁面,給出下載url地址,可以下載後本地上傳至服務器中。
或者通過wget下載
wget http://downloads.sourceforge.net/project/boost/boost/1.59.0/boost_1_59_0.tar.gz
wget http://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.12.tar.gz
環境準備
# cat /etc/centos-release
CentOS Linux release 7.6.1810 (Core)
# uname -r
3.10.0-957.el7.x86_64
# getenforce
Disabled
# systemctl status firewalld.service
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
Active: inactive (dead)
Docs: man:firewalld(1)
編譯MySQL
解決依賴關系
# yum -y install autoconf automake libtool cmake ncurses-devel openssl-devel lzo-devel zlib-devel gcc gcc-c++
註:下面所有的名利都要用絕對路徑避免出錯。
創建用戶MySQL啟動用戶
groupadd mysql
useradd -r -g mysql -s /bin/false -M mysql
解壓MySQL壓縮包
# tar xf mysql-5.7.25
# cd mysql-5.7.25
進入解壓目錄並將boost_1_59_0.tar.gz放入mysql-5.7.25
編譯安裝MySQL
cmake -DCMAKE_INSTALL_PREFIX=/application/mysql-5.7.25/ -DSYSCONFDIR=/application/mysql-5.7.25/ -DMYSQL_DATADIR=/application/mysqlData -DMYSQL_UNIX_ADDR=/tmp/mysql.sock -DMYSQL_TCP_PORT=3306 -DMYSQL_USER=mysql -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_READLINE=1 -DENABLED_LOCAL_INFILE=1 -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DDOWNLOAD_BOOST=1 -DWITH_BOOST=.
-DCMAKE_INSTALL_PREFIX=/application/mysql-5.7.25/ #安裝位置
-DSYSCONFDIR=/application/mysql-5.7.25/ #配置文件(my.cnf)目錄
-DMYSQL_DATADIR=/application/mysqlData #數據存儲目錄
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock #mysql.sock目錄
-DMYSQL_TCP_PORT=3306 #服務端口
-DMYSQL_USER=mysql #運行用戶
-DEXTRA_CHARSETS=all #擴展字符支持
-DDEFAULT_CHARSET=utf8 #默認字符集
-DDEFAULT_COLLATION=utf8_general_ci #默認字符校對
-DWITH_READLINE=1 #readline庫
-DENABLED_LOCAL_INFILE=1 #啟用加載本地數據
-DWITH_MYISAM_STORAGE_ENGINE=1 #使用Myisam存儲引擎
-DWITH_INNOBASE_STORAGE_ENGINE=1 #使用INNOBASE存儲引擎
-DWITH_PARTITION_STORAGE_ENGINE=1 #安裝數據庫分區
-DWITH_MEMORY_STORAGE_ENGINE=1 #安裝memory存儲引擎
-DDOWNLOAD_BOOST=1 #下載升壓文件boost_1_59_0.tar.gz
-DWITH_BOOST=. #指定boots文件位置,點表示當前位置,也可以用絕對路徑,開始時因為將文件上傳至此目錄內,所以用點指定即可。
註:註意磁盤空間,不滿足的情況下也是會編譯失敗的,最少給MySQL準備10G空間。
# make && make install
復制啟動程序
cp support-files/mysql.server /etc/init.d/mysqld
註:centos7中,init.d下的啟動文件要配置755權限,才可以使用,安裝包中含有systemctl啟動文件,可以用systemctl管理MySQL。
註:mysql。sock文件存放位置要設置成mysql:mysql用戶,不然會提示沒有sock文件。
設置mysql環境變量:
# echo "export PATH=/application/mysql/bin:/application/mysql/lib:$PATH" >>/root/.bash_profile
# source /root/.bash_profile
修改mysql權限
# chown -R mysql:mysql /application/*
創建軟連接,MySQL升級後可以指定新的目錄
# ln -s # ln -s /application/mysql-5.7.25 /application/mysql
MySQL初始化
# /application/mysql/bin/mysqld --initialize-insecure --user=mysql --basedir=/application/mysql --datadir=/application/mysqlData
l 之前版本mysql_install_db是在mysql_basedir/script下,5.7放在了bin目錄下,且已被廢棄,使用--initialize-insecure。
l –-initialize會生成一個隨機密碼(~/.mysql_secret),--initialize-insecure不會生成密碼
配置my.cnf
# vim /etc/my.cnf
[client]
port = 3306
socket = /var/lib/mysql/mysql.sock
default-character-set = utf8
[mysqld]
port = 3306
socket = /var/lib/mysql/mysql.sock
basedir = /application/mysql
datadir = /application/mysqlDate
pid-file = /application/mysqlDate/mysql.pid
user = mysql
bind-address = 0.0.0.0
server-id = 1
init-connect = ‘SET NAMES utf8mb4‘
character-set-server = utf8mb4
skip-name-resolve
#skip-networking
back_log = 300
max_connections = 1000
max_connect_errors = 6000
open_files_limit = 65535
table_open_cache = 128
max_allowed_packet = 4M
binlog_cache_size = 1M
max_heap_table_size = 8M
tmp_table_size = 16M
read_buffer_size = 2M
read_rnd_buffer_size = 8M
sort_buffer_size = 8M
join_buffer_size = 8M
key_buffer_size = 4M
thread_cache_size = 8
query_cache_type = 1
query_cache_size = 8M
query_cache_limit = 2M
ft_min_word_len = 4
log_bin = mysql-bin
binlog_format = mixed
expire_logs_days = 30
log_error = /application/mysqlDate/mysql-error.log
slow_query_log = 1
long_query_time = 1
slow_query_log_file = /application/mysqlDate/mysql-slow.log
performance_schema = 0
explicit_defaults_for_timestamp
#lower_case_table_names = 1
skip-external-locking
default_storage_engine = InnoDB
#default-storage-engine = MyISAM
innodb_file_per_table = 1
innodb_open_files = 500
innodb_buffer_pool_size = 64M
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 = 8M
myisam_max_sort_file_size = 10G
myisam_repair_threads = 1
interactive_timeout = 28800
wait_timeout = 28800
[mysqldump]
quick
max_allowed_packet = 16M
[myisamchk]
key_buffer_size = 8M
sort_buffer_size = 8M
read_buffer = 4M
write_buffer = 4M
啟動MySQL
# /etc/init.d/mysqld restart
# ps -ef |grep mysql
root 11478 1 0 14:13 pts/0 00:00:00 /bin/sh /application/mysql/bin/mysqld_safe --datadir=/application/mysqlDate --pid-file=/application/mysqlDate/mysql.pid
mysql 12320 11478 8 14:13 pts/0 00:00:00 /application/mysql/bin/mysqld --basedir=/application/mysql --datadir=/application/mysqlDate --plugin-dir=/application/mysql/lib/plugin --user=mysql --log-error=/application/mysqlDate/mysql-error.log --open-files-limit=65535 --pid-file=/application/mysqlDate/mysql.pid --socket=/var/lib/mysql/mysql.sock --port=3306
# netstat -antup|grep mysql
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 12320/mysqld
啟動MySQL後的初始化部分
進行一些安全設置,以及修改數據庫管理員密碼
# /application/mysql/bin/mysql_sceure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
註意:建議對所有Mariadb運行此腳本的所有部分生產中使用的服務器!請仔細閱讀每一步!
In order to log into MariaDB to secure it, we‘ll need the currentpassword for the root user. If you‘ve just installed MariaDB, andyou haven‘t set the root password yet, the password will be blank,so you should just press enter here.
Enter current password for root (enter for none): 回車即可
OK, successfully used password, moving on...
為了登錄Mariadb以保護它,我們需要MariaDB根用戶的密碼。如果你剛剛安裝了Mariadb,您尚未設置根密碼,密碼將為空,所以你只需在這裏按回車鍵。
Setting the root password ensures that nobody can log into the MariaDBroot user without the proper authorisation.
Set root password? [Y/n]
設置根密碼確保沒有人可以登錄mariadb未經適當授權的根用戶。設置根密碼?[y/n] #這裏選擇y,如果出現報錯說明你沒有用絕對路徑,所有命令都是運行在據對路徑下的,可以通過配置PATH來使命令生效。
By default, a MariaDB installation has an anonymous user, allowing anyoneto log into MariaDB without having to have a user account created forthem. This is intended only for testing, and to make the installationgo a bit smoother. You should remove them before moving into aproduction environment.
Remove anonymous users? [Y/n]
默認情況下,Mariadb安裝有匿名用戶,允許任何人登錄Mariadb而不必為其創建用戶帳戶他們。僅用於測試和安裝稍微平穩一點。在移動到生產環境。
刪除匿名用戶?[y/n] #這裏y清除匿名用戶。
Normally, root should only be allowed to connect from ‘localhost‘. Thisensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n]
通常,只允許根目錄從“localhost”連接。這個確保有人無法猜測網絡中的根密碼。
不允許遠程登錄根目錄?[y/n] # 這裏選擇y
By default, MariaDB comes with a database named ‘test‘ that anyone canaccess. This is also intended only for testing, and should be removedbefore moving into a production environment.
Remove test database and access to it? [Y/n]
默認情況下,Mariadb附帶一個名為“test”的數據庫,任何人都可以訪問該數據庫。這也僅用於測試,在進入生產環境之前應將其移除。
刪除測試數據庫並訪問它?[y/n] #這裏選y
Reloading the privilege tables will ensure that all changes made so farwill take effect immediately.
Reload privilege tables now? [Y/n]、
重新加載特權表將確保所做的所有更改立即生效。
現在重新加載特權表?[y/n] #這裏選擇y
此時初始化及安全初始化就算做完了。
MySQL密碼更改方法:
# ./mysqladmin -uroot -p password #enter
第一次輸入原密碼
第二次輸入新密碼
第三次驗證新密碼
登錄進MySQL後輸入下面命令
修改mysql的用戶密碼,分別使用grant、alter、set修改
l grant all on *.* to ‘用戶名‘@‘登錄主機‘ identified by ‘密碼‘;
l alter user ‘用戶名‘@‘登錄主機‘ identified by ‘密碼(自定義)‘;
l set password for ‘root‘@‘localhost‘ = password(‘new password‘);
#所有修改密碼操作,完成後都需要執行下邊的命令
flush privileges;
忘記密碼修改方法:
l 修改 /etc/my.cnf,在 [mysqld] 小節下添加一行:skip-grant-tables=1
l 這一行配置讓 mysqld 啟動時不對密碼進行驗證
l 重啟 mysqld 服務:systemctl restart mysqld
l 使用 root 用戶登錄到 mysql:mysql -u root
l 切換到mysql數據庫,更新 user 表:
l update user set authentication_string = password(‘root‘), password_expired = ‘N‘, password_last_changed = now() where user = ‘root‘;
l 在之前的版本中,密碼字段的字段名是 password,5.7版本改為了 authentication_string
l 退出 mysql,(exit或者quit)編輯 /etc/my.cnf 文件,刪除 skip-grant-tables=1 的內容
l 重啟 mysqld 服務,再用新密碼登錄即可
CentOS7.x編譯安裝mysql 5.7