首页 > 技术文章 > 记录一下自己安装centos7安装mysql

zk-94872 2022-03-29 10:42 原文

centos7安装mysql(解压版)

每个人 的安装mysql都会遇到一些奇奇怪怪的问题,问题大多部分都是在初始化和启动这两步骤,无非就是权限的问题,

这篇是记录我自己安装时候遇到的部分问题,大家因该有自己的安装笔记;

 

1.检查系统是否有mysql,如果有统统删除

  find / -name mysql

删除命令

  rm -rf /etc/selinux/targeted/active/modules/100/mysql
rm -rf /usr/share/mysql
rm -rf /usr/lib64/mysql

2.创建临时目录,下载并上传mysql-5.7.30-el7-x86_64.tar.gz

到Lehand/tmp

  mkdir -p /Lehand/tmp /Lehand/tools /Lehand/web /Lehand/service /Lehand/api /Lehand/third /Lehand/upload /Lehand/data /Lehand/jdk /Lehand/bak 

 

3.解压,把解压后的文件夹移动到/Lehand/tools

  tar -zxvf mysql-5.7.30-el7-x86_64.tar.gz
mv mysql-5.7.30-el7-x86_64 ../tools
cd ../tools
mv mysql-5.7.30-el7-x86_64 mysql

4.添加用户

  groupadd mysql
useradd -r -g mysql mysql
id mysql

5.创建data目录

  cd /Lehand/data
mkdir mysql
设置权限
chown 777 mysql

6.更改mysql目录权限

  chown -R mysql:mysql /Lehand/tools/mysql

7.配置my.cnf文件

  rm -f /etc/my.cnf
vim /etc/my.cnf
  [client]
port = 33006
default-character-set=utf8
socket = /Lehand/tools/mysql/mysql.sock
[mysqld]
# 一般配置选项
basedir = /Lehand/tools/mysql
datadir = /Lehand/data/mysql
socket=/Lehand/tools/mysql/mysql.sock
max_connections=200
port = 33006
character-set-server=utf8
default_storage_engine = InnoDB
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

8.初始化mysql

cd /Lehand/tools/mysql/bin

./mysqld --initialize-insecure --user=mysql --basedir=/Lehand/tools/mysql --datadir=/Lehand/data/mysql

 

正确的结果

  2022-03-10T08:04:59.243908Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2022-03-10T08:05:01.186604Z 0 [Warning] InnoDB: New log files created, LSN=45790
2022-03-10T08:05:01.788668Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2022-03-10T08:05:02.091122Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: c988acec-a048-11ec-b4ec-525400622edd.
2022-03-10T08:05:02.161651Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2022-03-10T08:05:04.068487Z 0 [Warning] CA certificate ca.pem is self signed.
2022-03-10T08:05:04.644909Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.

 

 

初始化错误:

把data/mysql的文件清楚干净,再次初始化即可(保证文件夹都存在);

  cd /Lehand/data/mysql/
rm -rf *
cd ../
chown 777 mysql

 

9.启动mysql

  ./mysqld_safe --user=mysql &
service mysql status

 

报错: .pid 就需要看日志;

  在日志文件 /var/log/mysql/error.log 中可以看到具体的ERROR
信息:Could not create unix socket lock file /var/run/mysql/mysql.sock.lock。
  这种错误一般都是目录不存在或者权限不足,所以我们直接使用命令 mkdir -p /var/run/mysql/ 创建该目录即可,然后可以设置目录权限 chown -R mysql:mysql /var/run/mysql/ 。

 

10.修改密码,设置外网可以访问

  ./mysql -u root -p

use mysql;
update user set authentication_string=password('root') where user='root';
update user set host='%' where user ='root';
flush privileges;
select host,user from user;
exit;

firewall-cmd --zone=public --add-port=3306/tcp --permanent
firewall-cmd --reload
firewall-cmd --list-ports

11.设置开机启动

  
cp /Lehand/tools/mysql/support-files/mysql.server /etc/init.d/mysql

chkconfig --list

chkconfig mysql on

mysql的状态
service mysql status

关闭 mysql
service mysql stop

启动 mysql
service mysql start

12.添加mysql环境变量

  vim /etc/profile
export PATH=$PATH:/Lehand/tools/mysql/bin
更新资源文件
source /etc/profile
mysql -u root -p

 

重新初始化;

各种报错解决方案:

Can‘t connect to local MySQL server through socket ‘/tmp/mysql.sock‘

推荐阅读