先进入MySQL Community Downloads(https://dev.mysql.com/downloads/),选择使用红色红框标记的菜单 MySQL Community Server
因为我们这里示范安装的是MySQL 5.7.38,所以进入之后点击“Archives”
接着按照以下步骤操作
由于在linux上下载比较快,所以,先使用yum安装wget
代码语言:javascript复制yum install wget
然后使用wget下载mysql文件包(通常都是下载到指定文件夹下,这里是在"/usr/local/"目录)
代码语言:javascript复制wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz
下载完成后解压mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz
代码语言:javascript复制#解压
tar -zxvf mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz
#将mysql-5.7.38-linux-glibc2.12-x86_64重命名为mysql
mv mysql-5.7.38-linux-glibc2.12-x86_64 mysq
创建用户组和用户并修改权限
代码语言:javascript复制groupadd mysql
useradd -r -g mysql mysql
创建数据目录并赋予权限
代码语言:javascript复制#创建数据库目录
mkdir -p /data/mysql
#赋予数据库目录权限
chown mysql:mysql -R /data/mysql
修改配置“/etc/my.cnf”
代码语言:javascript复制vim /etc/my.cnf
代码语言:javascript复制# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html
[mysqld]
user=mysql
port = 33306
sql-mode=STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION
character_set_server=utf8mb4
init_connect='SET NAMES utf8'
explicit_defaults_for_timestamp=true
lower_case_table_names = 1
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
basedir=/usr/local/mysql
datadir=/data/mysql
socket=/tmp/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
[mysqld_safe]
log-error=/data/mysql/mysql.err
pid-file=/data/mysql/mysql.pid
初始化数据库
代码语言:javascript复制cd /usr/local/mysql/bin
./mysqld --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql/ --datadir=/data/mysql/ --user=mysql --initialize
初始化完成之后,输出的日志最后一行会有
代码语言:javascript复制root@localhost: PASSWORD
或者输入cat /data/mysql/mysql.err查看临时密码
先将mysql.server移动到/etc/init.d/mysql中
代码语言:javascript复制cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
然后启动mysql,并查看mysql状态
代码语言:javascript复制#启动mysql
service mysql start
#查看mysql运行状态
service mysql status
ps -ef|grep mysql
然后登录mysql
代码语言:javascript复制#由于当前并没有全局设置mysql命令,所以先切换到/usr/local/mysql/bin/文件夹下
./mysql -uroot -pPASSWORD
然后再执行下面的操作
代码语言:javascript复制SET PASSWORD = PASSWORD('newPassword');
ALTER USER 'root'@'localhost' PASSWORD EXPIRE NEVER;
FLUSH PRIVILEGES;
use mysql;
#使root用户可以在任何IP访问
update user set host = '%' where user = 'root';
#刷新
FLUSH PRIVILEGES;
然后在重新登录;
也有许多小伙伴问什么在其他目录下使用mysql登录命令没有用,mysql命令没有找到,这是因为没有设置软连接,此时,先找到我们安装mysql的目录
然后输入一下命令建立软连接
代码语言:javascript复制ln -s /usr/local/mysql/bin/mysql /usr/bin/
然后在全局使用mysql登录命令,就可以成功使用了,如图
设置mysql开机启动
代码语言:javascript复制#设置mysql开机自启动
chkconfig mysql on
#取消mysql开机自启动
chkconfig mysql off
# 查看系统服务列表,以及每个服务的运行级别
chkconfig --list
#或
systemctl list-unit-file
还有一种专业的设置mysql开机自启动的方法,查看详情请点击
linux如何设置nginx、mysql开机自启动