欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Linux系统-MySQL数据库(上)

程序员文章站 2022-05-27 12:39:41
...

一、查看linux中有没有安装自带的mysql数据库, 如果安装了自带的mysql,则需要卸载

[[email protected] ~]# rpm -qa | grep mysql                # 查看系统中有没有安装过mysql
mysql-libs-5.1.71-1.el6.x86_64

[[email protected] ~]# yum -y remove mysql-libs            # 卸载 安装过的mysql-lib
Loaded plugins: product-id, refresh-packagekit, security, subscription-manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
Setting up Remove Process
Resolving Dependencies
........此处卸载内容省略............
Removed:
  mysql-libs.x86_64 0:5.1.71-1.el6                                                                                       

Dependency Removed:
  cronie.x86_64 0:1.4.4-12.el6           cronie-anacron.x86_64 0:1.4.4-12.el6       crontabs.noarch 0:1.10-33.el6      
  postfix.x86_64 2:2.6.6-2.2.el6_1       sysstat.x86_64 0:9.0.4-22.el6             

Complete!
[[email protected] ~]# rpm -qa | grep mysql                # 再次查询看看有没有卸载干净
[[email protected] ~]# 

二、下载mysql软件包(这里用的是mysql 5.5 版本)

下载地址:

点击下载mysql5.5软件包


三、将软件包上传到 linux 系统的 /opt 目录下

[[email protected] opt]# ls
MySQL-5.5.48-1.linux2.6.x86_64.rpm-bundle.tar  rh
[[email protected] opt]#

四、解压 MySQL-5.5.48-1.linux2.6.x86_64.rpm-bundle.tar 文件

[[email protected] opt]# tar -xvf MySQL-5.5.48-1.linux2.6.x86_64.rpm-bundle.tar  # 解压 压缩文件
MySQL-client-5.5.48-1.linux2.6.x86_64.rpm
MySQL-shared-compat-5.5.48-1.linux2.6.x86_64.rpm
MySQL-test-5.5.48-1.linux2.6.x86_64.rpm
MySQL-server-5.5.48-1.linux2.6.x86_64.rpm
MySQL-embedded-5.5.48-1.linux2.6.x86_64.rpm
MySQL-devel-5.5.48-1.linux2.6.x86_64.rpm
MySQL-shared-5.5.48-1.linux2.6.x86_64.rpm

[[email protected] opt]# ls                                                      # 查看解压后的情况
MySQL-5.5.48-1.linux2.6.x86_64.rpm-bundle.tar  MySQL-shared-5.5.48-1.linux2.6.x86_64.rpm
MySQL-client-5.5.48-1.linux2.6.x86_64.rpm      MySQL-shared-compat-5.5.48-1.linux2.6.x86_64.rpm
MySQL-devel-5.5.48-1.linux2.6.x86_64.rpm       MySQL-test-5.5.48-1.linux2.6.x86_64.rpm
MySQL-embedded-5.5.48-1.linux2.6.x86_64.rpm    rh
MySQL-server-5.5.48-1.linux2.6.x86_64.rpm
[[email protected] opt]# 

五、在/opt 目录下安装 mysql的server端, 使用命令 yum -y install 包名

[email protected] opt]#  yum -y install MySQL-server-5.5.48-1.linux2.6.x86_64.rpm   #安装服务端
Loaded plugins: product-id, refresh-packagekit, security, subscription-manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
.............. 此处省略输出信息....................
Please report any problems at http://bugs.mysql.com/

  Verifying  : MySQL-server-5.5.48-1.linux2.6.x86_64                                                                 1/1 

Installed:
  MySQL-server.x86_64 0:5.5.48-1.linux2.6                                                                                

Complete!                                    # 安装成功
[[email protected] opt]# 

六、安装 mysql的client端

[[email protected] opt]# yum -y install MySQL-client-5.5.48-1.linux2.6.x86_64.rpm  # 安装 client端
Loaded plugins: product-id, refresh-packagekit, security, subscription-manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
Setting up Install Process
Examining MySQL-client-5.5.48-1.linux2.6.x86_64.rpm: MySQL-client-5.5.48-1.linux2.6.x86_64

