下载
- 官网:https://dev.mysql.com/downloads/mysql/
- 最新版本:
https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz
安装
安装目录:/usr/local/mysql
tar包目录:/usr/local/setup/
步骤
- 解压并改名
tar -zxvf /usr/local/setup/mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz
mv mysql-5.7.23-linux-glibc2.12-x86_64/ mysql
- 创建用户组和用户,并赋予权限
groupadd mysql
useradd -r -g mysql mysql
chown -R mysql mysql/
chgrp -R mysql mysql/
- 创建配置文件,并赋予权限
vi /etc/my.cnf
chmod 644 my.cnf
chown mysql:mysql my.cnf
/etc/my.cnf 是mysql.server指定的配置文件,须赋予指定权限644,这点很重要。写入被指文件内容如下:
port = 3306
socket = /tmp/mysql.sock
[mysqld]
character_set_server=utf8
init_connect='SET NAMES utf8'
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
socket=/tmp/mysql.sock
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
lower_case_table_names = 1
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
max_connections=5000
default-time_zone = '+8:00'
- 创建步骤3指定的log文件和pid文件,并赋予权限
yum install libaio
cd /var/log
touch mysqld.log
chmod 777 mysqld.log
chown mysql:mysql mysqld.log
cd /var/run
mkdir mysqld
chmod 777 mysqld
cd mysqld
touch mysqld.pid
chmod 777 mysqld.pid
chown mysql:mysql mysqld.pid
关于yum install libaio,别人都这么做,说是不安装会编译错误,没时间去验证这个
- 初始化mysql
/usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --lc_messages_dir=/usr/local/mysql/share --lc_messages=en_US
注意执行完之后去/etc/log/mysqld.log中找数据库root用户的初始密码。
#A temporary password is generated for root@localhost: 6H#Sk>kIG5QG
@localhost 后面的就是初始密码
- 启动mysql,建立连接,修改密码
/usr/local/mysql/support-files/mysql.server start
/usr/local/mysql/bin/mysql -u root -p
#然后输入初始密码即可进入mysql操作界面
mysql> set password = password('新密码');
- 创建mysql系统服务,并设置开机自启
cd /usr/local/mysql/support-files
#先停止之前启动的mysql
./mysql.server stop
cp mysql.server /etc/init.d/mysqld
chkconfig --add mysqld
chkconfig mysqld on
service mysqld start
- 添加系统路径,使mysql成为系统命令
vim /etc/profile
export PATH=/usr/local/mysql/bin:$PATH
source /etc/profile
- 创建远程操作用户,远程客户端连接测试
mysql -u root -p
grant all privileges on *.* to '新用户名'@'%' identified by '新密码';
flush privileges;
这里的*.*,前一个是database或者schemas,后一个是表名
%表示所有ip的客户端均可连接这里的mysql服务,可以改成ip地址,也可以使用%和_通配的ip地址
远程测试可使用图形操作界面如mysql workbench、navicat等,可以通过命令行操作
mysql -h [HOST] -P [PORT] -u root -p