如何在linux系统中使用yum来安装mysql数据库(最全)

很多同学都装过mysql数据库,但是windows系统和linux系统有点不一样,我就自己使用yum源在云服务器上安装mysql做一个分享,希望能帮助到其他小伙伴。

一、安装器前的准备工作

我的云主机使用的系统是CentOS7,但是不巧的是yum源中好像没有mysql,所以查阅了一下资料,可以通过现在mysql的repo源来解决问题。也就是下面两条语句:

1[root@VM_0_9_centos /]#wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm(下载repo源)
2[root@VM_0_9_centos/]#sudo rpm -ivh mysql-community-release-el7-5.noarch.rpm(安装mysql-community-release-el7-5.noarch.rpm包)

上述两步可能需要等一会才能完成(中途会让你输入y/n,输入y即可),当上述两步完成后在我们的 /etc 目录下会多出两个以 .repo 结尾的文件,目录如下:

二、安装

使用命令:

1、[root@VM_0_9_centos /]# sudo yum install mysql-server

等待安装完之后先登录一下:

2、[root@VM_0_9_centos /]# mysql -u root

这时一般会报错:ERROR 2002 (HY000): Can‘t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock‘ (2)

执行赋权命令:

3、[root@localhost ~]# sudo chown -R openscanner:openscanner /var/lib/mysql(把/var/lib/mysql的拥有者改为当前用户)

接下来重启服务:

4、[root@VM_0_9_centos /]# service mysqld restart

一般这个时候可以修改密码了:

5、[root@VM_0_9_centos /]#  mysql -u root

mysql > use mysql;

mysql > update user set password=password("新密码") where user="root";

mysql > exit;

6、重置完密码以后一般是可以链接的,但也有可能会报错:

Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)


这个时候我们就需要去搞一下my.cnf文件了,不知道这个文件在哪里?没关系,使用[root@localhost ~]#whereis my;

[root@VM_0_9_centos /]# whereis my
my: /etc/my.cnf

编辑这个文件:

7、[root@VM_0_9_centos /]# vim /etc/my.cnf, 输入i进入编辑模式在[mysqld]后面任意一行添加“skip-grant-tables”用来跳过密码验证的过程,按Esc键退出编辑模式,使用wq保存。

接下来重启服务:

[root@VM_0_9_centos /]# service mysqld restart

[root@VM_0_9_centos /]# cd usr/lib64/mysql

mysql> use mysql;
mysql> update user set password=password("新密码") where user="root";
mysql> flush privileges;
mysql> quit

8、重新编辑my.cnf文件[root@VM_0_9_centos /]# vim /etc/my.cnf, 输入i进入编辑模式删掉添加“skip-grant-tables”按Esc键退出编辑模式,使用wq保存。

重启服务:

[root@VM_0_9_centos /]#service mysqld restart,到这里在命令行里链接已经没有任何问题了,但是想要其他主机连接还的进行一次修改。

[root@VM_0_9_centos /]#mysql -h localhost -uroot -p
Enter password: ******
Welcome to the MySQL monitor.   Commands end with ; or \g.
Your MySQL connection id is 4 to server version: 4.0.20a-debug
Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the buffer.
mysql> use mysql; (此DB存放MySQL的各种配置信息)
Database changed
mysql> select host,user from user; (查看用户的权限情况)
mysql> select host, user, password from user;

我们可以看到机器全部是用localhost方式来连接的

mysql> Grant all privileges on *.* to 'root'@'%' identified by '123456' with grant option;
(%表示是所有的外部机器,如果指定某一台机,就将%改为相应的机器名;‘root’则是指要使用的用户名,)
mysql> flush privileges;    (运行此句才生效,或者重启MySQL) 

至此大功告成,可以远程连接,可以本地链接。

更多内容请移步微信公众号和微信小程序“每天学java”,更多内容等你来get。

    
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Linux系统安装和配置MySQL数据库,可以按照以下步骤进行操作: 1. 打开终端,并使用root或具有管理员权限的用户登录到Linux系统。 2. 使用包管理器安装MySQL服务器。具体的命令取决于你使用Linux发行版。以下是常见发行版的安装命令: - Ubuntu/Debian: `sudo apt-get install mysql-server` - CentOS/Fedora: `sudo yum install mysql-server` - Arch Linux: `sudo pacman -S mysql` 3. 安装过程,你可能会被要求输入MySQLroot用户密码。请设置一个强密码并记住它。 4. 安装完成后,启动MySQL服务。使用以下命令: - Ubuntu/Debian: `sudo systemctl start mysql` - CentOS/Fedora: `sudo systemctl start mysqld` - Arch Linux: `sudo systemctl start mariadb` 5. 启动MySQL服务后,运行以下命令来确保MySQL服务在系统启动时自动启动: - Ubuntu/Debian: `sudo systemctl enable mysql` - CentOS/Fedora: `sudo systemctl enable mysqld` - Arch Linux: `sudo systemctl enable mariadb` 6. 接下来,运行MySQL安全性脚本来提高安全性并进行一些基本配置。使用以下命令: - `sudo mysql_secure_installation` 7. 脚本将引导你进行一些配置选项,例如删除匿名用户、禁止远程root登录等。根据需要进行选择。 8. 完成安装和配置后,使用以下命令登录到MySQL数据库服务器: - `mysql -u root -p` 9. 输入之前设置的MySQL root用户密码,即可成功登录到MySQL数据库。 通过以上步骤,你就可以在Linux系统成功安装并配置MySQL数据库了。你可以使用MySQL客户端工具进行数据库的管理和操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值