正常情况下,ubuntu下在线安装mysql都是采用apt方式,几句命令就搞定了,网上教程很多,这里就不多说了,命令如下:
apt-get install mysql-server
apt-get install mysql-client
apt-get install libmysqlclient-dev
我这里要介绍的是非正常情况,由于我们服务器的原因,无法随意连接外网,也就不能使用apt在线安装方式,只能使用离线安装,网上搜了一下,相关教程不是很多,这里介绍一下我的操作。
首先是相关文件下载,下载之后上传到服务器,服务器环境:ubuntu16.04 64位
mysql免安装包:http://mirrors.sohu.com/mysql/MySQL-5.7/mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz
依赖包:http://archive.ubuntu.com/ubuntu/pool/main/liba/libaio/libaio1_0.3.110-2_amd64.deb
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中查看的默认密码
SET PASSWORD = PASSWORD('your new password');
update mysql.user set authentication_string=password('your new password') where user='root';
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'your new password';
FLUSH PRIVILEGES;
大功告成!!!