396°

linux安装mysql

本文以 centos为例,使用官方编译好的二进制文件安装

一.下载tar文件

wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz

二.解压移动到相应目录

tar -xvf mysql-5.7.22-linux-glibc2.12-x86_64.tar 
mv mysql-5.7.22-linux-glibc2.12-x86_64 /usr/local/mysql

三.创建相关用户和目录

  • 创建用户
groupadd mysql
useradd -r -g mysql mysql
  • 创建目录 我这里把数据存贮再 /home/data/mysql 目录下 并赋予权限
mkdir -p  /data/mysql             
chown mysql:mysql -R /data/mysql   

四.初始化数据库

  • 初始化之前先修改配置文件
vim /etc/my.cnf
[mysqld]
datadir=/home/data/mysql     	
socket=/var/lib/mysql/mysql.sock
lower_case_table_names=1
character_set_server=utf8
init_connect='SET NAMES utf8'
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]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
  • 执行初始化操作
cd /usr/local/mysql/bin/
./mysqld --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql/ --datadir=/home/data/mysql/ --user=mysql --initialize

成功之后会打印出临时密码,记住这个密码后面登录客户端会用到

[Note] A temporary password is generated for root@localhost: ,(d.F*5=ay*P

五.启动mysql

  • 先将mysql.server放置到/etc/init.d/mysql中
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
  • 启动
[root@iz2z bin]# service mysql start
Starting MySQL.Logging to '/home/data/mysql/iz2ze5p6ra1evukysvb5rpz.err'.
2021-03-04T02:15:32.805481Z mysqld_safe Directory '/var/lib/mysql' for UNIX socket file don't exists.
The server quit without updating PID file (/home/data/mysql[FAILED]6ra1evukysvb5rpz.pid).

启动失败报错无相关目录 创建该目录后再次尝试

[root@iz2z bin]# mkdir -p /var/lib/mysql
[root@iz2z bin]# chmod 777 /var/lib/mysql/
[root@iz2z bin]# service mysql start
Starting MySQL.The server quit without updating PID file (/[FAILED]mysqld/mysqld.pid).

还是启动失败 查看错误日志

[root@iz2z bin]# cat /var/log/mysqld.log 
.....
2021-03-04T02:20:13.982173Z 0 [Note]   - '::' resolves to '::';
2021-03-04T02:20:13.982192Z 0 [Note] Server socket created on IP: '::'.
2021-03-04T02:20:13.983756Z 0 [ERROR] Can't start server: can't check PID filepath: No such file or directory

PID对应得目录不存在,创建目录再次重试

[root@iz2z bin]# mkdir -p /var/run/mysqld
[root@iz2z bin]# service mysql start
Starting MySQL.The server quit without updating PID file (/[FAILED]mysqld/mysqld.pid).

启动再次失败,还是看日志

[root@iz2z bin]# cat /var/log/mysqld.log 
2021-03-04T02:22:06.145196Z 0 [ERROR] /usr/local/mysql/bin/mysqld: Can't create/write to file '/var/run/mysqld/mysqld.pid' (Errcod
2021-03-04T02:22:06.145211Z 0 [ERROR] Can't start server: can't create PID file: Permission denied

目录没有权限,赋予权限后再次重试

[root@iz2z bin]# chmod 777 /var/run/mysqld/
[root@iz2z bin]# service mysql start
Starting MySQL.                 [  OK  ]

启动成功

六.修改密码配置远程连接

登录本地客户端输入刚才记住得密码

[root@iz2z bin]# cd /usr/local/mysql/bin/
[root@iz2z bin]# ./mysql -u root -p
Enter password: 
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

提示找不到 /tmp/mysql.sock而 mysql.sock是在/var/lib/mysql/mysql.sock目录下(在/etc/my.cnf中配置),做一个软连接

[root@iz2z bin]# ln -s /var/lib/mysql/mysql.sock /tmp
[root@iz2z bin]# ./mysql -u root -p
Enter password: 

输入密码后成功登录

  • 修改密码
SET PASSWORD = PASSWORD('root');
ALTER USER 'root'@'localhost' PASSWORD EXPIRE NEVER;
FLUSH PRIVILEGES;
  • 配置远程连接
use mysql
update user set host = '%' where user = 'root'; 
FLUSH PRIVILEGES;

最后可以用navcat远程连接


已有 0 条评论

    我有话说: