欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Linux系统安装mysql数据库

程序员文章站 2022-03-03 14:41:54
...

前言

第一次安装linux系统的mysql数据库,经过各种error,终于安装完成,记录下来以备查看。

安装流程

下载安装软件

官网下载mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz
https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz

解压文件,将解压的文件拷贝到/usr/local/mysql

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

创建配置文件my.cnf

vi /etc/my.cnf

粘帖以下内容:

[mysql]
#设置mysql客户端默认字符集
default-character-set=utf8 
[mysqld]
skip-name-resolve
#设置3306端口
port = 3306 
# 设置mysql的安装目录
basedir=/usr/local/mysql/
# 设置mysql数据库的数据的存放目录
datadir=/usr/local/mysql/data
# 允许最大连接数
max_connections=200
# 服务端使用的字符集默认为8比特编码的latin1字符集
character-set-server=utf8
# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB 
lower_case_table_names=1
max_allowed_packet=16M
log-error = /usr/local/mysql/log/error.log
pid-file = /usr/local/mysql/pid/mysql.pid
user = root
tmpdir = /tmp
socket = /tmp/mysql.sock
explicit_defaults_for_timestamp=true

为mysql目录授权

groupadd mysql
useradd -r -g mysql mysql
cd /usr/local/mysql
chown -R mysql:mysql ./

安装和初始化

初始化mysql数据库(initialize常见异常解决见下方)
命令目录/usr/local/mysql

bin/mysqld --initialize
cp ./support-files/mysql.server /etc/init.d/mysqld
chown 777 /etc/my.cnf
chmod +x /etc/init.d/mysqld

启动mysql

/etc/init.d/mysqld start

结果如下

Starting MySQL... SUCCESS! 

查看数据库状态

netstat -lntp 

结果如下

Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name   
tcp        0      0 :::3306                     :::*                        LISTEN      17965/mysqld        

登录mysql

mysql -u root -p

忘记root密码详见常见异常中修改root密码

常见异常

bin/mysqld --initialize问题总汇

问题
bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory
解决
yum install -y libaio
问题
bin/mysqld: error while loading shared libraries: libnuma.so.1: cannot open shared object file: No such file or directory
解决
yum -y install numactl.x86_64
--initialize specified but the data directory has files in it. Aborting
没有找到data路径,从/etc/my.cnf配置文件中找到data文件路径
datadir=/usr/local/mysql/data
根据配置信息建好文件路径
## 如何创建一个注脚
Could not open file '/usr/local/mysql/log/error.log' for error logging: No such file or directory
没有找到log路径,从/etc/my.cnf配置文件中找到log文件路径
log-error = /usr/local/mysql/log/error.log
根据配置信息建好文件路径

mysql命令不可用

-bash: mysql: command not found
打开编辑系统环境变量文件 /etc/profile
把 MySQL 的 bin 路径加入到环境变量中:
export PATH=$PATH://usr/local/mysql/bin
执行profile
. /etc/profile

新装数据库修改root密码

修改root密码
停掉mysqld服务

[[email protected] mysql]# service mysqld stop
Shutting down MySQL.. SUCCESS! 

修改my.cnf配置文件

[[email protected] mysql]# vi /etc/my.cnf

在[mysqld]的段中加上一句:skip-grant-tables 保存并且退出vi。

启动mysqld服务

[[email protected] mysql]# service mysqld start
Starting MySQL. SUCCESS! 
[[email protected] mysql]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.22 MySQL Community Server (GPL)
 
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
 
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
 
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
 
mysql> 

登录并修改MySQL的root密码

mysql> USE mysql ; 
mysql> update user set password=password("*******") where user="*******";  #修改密码报错
ERROR 1054 (42S22): Unknown column 'password' in 'field list'
mysql> update mysql.user set authentication_string=password('*******') where user='*******';  #修改密码成功
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 1 
mysql> flush privileges ; 
mysql> exit

将my.cnf配置改回来

vi /etc/my.cnf

将刚才在[mysqld]的段中加上的skip-grant-tables删除
保存并且退出vi。
重新启动mysqld

service mysqld restart