阿里云ECS服务器linux安装mysql 5.7,并远程连接

  • Post author:
  • Post category:linux


1.

MySQL :: MySQL Downloads

2.执行解压命令,解压后的文件放入/usr/local/mysql

#查询一下有没有安装mariadb,有的话删除
rpm -qa|grep mariadb
mariadb-libs-5.5.52-1.el7.x86_64


忽略依赖关系,删除此包
rpm -e --nodeps mariadb-libs-5.5.52-1.el7.x86_64

tar -zxvf mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz

# 将解压后的文件夹移动到/usr/local下并更名为mysql
$ mv mysql-5.7.25-linux-glibc2.12-x86_64 /usr/local/mysql

# 添加用户组mysql
groupadd mysql
useradd -r -g mysql mysql

# 新建mysql数据存放目录
$ mkdir -p /usr/local/mysql/data

# 授权此目录的所有者为mysql用户
$ chown -R mysql:mysql /usr/local/mysql/data

# 权限设置为只有mysql用户可读写,mysql用户组可读,其他无权限。
$ chmod 750 /usr/local/mysql/data

3.初始化及安装libaio依赖包

初始化
cd /usr/local/mysql/bin
./mysqld --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --initialize


如果出现如下错误
./mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory
安装依赖包
yum install libaio

会出现一行,后面那一串

B;a%qYrVS6i&

是临时密码,一会用来登录

[Note] A temporary password is generated for root@localhost: B;a%qYrVS6i&

4.修改mysql配置文件,如果有修改安装目录或者是数据库目录的,要修改成相应目录,按上文的目录无需修改

vim /usr/local/mysql/support-files/mysql.server

修改相应的路径

cp /usr/local/mysql/support-files/mysql.server  /etc/init.d/mysqld
chmod 755 /etc/init.d/mysqld

5.修改my.cnf

vim /etc/my.cnf

修改为以下内容

[client]
# 本地客户端指定sock路径,必须跟下面服务端配置的socket一致,不然本地找不到sock文件,无法连接直接使用mysql命令登录
socket = /usr/local/mysql/data/mysql.sock


[mysql]
# 设置mysql客户端默认字符集
default-character-set=utf8mb4

[mysqld]
# 指定用户
user = mysql

# 服务端使用的字符集默认为8比特编码的latin1字符集
character-set-server=utf8mb4

# mysql主目录
basedir = /usr/local/mysql

# 数据目录
datadir = /usr/local/mysql/data

# 端口
port = 3306

# pid文件路径
pid-file = /usr/local/mysql/data/mysql.pid

# sock文件路径
socket = /usr/local/mysql/data/mysql.sock

# 错误日志路径
log-error = /usr/local/mysql/data/error.log

# 禁止反向解析hostname和dns,只使用ip进行连接
skip-name-resolve = 1

# 默认的sql_mode配置
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION

6.启动mysql

/etc/init.d/mysqld start

或者

# 添加此服务到系统服务
$ chkconfig --add mysqld

# 添加服务后,就可以使用systemctl命令进行管理了
# 启动服务
$ systemctl start mysqld

# 设置服务为开机自启动
$ systemctl enable mysqld

# 查看服务运行状态
$ systemctl status mysqld

我这里出现了两个错误

该错误在配置文件改正之后已经没有出现,放在此处仅供备忘

第一个错误
Starting MySQL.2019-04-12T02:44:42.441538Z mysqld_safe error: log-error set to '/var/log/mariadb/mariadb.log', however file don't exists. Create writable for user 'mysql'.
The server quit without updating PID file (/data/mysql/izwz[FAILED]34io8uqj72z.pid).

解决如下
mkdir -p /var/log/mariadb/
cd /var/log/mariadb/
touch mariadb.log
chmod -R 775 mariadb.log
chown -R mysql:mysql mariadb.log


第二个错误
Starting MySQL.2019-04-12T02:49:18.214274Z mysqld_safe Directory '/var/lib/mysql' for UNIX socket file don't exists.
The server quit without updating PID file (/data/mysql/izwz[FAILED]34io8uqj72z.pid).



这个错误解决如下
mkdir   /var/lib/mysql
chmod 777  /var/lib/mysql

7.成功启动之后

/usr/local/mysql/bin/mysql -uroot -p

输入临时密码登录,修改密码并开放远程连接

#更改本机连接密码
alter user 'root'@'localhost' identified by 'root';
#开放远程连接登录账户名及密码
GRANT ALL PRIVILEGES ON *.* TO 'your username'@'%' IDENTIFIED BY 'your password' WITH GRANT OPTION;
#刷新
flush privileges;

8.阿里云ECS服务器要开放3306端口



版权声明:本文为weixin_39112840原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。