怎样安装mysql的数据库

本文档详细介绍了在CentOS 6.8或7上安装MySQL 5.7的步骤,包括下载软件包、解压、用户设定与初始化、启动数据库时遇到的问题及解决方法,确保数据库能够成功启动并运行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

目录

0) 安装的环境介绍

1)首先将mysql的包下载到/home/appdeploy/soft目录下

2)然后将下载的包解压到要安装的包

3)设定用户和初始化,将用户设置成mysql

note: 在执行上面代码最后一行初始化操作时可能会有下面的报错

3-1)如果有上面的报错请执行下面的语句

3-2)然后执行下面的初始化操作就可以了

3-3)结果如下,没有报错表示成功

4) 怎样启动数据库

4-1)下面是启动数据库的命令,发现有报错

4-2)修改配置文件,解决上述报错

4-3)启动测试


0) 安装的环境介绍

安装的环境是:centos 6.8 或者centos 7

安装的mysq版本是:mysql-5.7.27

实现的需求:将数据库安装到/app/db/mysql下,但是需要将软件下载到/home/appdeploy/soft

镜像地址:http://mirrors.sohu.com/mysql/

1)首先将mysql的包下载到/home/appdeploy/soft目录下

wget -P /home/appdeploy/soft http://mirrors.sohu.com/mysql/MySQL-5.7/mysql-5.7.27-linux-glibc2.12-x86_64.tar.gz

2)然后将下载的包解压到要安装的包

cd /home/appdeploy/soft
tar -zxvf mysql-5.7.27-linux-glibc2.12-x86_64.tar.gz -C /app/db
mv mysql-5.7.27-linux-glibc2.12-x86_64 mysql

3)设定用户和初始化,将用户设置成mysql

groupadd mysql
useradd -r -g mysql -s /bin/false mysql
/app/db/mysql/bin/mysqld --user=mysql --basedir=/app/db/mysql --datadir=/app/db/mysql/data/  --initialize

note: 在执行上面代码最后一行初始化操作时可能会有下面的报错

[root@hsl mysql]# /app/db/mysql/bin/mysqld --user=mysql --basedir=/app/db/mysql --datadir=/app/db/mysql/data/ --initialize
/app/db/mysql/bin/mysqld: error while loading shared libraries: libnuma.so.1: cannot open shared object file: No such file or directory

3-1)如果有上面的报错请执行下面的语句

[root@hsl mysql]# yum -y install numactl

3-2)然后执行下面的初始化操作就可以了

[root@hsl mysql]# /app/db/mysql/bin/mysqld --user=mysql --basedir=/app/db/mysql --datadir=/app/db/mysql/data/ --initialize

3-3)结果如下,没有报错表示成功

[appdeploy@hsl mysql]$ /app/db/mysql/bin/mysqld --user=mysql --basedir=/app/db/mysql --datadir=/app/db/mysql/data/  --initialize
2019-10-27T05:23:14.591365Z 0 [Warning] Changed limits: max_open_files: 1024 (requested 5000)
2019-10-27T05:23:14.591579Z 0 [Warning] Changed limits: table_open_cache: 431 (requested 2000)
2019-10-27T05:23:14.591784Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2019-10-27T05:23:14.656710Z 0 [Warning] One can only use the --user switch if running as root

2019-10-27T05:23:17.035704Z 0 [Warning] InnoDB: New log files created, LSN=45790
2019-10-27T05:23:17.186510Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2019-10-27T05:23:18.410614Z 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: e25ecd79-f879-11e9-ab06-000c29f59b7f.
2019-10-27T05:23:18.411377Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2019-10-27T05:23:18.587385Z 1 [Note] A temporary password is generated for root@localhost: cP=dlTxk0CZu

4) 怎样启动数据库

4-1)下面是启动数据库的命令,发现有报错

报错就说明启动的路径有问题,需要指定配置文件才行

./mysqld restart
mysqld: Can't change dir to '/var/lib/mysql/' (Errcode: 2 - No such file or directory)
2019-10-27T05:38:12.744538Z 0 [Warning] Changed limits: max_open_files: 1024 (requested 5000)
2019-10-27T05:38:12.744662Z 0 [Warning] Changed limits: table_open_cache: 431 (requested 2000)
2019-10-27T05:38:12.967244Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2019-10-27T05:38:12.968733Z 0 [Note] --secure-file-priv is set to NULL. Operations related to importing and exporting data are disabled
2019-10-27T05:38:12.968767Z 0 [Note] ./mysqld (mysqld 5.7.27) starting as process 31767 ...
2019-10-27T05:38:12.968853Z 0 [ERROR] Can't find error-message file '/usr/local/mysql/share/errmsg.sys'. Check error-message file location and 'lc-messages-dir' configuration directive.
2019-10-27T05:38:12.969529Z 0 [Warning] Can't create test file /var/lib/mysql/hsl.lower-test
2019-10-27T05:38:12.969538Z 0 [Warning] Can't create test file /var/lib/mysql/hsl.lower-test
2019-10-27T05:38:12.969706Z 0 [Warning] One can only use the --user switch if running as root

2019-10-27T05:38:12.969739Z 0 [ERROR] failed to set datadir to /var/lib/mysql/
2019-10-27T05:38:12.969751Z 0 [ERROR] Aborting

2019-10-27T05:38:12.969760Z 0 [Note] Binlog end
2019-10-27T05:38:12.969877Z 0 [Note] 

4-2)修改配置文件,解决上述报错

vim /etc/my.cnf

然后修改后的配置文件如下:

[mysqld]
# 下面是指定mysql的启动目
basedir=/app/db/mysql
# 显示是指定mysql的数据库存放木库
datadir=/app/db/mysql/data
socket=/app/db/mysql/log/mysql.sock
user=mysql
port=3306
pid-file=/app/db/mysql/log/mysql.pid
bind-address=0.0.0.0
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
log_error = /app/db/mysql/log/mysql-error.log

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

4-3)启动测试

执行下面的命令就可以了

/app/db/mysql/bin/mysqld &

查看进程是否存在

[appdeploy@hsl log]$ ps -ef | grep mysqld
500      32568     1  0 14:45 ?        00:00:00 ./mysqld
500      32693 32649  0 14:58 pts/3    00:00:00 grep mysqld

登陆数据库后执行show databases发现如下报错

mysql> show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

解决方法是:

alter user 'root'@'localhost' identified by 'sf123456qwe';

测试脚本,下面说明就已经可以了

[appdeploy@hsl bin]$ /app/db/mysql/bin/mysql -h 127.0.0.1 -P 3306 -uroot -psf123456qwe -e "show databases"
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值