准备
安装包(必须):
Linux - Generic (glibc 2.12) (x86, 64-bit), Compressed TAR Archive
安装libaio、numactl(非必须,如果在初始化mysql的时候报错就需要安装):
yum的两种安装方式:
yum search libaio # search for info
yum search numactl
yum install libaio # install library
yum install numactl
AI 代码解读
APT-based安装方式:
apt-cache search libaio # search for info
apt-cache search numactl
apt-get install libaio # install library
apt-get install numactl
AI 代码解读
在linux上添加mysql用户组以及用户
groupadd mysql
useradd -g mysql -s /sbin/nologin mysql
AI 代码解读
*解压mysql到指定目录
这里笔者放到了/usr/local/mysql/
AI 代码解读
sudo tar zxvf mysql-5.7.21-linux-glibc2.12-x86_64.tar.gz
AI 代码解读
重命名
sudo mv mysql-5.7.21-linux-glibc2.12-x86_64.tar.gz mysql-5.7.21-3306
AI 代码解读
简单说一下笔者数据库目录规划
文件类型 实例3306
数据datadir:/usr/local/mysql/mysql-5.7.21-3306/data
参数文件my.cnf:/usr/local/mysql/mysql-5.7.21-3306/etc/my.cnf
错误日志log-error:/usr/local/mysql/mysql-5.7.21-3306/log/mysql_error.log
二进制日志log-bin:/usr/local/mysql/mysql-5.7.21-3306/binlogs/mysql-bin
慢查询日志slow_query_log_file:/usr/local/mysql/mysql-5.7.21-3306/log/mysql_slow_query.log
套接字socket文件:/usr/local/mysql/mysql-5.7.21-3306/run/mysql.sock
pid文件:/usr/local/mysql/mysql-5.7.21-3306/run/mysql.pid
AI 代码解读
所以,我会在mysql-5.7.21-3306目录下创建run、etc、log、binlogs目录,并且赋予权限;
cd mysql-5.7.21-3306
sudo mkdir {run,etc,log,binlogs}
sudo chown -R mysql:mysql {run,etc,log,binlogs}
AI 代码解读
然后vim在目录下的etc中编辑my.cnf,my.cnf的配置不只是这些,我只配置了我需要的,具体的大家可以自己去好好了解下;
[client] port=3306 socket=/usr/local/mysql/mysql-5.7.21-3306/run/mysql.sock [mysqld] port = 3306 socket = /usr/local/mysql/mysql-5.7.21-3306/run/mysql.sock pid_file = /usr/local/mysql/mysql-5.7.21-3306/run/mysql.pid datadir = /usr/local/mysql/mysql-5.7.21-3306/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 = 128M innodb_log_file_size = 128M innodb_file_per_table = 1 innodb_flush_log_at_trx_commit = 0 key_buffer_size = 64M log-error = /usr/local/mysql/mysql-5.7.21-3306/log/mysql_error.log log-bin = /usr/local/mysql/mysql-5.7.21-3306/binlogs/mysql-bin slow_query_log = 1 slow_query_log_file = /usr/local/mysql/mysql-5.7.21-3306/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
AI 代码解读
然后我们通过编辑好的my.cnf初始化mysql
sudo /usr/local/mysql/mysql-5.7.21-3306/bin/mysqld --defaults-file=/usr/local/mysql/mysql-5.7.21-3306/etc/my.cnf --basedir=/usr/local/mysql/mysql-5.7.21-3306 --datadir=/usr/local/mysql/mysql-5.7.21-3306/data --user=mysql --initialize
AI 代码解读
然后通过my.cnf启动服务
/usr/local/mysql/mysql-5.7.21-3306/bin/mysqld_safe --defaults-file=/usr/local/mysql/mysql-5.7.21-3306/etc/my.cnf &
AI 代码解读
再查看下端口:
netstat -anp | grep 3306
AI 代码解读
ok,mysql服务已经跑起来了,如果是多实例的话,其实也差不多,大家把端口号改一下,其他步骤重新来一边,ok了; 那么,服务起来了,我们没设置密码,怎么登陆呢?
AI 代码解读
开始登陆mysql
原来为了加强安全性,MySQL5.7为root用户随机生成了一个密码,在error_log中。笔者这里的事例的话,文件为
AI 代码解读
/usr/local/mysql/mysql-5.7.21-3306/log/mysql_error.log
AI 代码解读
使用vim打开查看:
sudo vim /usr/local/mysql/mysql-5.7.21-3306/log/mysql_error.log
AI 代码解读
显示vim行号,第八行
2018-03-26T09:47:13.104037Z 1 [Note] A temporary password is generated for root@localhost: Y3Y)#dEYc(ZN
AI 代码解读
这里的
Y3Y)#dEYc(ZN
AI 代码解读
就是我们的启动密码
使用sock方式登录mysql
/usr/local/mysql/mysql-5.7.21-3306/bin/mysql -uroot -S /usr/local/mysql/mysql-5.7.21-3306/run/mysql.sock -p
AI 代码解读
okok,登录成功!
好了,如果我们想修改登录密码的话,传送门:
[mysql]MySQL修改账号密码的3种方法