安装说明
我们目前测试的环境都是centos7.9.
默认安装 mariadb 5.5.68 版本
yum -y install mariadb-server mariadb
systemctl enable mariadb && systemctl start mariadb
启动完以后就直接可以使用mysql直接登录,无需用户名和密码。
安扎mysql 5.7或者8.0版本
如果是centos8以上,默认安装的就是8.0版本。但是很多情况下我们业务上要求按照5.7。
因此按照下面步骤安装。如果是安装8.0,就不要修改这个文件
下载mysql高版本的镜像仓库。
[root@zbserver1 ~]# yum -y install wget
[root@zbserver1 ~]# wget https://dev.mysql.com/get/mysql80-community-release-el7-10.noarch.rpm
[root@zbserver1 ~]# yum -y localinstall mysql80-community-release-el7-10.noarch.rpm
安装完成以后,我们按照mysql5.7,我们修改 vi /etc/yum.repos.d/mysql.repo
# Enable to use MySQL 5.7
[mysql57-community]
name=MySQL 5.7 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/$basearch
enabled=0 #此处修改为1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql-2022
file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
[mysql80-community]
name=MySQL 8.0 Community Server
baseurl=http://repo.mysql.com/yum/mysql-8.0-community/el/7/$basearch
enabled=0 # 此处修改为0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql-2022
file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
把5.7的enabled=0改成1,把8.0的enable=1改成0
然后执行安装命令
yum -y install mysql-community-server net-tools
systemctl enable mysqld && systemctl start mysqld
获取初始密码,5.5.68的密码初始的时候是不需要的,但是5.7/8.0都是在日志里面的.
** 初始密码一定要修改 ,否则不能创建用户 **
[root@zbserver1 ~]# mysql -uroot -p'RjFCS3fIO,:a'
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.43
mysql> use mysql;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
mysql> alter user root@localhost identified by '1qaz!QAZ';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
这样的话密码就改好了。这个试试可以创建用户和做其他的工作了,代码如下
[root@zbserver1 ~]# mysql -uroot -p'1qaz!QAZ';
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> create user zabbix@localhost identified by '1qaz!QAZ';
Query OK, 0 rows affected (0.01 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
[root@zbserver1 ~]# cat /var/log/mysqld.log |grep pass
2023-09-10T06:11:04.455813Z 1 [Note] A temporary password is generated for root@localhost: RjFCS3fIO,:a
2023-09-10T06:11:27.822245Z 2 [Note] Access denied for user ‘root‘@’localhost’ (using password: NO)
`
如何判断mysql是否启动或者连接不上
如果出现can’t connect to ,无法连接
- 防火墙是否关闭
systemctl status firewalld
以下情况证明防火墙是OK的,但是用户不对。
防火墙如没关闭
systemctl stop firewalld
但是这种情况一般不用
firewall-cmd --add-port=3306/tcp --permanent
- mysql是否真的启动?
1) 看进程
ps -ef |grep (mariadb|mysql)
2)看端口
netstat -antp |grep 3306 |grep LISTEN
3)日志文件查看
最后编辑:严锋 更新时间:2025-05-22 13:03