.........................此处省略中间输出信息.......................

Running Transaction
  Installing : MySQL-client-5.5.48-1.linux2.6.x86_64                                                                 1/1 
  Verifying  : MySQL-client-5.5.48-1.linux2.6.x86_64                                                                 1/1 

Installed:
  MySQL-client.x86_64 0:5.5.48-1.linux2.6                                                                                

Complete!                                                 # 安装完成   
[[email protected] opt]# 

七、检查 安装mysql后, 系统中是否添加了mysql的用户和组

[[email protected] opt]# grep mysql /etc/passwd                    
mysql:x:496:493:MySQL server:/var/lib/mysql:/bin/bash
[[email protected] opt]# grep mysql /etc/group
mysql:x:493:
[[email protected] opt]# 

八、检查mysql服务是否启动, 如果没启动,则开启mysql服务

[[email protected] opt]# service mysql status            # 通过service 服务的方式查看 mysql 状态
 ERROR! MySQL is not running
[[email protected] opt]# /etc/init.d/mysql status        # 通过脚本 方式查看 mysql 状态
 ERROR! MySQL is not running
[[email protected] opt]# ps -ef | grep mysql             # 通过查看进程的方式,来判断 mysql 是否开启
root       2274   2025  0 22:08 pts/0    00:00:00 grep mysql
[[email protected] opt]# 
[[email protected] opt]# service mysql start             # 启动 mysql 服务
Starting MySQL... SUCCESS! 
[[email protected] opt]# 
[[email protected] opt]# service mysql status            
 SUCCESS! MySQL running (2381)
[[email protected] opt]# /etc/init.d/mysql status
 SUCCESS! MySQL running (2381)
[[email protected] opt]# ps -ef | grep mysql
root       2288      1  0 22:09 pts/0    00:00:00 /bin/sh /usr/bin/mysqld_safe --datadir=/var/lib/mysql --pid-file=/var/lib/mysql/www.pid
mysql      2381   2288  6 22:09 pts/0    00:00:00 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --user=mysql --log-error=/var/lib/mysql/www.err --pid-file=/var/lib/mysql/www.pid
root       2422   2025  0 22:09 pts/0    00:00:00 grep mysql
[[email protected] opt]# 

九、设置 mysql 服务开机自启

[[email protected] opt]# chkconfig --list mysql        # 查看 mysql 服务开机自启情况
mysql          	0:关闭	1:关闭	2:关闭	3:关闭	4:关闭	5:关闭	6:关闭
[[email protected] opt]# chkconfig mysql on            # 将 mysql 服务设置为开机自启
[[email protected] opt]# chkconfig --list mysql
mysql          	0:关闭	1:关闭	2:启用	3:启用	4:启用	5:启用	6:关闭
[[email protected] opt]# 

十、查看 mysql 服务监听端口

[[email protected] opt]# netstat -anpt | grep mysql   # 3306 端口为 mysql 服务的默认端口号
tcp        0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN      2381/mysqld         
[[email protected] opt]# 

十一、刚装好的 mysql 服务在登录的时候是不需要密码的

[[email protected] opt]# mysql             # 刚装完 mysql 软件, 没有设置密码的时候, 不需要密码就能登录
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.48 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;            # 显示当前 数据库
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)

mysql> quit                       # quit 退出 
Bye
[[email protected] opt]# 

十二、 为 mysql 服务器 root管理员 设置登录密码

[[email protected] opt]# mysql                        # 使用 mysql 命令连接到本地 mysql 服务器
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.5.48 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> SET PASSWORD FOR 'root'@'localhost'=PASSWORD('abc123456');   # 设置 root 用户的密码
Query OK, 0 rows affected (0.00 sec)

mysql> quit                                                         # 退出 mysql 客户端
Bye
[[email protected] opt]# mysql                       # 再次使用 mysql 命令时, 提示要输入用户名和密码
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

十三、登录 以root 用户登录 mysql 服务器

[[email protected] opt]# mysql -u root  -pabc123456    # -u 指定登录用户名, -p指定密码,可以直接跟上密码. 相当于 先执行 mysql -u root  -p 然后再输入密码
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 18
Server version: 5.5.48 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>