作者:zuoguohui
一、场景:最近想搞mysql主从复制,需要在两台服务器上安装mysql,之前有一台已经装好了mysql5.7.21,于是在另外一台上也装mysql5.7.21,安装过程中碰到了蛮多坑,因此记录下来。
二、下载安装命令见下:
- wget https://dev.mysql.com//Downloads/MySQL-5.7/mysql-5.7.21-linux-glibc2.12-x86_64.tar.gz
- tar -zxvf mysql-5.7.21-linux-glibc2.12-x86_64.tar.gz
我将解压后文件夹所有文件复制到/usr/local/mysql下
- sudo cp -r mysql-5.7.21-linux-glibc2.12-x86_64/. /usr/local/mysql
在mysql文件夹下创建data文件夹(mysql的数据文件夹)和mysql_3306.err日志文件
- sudo mkdir /usr/local/mysql/data
- sudo vim mysql_3306.err
- sudo chmod -R 777 ./ #设置读写权限
添加用户组
- sudo groupadd mysql
添加用户
- sudo useradd -r -g mysql -s /bin/false mysql
添加mysql配置信息
- sudo /etc/my.cnf
添加信息如下:
[client]
port = 3306
socket = /usr/local/mysql/mysql.sock
[mysql]
no-auto-rehash
[mysqld]
socket = /usr/local/mysql/mysql.sock
character_set_server= utf8
init_connect= 'SET NAMES utf8'
basedir= /usr/local/mysql
datadir= /usr/local/mysql/data
socket = /usr/local/mysql/mysql.sock
pid-file= /usr/local/mysql/mysqld.pid
log-error= /usr/local/mysql/mysql_3306.err
sql_mode= STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
[mysqld_safe]
open-files-limit = 8192
log-error= /usr/local/mysql/mysql_3306.err
保存后对mysql初始化
- sudo /usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql/bin/mysqld --datadir=/usr/local/mysql/data
如果报错,可以去/usr/local/mysql/mysql_3306.err文件查看error
初始化密码可以看到,如:
[Note] A temporary password is generated for root@localhost: *.>9Yy9Uk&Fx
启动在后台
- sudo /usr/local/mysql/bin/mysqld_safe --user=mysql &
修改密码
- sudo /usr/local/mysql/bin/mysql -u root -p
密码见10,如果报错,则修改/etc/my.cnf文件,在mysqld下面添加一行:
skip-grant-tables
12成功后会进入mysql命令模式
- use mysql; 一定要加分号
- update mysql.user set authentication_string=password('你的密码') where user='root' ;
- flush privileges;
- quit 退出
注:启动:/usr/local/mysql/support-files/mysql.server start
自启动:sudo cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql.server
若出现Navicat连接报错Host not allowed,
进入use mysql;
update user set host = '%' where user ='root';
flush privileges;