# 一、准备工作
下载安装包 下载地址
查看系统⾃带的
MARIADB
rpm -qa|grep mariadb
卸载系统⾃带的
MARIADB
(如果有),remove后面加rpm -qa|grep mariadb
查询出来的路径yum -y remove
# 二、解压
将
mysql-8.0.26-linux-glibc2.12-x86_64.tar.xz
安装包上传到/usr/local/
目录下安装包解压,并重命名为mysql
xz -d mysql-8.0.26-linux-glibc2.12-x86_64.tar.xz tar -xvf mysql-8.0.26-linux-glibc2.12-x86_64.tar mv mysql-8.0.26-linux-glibc2.12-x86_64 mysql
创建MYSQL⽤户和⽤户组
groupadd mysql useradd -g mysql mysql
修改MYSQL⽬录的归属⽤户
chown -R mysql:mysql ./
准备MYSQL的配置⽂件,编辑
vim /etc/my.cnf
[mysql] # 设置mysql客户端默认字符集 default-character-set=utf8 socket=/var/lib/mysql/mysql.sock [mysqld] skip-name-resolve #设置3306端⼝ port = 3306 socket=/var/lib/mysql/mysql.sock # 设置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 # binglog配置 server-id = 1 log-bin = mysql-bin log_bin_index = binlog.index binlog_format = ROW # binlog过期清理时间; expire_logs_days= 7 # binlog每个日志文件大小; max_binlog_size = 1024m # binlog缓存大小; binlog_cache_size = 128m # 最大binlog缓存大小。 max_binlog_cache_size = 1024m
并且修改权限
mkdir /var/lib/mysql chmod 777 /var/lib/mysql
# 三、 安装MySQL
进入
cd /usr/local/mysql
目录 进行初始化./bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
执行初始化后,控制台会返回临时密码,
记录临时密码
,后面会用到
例如:
像我的例子在最后一行 A temporary password is generated for root@localhost:kvct:yIy8_qk
我的临时密码就是这个:kvct:yIy8_qk
例子: 2021-09-14T01:30:19.227943Z 0 [System] [MY-013169] [Server] /usr/local/mysql/bin/mysqld (mysqld 8.0.26) initializing of server in progress as process 1808 2021-09-14T01:30:19.250431Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started. 2021-09-14T01:30:20.184005Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended. 2021-09-14T01:30:21.140583Z 0 [Warning] [MY-013746] [Server] A deprecated TLS version TLSv1 is enabled for channel mysql_main 2021-09-14T01:30:21.141148Z 0 [Warning] [MY-013746] [Server] A deprecated TLS version TLSv1.1 is enabled for channel mysql_main 2021-09-14T01:30:21.341903Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: kvct:yIy8_qk
复制启动脚本到资源目录
cp ./support-files/mysql.server /etc/init.d/mysqld
编辑
vim /etc/init.d/mysqld
,修改basedir
和datadir
,为其实际对应的目录basedir=/usr/local/mysql datadir=/usr/local/mysql/data
# 四、设置MYSQL服务开机自启
增加 mysqld 服务控制脚本执⾏权限
chmod +x /etc/init.d/mysqld
将 mysqld 服务加⼊到系统服务
chkconfig --add mysqld
检查 mysqld 服务是否已经⽣效即可
mysqld 0:关 1:关 2:开 3:开 4:开 5:开 6:关
chkconfig --list mysqld
启动
mysql
服务, 返回SUCCESS!
service mysqld start
如何停止
mysql
服务service mysqld stop
# 五、配置环境变量
编辑
vim ~/.bash_profile
,在文件末尾添加下面信息export PATH=$PATH:/usr/local/mysql/bin
执行下面命令,使环境变量生效
source ~/.bash_profile
# 六、登录MySQL,并修改密码
以 root 账户登录 mysql
mysql -u root -p
输入刚才记录的
临时密码
修改
Root
账户密码,并刷新权限alter user user() identified by "root"; flush privileges;
设置远程主机登录
use mysql; update user set user.Host='%' where user.User='root'; flush privileges;
# 七、检查防火墙
做完上面步骤,肯能还是远程连接不上,开放端口或者关闭防火墙
查看防火墙状态
systemctl status firewalld
查看开机是否启动防火墙服务
systemctl is-enabled firewalld
关闭防火墙
systemctl stop firewalld
禁用防火墙(系统启动时不启动防火墙服务)
systemctl disable firewalld systemctl is-enabled firewalld
开放端口
firewall-cmd --zone=public --add-port=3306/tcp --permanent firewall-cmd --reload # 配置立即生效
关闭端口
firewall-cmd --zone=public --remove-port=3306/tcp --permanent firewall-cmd --reload # 配置立即生效
查看防火墙所有开放的端口
firewall-cmd --zone=public --list-ports
查看监听的端口
centos7默认没有 netstat 命令,需要安装 net-tools 工具,yum install -y net-tools
netstat -lnpt
检查端口被哪个进程占用
```
netstat -lnpt |grep 3306
```
- 查看进程的详细信息
```
ps 6832
```
- 查看包含
mysql
的所有进程
```
ps -ef | grep mysql
```
- 中止进程
```
kill -9 6832
```
# 八、忘记 mysql
密码, 找回密码
停止
mysql
服务service mysqld stop
修改
mysql
的配置文件my.cnf
my.cnf
配置文件的位置,一般在/etc/my.cnf
,有些版本在/etc/mysql/my.cnf
在配置文件中添加下面代码,安全模式启动
[mysqld] skip-grant-tables
启动
mysql
服务service mysqld start
进入
mysql
, 这样可以不用输入密码进入mysql
mysql -u root
修改密码, 并刷新权限;
mysql> use mysql; Database changed mysql> update user set authentication_string="123456" where user="root"; Query OK, 0 rows affected (0.00 sec) Rows matched: 1 Changed: 0 Warnings: 0 mysql> flush privileges; Query OK, 0 rows affected (0.00 sec)
将刚才新添加的配置文件
my.cnf
删除这两行[mysqld] skip-grant-tables
重新启动
mysql
服务service mysqld start
MYSQL 8.0
默认user表mysql> select host, user, authentication_string, plugin from user; +-----------+------------------+------------------------------------------------------------------------+-----------------------+ | host | user | authentication_string | plugin | +-----------+------------------+------------------------------------------------------------------------+-----------------------+ | % | root | $A$005$_N6 HbERn<n&lg8k*XmaW25XLFVs8MDOgM1K18egNm1p87ttKMQ9Awcf0/rUA | caching_sha2_password | | localhost | mysql.infoschema | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password | | localhost | mysql.session | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password | | localhost | mysql.sys | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password | +-----------+------------------+------------------------------------------------------------------------+-----------------------+
# 九、Access denied for user 'root'@'localhost' (using password: YES)
安全模式启动
mysql服务
,先将密码设置为空use mysql; update user set authentication_string='' where user='root';
在重新设置密码, 刷新权限
use mysql; alter user user() identified by "root"; flush privileges;