Linux——MySQL安装、配置

本文提供MySQL在Linux系统的安装指导,包括检查已有安装、卸载旧版本、通过YUM自动安装和手动安装二进制包两种方式,并详细介绍配置过程、启动与管理命令等关键步骤。

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

下载及准备

mysql官网下载地址:https://dev.mysql.com/downloads/mysql/
在这里插入图片描述

检查当前系统是否安装过MySQL

rpm -qa | grep mysql
yum list installed | grep mysql

如果发现系统自带了mysql-libs包,需要卸载之。
通过 如下 命令卸载。

yum -y remove mysql-libs.x86_64
rpm -e mysql-libs-5.1.73-3.el6_5.i686

由于存在依赖卸载失败,解决办法:–nodeps 强力删除模式:

rpm -e --nodeps mysql-libs-5.1.73-3.el6_5.i686

方式一:YUM安装(自动)

下载 mysql57-community-release-el7-8.noarch.rpm 的 YUM 源

wget http://repo.mysql.com/mysql57-community-release-el7-8.noarch.rpm

安装 mysql57-community-release-el7-8.noarch.rpm

rpm -ivh mysql57-community-release-el7-8.noarch.rpm

#安装MySQL

yum install mysql-server

安装完毕后,运行mysql,然后在 /var/log/mysqld.log 文件中会自动生成一个随机的密码,我们需要先取得这个随机密码,以用于登录 MySQL 服务端:

service mysqld start
grep "password" /var/log/mysqld.log

结果如:A temporary password is generated for root@localhost: 6MQ:i=Y108m(

查看MySQL安装时创建的mysql用户和mysql组

[root@localhost opt]# cat /etc/passwd | grep mysql
mysql:x:496:493:MySQL server:/var/lib/mysql:/bin/bash
[root@localhost opt]# 
[root@localhost opt]# cat /etc/group | grep mysql
mysql:x:493:
[root@localhost opt]# 

查看mysql安装版本信息:

[root@localhost opt]# mysqladmin --version
mysqladmin  Ver 8.42 Distrib 5.5.48, for Linux on i686
[root@localhost opt]# 

MySQL安装目录

[root@localhost mysql]# ps -ef |grep mysql
root     30708     1  0 11:34 pts/2    00:00:00 /bin/sh /usr/bin/mysqld_safe --datadir=/var/lib/mysql --pid-file=/var/lib/mysql/localhost.localdomain.pid
mysql    31021 30708  0 11:34 pts/2    00:00:01 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib/mysql/plugin --user=mysql --log-error=/var/lib/mysql/localhost.localdomain.err --pid-file=/var/lib/mysql/localhost.localdomain.pid --socket=/var/lib/mysql/mysql.sock --port=3306
root     31099 30567  0 11:42 pts/2    00:00:00 grep mysql
[root@localhost mysql]# 

这里写图片描述

推荐方式二:二进制安装包(手动)

官网推荐步骤:https://dev.mysql.com/doc/refman/5.7/en/binary-installation.html

shell> groupadd mysql
shell> useradd -r -g mysql -s /bin/false mysql
shell> cd /usr/local
shell> tar zxvf /path/to/mysql-VERSION-OS.tar.gz
shell> ln -s full-path-to-mysql-VERSION-OS mysql
shell> cd mysql
shell> mkdir mysql-files
shell> chown mysql:mysql mysql-files
shell> chmod 750 mysql-files
shell> bin/mysqld --initialize --user=mysql 
shell> bin/mysql_ssl_rsa_setup              
shell> bin/mysqld_safe --user=mysql &
# Next command is optional
shell> cp support-files/mysql.server /etc/init.d/mysql.server

1、将下载的二进制tar.gz包,解压到/usr/local/目录下,并生成软连接,方便使用

[root@test local]# tar zxvf /opt/software/mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz 
[root@test local]# ln -s mysql-5.7.25-linux-glibc2.12-x86_64/ mysql

在这里插入图片描述
2、添加新的用户组和新的用户,用来管理mysql,提高安全性(非必要的,不过mysql官网安装步骤推荐这样做,这个步骤可以省略)

[root@test local]# groupadd mysql
[root@test local]# useradd -r -g mysql -s /bin/false mysql

3、新建mysql的data目录,如果不指定,mysql默认在/usr/local/mysql/下

[root@test mysql_test_data]# pwd
/mydata/mysql_test_data

4、修改mysql目录用户为刚刚新建的mysql组中的mysql用户

[root@test local]# chown -R mysql:mysql ./

5、初始化安装mysql数据库

[root@test mysql]# bin/mysqld --user=mysql --basedir=/usr/local/mysql --datadir=/mydata/mysql_test_data --initialize
2019-02-15T07:40:00.663576Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2019-02-15T07:40:01.734723Z 0 [Warning] InnoDB: New log files created, LSN=45790
2019-02-15T07:40:01.862401Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2019-02-15T07:40:01.930830Z 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: e7202681-30f4-11e9-b95f-00163e080723.
2019-02-15T07:40:01.933261Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2019-02-15T07:40:01.933748Z 1 [Note] A temporary password is generated for root@localhost: ZsMZh&h;e8Nf

6、修改my.conf配置文件,通过mysql官网可以知道,从版本5.7.18开始,mysql免安装版二进制包中就不包含该文件了,即不需要my.conf文件也可以正常运行;my.conf文件中配置的选项会在命令行启动mysql的时候作为参数进行启动,为了后面搭建mysql主从环境方便,下面可以添加了一个简单的my.conf文件作为实例(如果只是单纯的搭建一个mysql实例,可以直接忽略此步骤),使用vim /etc/my.conf命令,如果在该目录上不存在则会新建。

[client]
default-character-set=utf8

[mysql]
prompt=(\\u@\\h) [\\d]>\\_

[mysqld]
port=3306
user=mysql
character_set_server=utf8
basedir=/usr/local/mysql
datadir=/mydata/mysql_test_data
log_error=error.log

7、将mysql添加至开机启动,并使用vim /etc/init.d/mysqld 命令 修改以下代码部分

[root@test mysql]# cp support-files/mysql.server /etc/init.d/mysqld

在这里插入图片描述

8、设置开机启动

[root@test mysql]# chkconfig --add mysqld

9、为了可以在任意目录上都可以使用mysql命令登录mysql,将mysql安装目录配置到环境变量中,在/etc/profile文件的末尾添加以下代码
在这里插入图片描述

10、Centos7下,开放3306端口命令

[root@test mysql]# systemctl start firewalld
[root@test mysql]# firewall-cmd --zone=public --add-port=3306/tcp --permanent
success
[root@test mysql]# firewall-cmd --reload
success
[root@test mysql]# firewall-cmd --zone=public --query-port=3306/tcp
yes

启动mysql

[root@test mysql]# service mysqld start
Starting MySQL.Logging to '/mydata/mysql_test_data/error.log'.
                                                           [  OK  ]
[root@test mysql]# 

使用MySQL

登录MySQL

mysql -u root -p
6MQ:i=Y108m(

修改密码

方式一(首次):

mysql>SET PASSWORD = PASSWORD('new password');
mysql>ALTER USER 'root'@'localhost' PASSWORD EXPIRE NEVER;
mysql>flush privileges;

方式二(忘记密码后修改):

mysql>use mysql;
mysql> update user set password=password("new password") where user='root';
mysql> flush privileges;

设置用户 root 可以在任意 IP 下被访问

mysql>grant all privileges on *.* to root@"%" identified by "new password";

设置用户 root 可以在本地被访问

mysql>grant all privileges on *.* to root@"localhost" identified by "new password";

别忘了刷新权限

mysql>flush privileges;

启动/停止/重启

service mysqld status
service mysqld start
service mysqld stop
service mysqld restart

systemctl status mysqld
systemctl start mysqld
systemctl stop mysqld
systemctl restartmysqld

外网访问防火墙配置

vim  /etc/sysconfig/iptables

附:个人配置

# Firewall configuration written by system-config-firewall
# Manual customization of this file is not recommended.
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -i eth0 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
-A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT
-A FORWARD -p icmp -j ACCEPT
-A FORWARD -i lo -j ACCEPT
-A FORWARD -i eth0 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited

登录mysql,看看字符集


mysql> show variables like '%char%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | latin1                     |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | latin1                     |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)

mysql> 

设置 MySQL 的字符集为 UTF-8

vim /etc/my.cnf

在 [mysqld] 前添加如下代码

[client]
default-character-set=utf8

在 [mysqld] 后添加如下代码

character_set_server=utf8

MySQL表名不区分大小写的设置方法

原来Linux下的MySQL默认是区分表名大小写的,通过如下设置,可以让MySQL不区分表名大小写:
1、用root登录,修改 /etc/my.cnf;
2、在[mysqld]节点下,加入一行: lower_case_table_names=1
3、重启MySQL即可;

其中 lower_case_table_names=1 参数缺省地在 Windows 中这个选项为 1 ,在 Unix 中为 0

mysql配置文件加载顺序

[root@test ~]# mysql --help --verbose | grep my.cnf
                      order of preference, my.cnf, $MYSQL_TCP_PORT,
/etc/my.cnf /etc/mysql/my.cnf /usr/local/mysql/etc/my.cnf ~/.my.cnf 

mysql命令行下自动登录配置

vim /etc/my.cnf

[client]
user=root
password=密码

mysql命令行首格式优化

vim /etc/my.cnf

[mysql]
prompt=(\\u@\\h) [\\d]>\\_

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值