Linux 搭建mysql环境

Posted Adorable_Rocy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux 搭建mysql环境相关的知识,希望对你有一定的参考价值。

首先准备安装包

链接:https://pan.baidu.com/s/1n27P4uveIAdVEh0tlrTduA 
提取码:8509
  1. 解压文件到 /usr/local/mysql

  2. 在mysql目录中创建data目录

  3. 查看组名和添加组名

cat /etc/group | grep mysql

cat /etc/passwd | grep mysql


# 创建组名
groupadd mysql
useradd -r -g mysql mysql

# 修改mysql所有组
chown -R mysql.mysql /usr/local/mysql/mysql
# 添加权限
chmod u+x *
  1. 配置/etc/my.cnf配置文件
[mysqld]
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

basedir = /usr/local/mysql/mysql
datadir = /usr/local/mysql/mysql/data
port = 3306
socket = /tmp/mysql.sock
character-set-server=utf8

log-error = /usr/local/mysql/mysql/data/mysqld.log
pid-file = /usr/local/mysql/mysql/data/mysqld.pid
  1. 初始化
[root@localhost mysql]# ./bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql/mysql/ --datadir=/usr/local/mysql/mysql/data/

补充:可能报错./bin/mysqld: error while loading shared libraries: libnuma.so.1: cannot ope

执行命令:[root@VM-16-7-centos mysql]# yum -y install numactl

[root@VM-16-7-centos mysql]# yum -y install numactl
Loaded plugins: fastestmirror, langpacks
Determining fastest mirrors
epel                                                                                     | 4.7 kB  00:00:00
extras                                                                                   | 2.9 kB  00:00:00
os                                                                                       | 3.6 kB  00:00:00
updates                                                                                  | 2.9 kB  00:00:00
(1/2): epel/7/x86_64/updateinfo                                                          | 1.0 MB  00:00:00
(2/2): epel/7/x86_64/primary_db                                                          | 7.0 MB  00:00:11
Resolving Dependencies
--> Running transaction check
---> Package numactl.x86_64 0:2.0.12-5.el7 will be installed
--> Processing Dependency: libnuma.so.1(libnuma_1.4)(64bit) for package: numactl-2.0.12-5.el7.x86_64
--> Processing Dependency: libnuma.so.1(libnuma_1.3)(64bit) for package: numactl-2.0.12-5.el7.x86_64
--> Processing Dependency: libnuma.so.1(libnuma_1.2)(64bit) for package: numactl-2.0.12-5.el7.x86_64
--> Processing Dependency: libnuma.so.1(libnuma_1.1)(64bit) for package: numactl-2.0.12-5.el7.x86_64
--> Processing Dependency: libnuma.so.1()(64bit) for package: numactl-2.0.12-5.el7.x86_64
--> Running transaction check
---> Package numactl-libs.x86_64 0:2.0.12-5.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================================================
 Package                       Arch                    Version                        Repository           Size
================================================================================================================
Installing:
 numactl                       x86_64                  2.0.12-5.el7                   os                   66 k
Installing for dependencies:
 numactl-libs                  x86_64                  2.0.12-5.el7                   os                   30 k

Transaction Summary
================================================================================================================
Install  1 Package (+1 Dependent package)

Total download size: 96 k
Installed size: 191 k
Downloading packages:
(1/2): numactl-libs-2.0.12-5.el7.x86_64.rpm                                              |  30 kB  00:00:00
(2/2): numactl-2.0.12-5.el7.x86_64.rpm                                                   |  66 kB  00:00:00
----------------------------------------------------------------------------------------------------------------
Total                                                                           529 kB/s |  96 kB  00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Warning: RPMDB altered outside of yum.
  Installing : numactl-libs-2.0.12-5.el7.x86_64                                                             1/2
  Installing : numactl-2.0.12-5.el7.x86_64                                                                  2/2
  Verifying  : numactl-libs-2.0.12-5.el7.x86_64                                                             1/2
  Verifying  : numactl-2.0.12-5.el7.x86_64                                                                  2/2

Installed:
  numactl.x86_64 0:2.0.12-5.el7

Dependency Installed:
  numactl-libs.x86_64 0:2.0.12-5.el7

Complete!
[root@VM-16-7-centos mysql]# ./bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql/mysql/ --datadir=/usr/local/mysql/mysql/data/

  1. 查看初始密码
cat /usr/local/mysql/mysql/data/mysqld.log

2022-04-13T10:54:04.370436Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2022-04-13T10:54:04.370670Z 0 [Warning] 'NO_ZERO_DATE', 'NO_ZERO_IN_DATE' and 'ERROR_FOR_DIVISION_BY_ZERO' sql modes should be used with strict mode. They will be merged with strict mode in a future release.
 ...
2022-04-13T10:54:08.811937Z 1 [Note] A temporary

password is generated for root@localhost: W9h,mzx>Eepw

  1. 复制脚本到目录中
[root@localhost mysql]# cp support-files/mysql.server /etc/init.d/mysql
  1. 启动服务并且登录
[root@localhost mysql]# service mysql start
[root@localhost mysql]# ./bin/mysql -u root -p

#修改密码
set password=password('root');
#设置可以远程访问
grant all privileges on *.* to root@'%' identified by 'root';
#刷新权限
flush privileges;
#重启服务器
service mysql restart

随后正常登录完毕

补充:远程访问出现错误代码1045,是因为拒绝访问了

# 选择数据库
USE mysql;
# 增加权限
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'password' WITH GRANT OPTION;
# 刷新
FLUSH PRIVILEGES;

后续事件

  • 开放端口
 firewall-cmd --zone=public --add-port=3306/tcp --permanent
  • 引用数据库脚本
# -D 表示use test 
# 如果sql脚本中写了use xx数据库 那么就不要加-D
mysql –u root –p 123456 -D test < /xx/xx/xxx.sql

以上是关于Linux 搭建mysql环境的主要内容,如果未能解决你的问题,请参考以下文章

如何解决mysql安装时不知道初始密码的情况

PHP - CentOS下开发运行环境搭建(Apache+PHP+MySQL+FTP)

(--1--)在Linux环境下忘记mysql密码处理方法

Linux Mysql root密码忘记,初始化

环境搭建及wamp空密码修改

linux系统下使用xampp 丢失mysql root密码xampp的初始密码为空