正常情況下,ubuntu下線上安裝mysql都是採用apt方式,幾句命令就搞定了,網上教程很多,這裡就不多說了,命令如下:
apt-get install mysql-server
apt-get install mysql-client
apt-get install libmysqlclient-dev
複製程式碼
我這裡要介紹的是非正常情況,由於我們伺服器的原因,無法隨意連線外網,也就不能使用apt線上安裝方式,只能使用離線安裝,網上搜了一下,相關教程不是很多,這裡介紹一下我的操作。
首先是相關檔案下載,下載之後上傳到伺服器,伺服器環境:ubuntu16.04 64位
mysql免安裝包:mirrors.sohu.com/mysql/MySQL…
依賴包:archive.ubuntu.com/ubuntu/pool…
1. 安裝依賴
dpkg -i libaio1_0.3.110-2_amd64.deb
複製程式碼
2. 建立目錄存放資料庫檔案data和日誌log
mkdir /mysql /mysql/data /mysql/log
複製程式碼
3. 解壓mysql
tar zxf mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz
複製程式碼
4. 移到預設目錄
mv mysql-5.7.17-linux-glibc2.5-x86_64 /usr/local/mysql
複製程式碼
5. 增加使用者和組
groupadd mysql
useradd -g mysql mysql
複製程式碼
6. 修改目錄許可權
chown -R mysql:mysql /usr/local/mysql /mysql
複製程式碼
7. 建立配置檔案
vi /etc/my.cnf
[client]
port = 3306
socket = /tmp/mysql.sock
[mysqld]
server_id=10
port = 3306
user = mysql
socket = /tmp/mysql.sock
basedir = /usr/local/mysql
datadir = /mysql/data
pid-file = /mysql/data/mysql.pid
max_connections = 1000
max_connect_errors = 1000
table_open_cache = 1024
max_allowed_packet = 128M
open_files_limit = 65535
#####====================================[innodb]==============================
innodb_buffer_pool_size = 1024M
innodb_file_per_table = 1
innodb_write_io_threads = 4
innodb_read_io_threads = 4
innodb_purge_threads = 2
innodb_flush_log_at_trx_commit = 1
innodb_log_file_size = 512M
innodb_log_files_in_group = 2
innodb_log_buffer_size = 16M
innodb_max_dirty_pages_pct = 80
innodb_lock_wait_timeout = 30
innodb_data_file_path=ibdata1:1024M:autoextend
#####====================================[log]==============================
log_error = /mysql/log/mysql-error.log
slow_query_log = 1
long_query_time = 1
slow_query_log_file = /mysql/log/mysql-slow.log
sql_mode=ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
複製程式碼
8. 新增執行許可權
chmod +x /usr/local/mysql/bin/*
複製程式碼
9. 配置環境變數
vim /etc/profile
export PATH=$PATH:/usr/local/mysql/bin
source /etc/profile
複製程式碼
10. 配置啟動檔案
cp /usr/local/mysqlsupport-files/mysql.server /etc/init.d/mysql
複製程式碼
11. 系統預設啟動
update-rc.d mysql defaults
複製程式碼
12. 初始化資料庫(5.7.6之後的版本初始化資料庫不再使用mysql_install_db)
mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/home/fabric/mysql/data --innodb_undo_tablespaces=3 --explicit_defaults_for_timestamp
複製程式碼
13. 啟動服務
cd /usr/local/mysql/support-files
./mysql.server start
./mysql.server status(檢視狀態,如果顯示【 * MySQL running】則說明啟動成功)
複製程式碼
14. 可以在/mysql/log/mysql-error.log中檢視root使用者的初始化密碼
A temporary password is generated for root@localhost: *****************
複製程式碼
15. 修改root密碼和遠端訪問許可權
mysql -u root -p
輸入14中檢視的預設密碼
mysql>SET PASSWORD = PASSWORD('your new password');
mysql>update mysql.user set authentication_string=password('your new password') where user='root';
mysql>GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'your new password';
mysql>FLUSH PRIVILEGES;
複製程式碼