前言

在linux上安装mysql5.6版本,并远程连接mysql数据库操作

安装mysql

mysql的安装可以用yum安装更方便

[root@yoyo ~]# cd /usr/local/
[root@yoyo ~]# mkdir mysql-community-release
[root@yoyo ~]# cd mysql-community-release
[root@yoyo ~]# wget http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm
[root@yoyo ~]# rpm -ivh mysql-community-release-el7-5.noarch.rpm
[root@yoyo ~]# yum -y install mysql-community-server

安装完成后查看版本号:mysql -V

[root@yoyo local]# mysql -V
mysql Ver 14.14 Distrib 5.6.42, for Linux (x86_64) using EditLine wrapper

安装完成后重启mysql服务,查看状态是 Active: active (running) ,说明启动成功

启动服务:service mysqld restart

[root@yoyo local]# service mysqld restart

查看mysql运行状态:systemctl status mysql.service

[root@yoyo local]# systemctl status mysql.service

[root@yoyo local]# service mysqld restart
Redirecting to /bin/systemctl restart mysqld.service
Job for mysqld.service failed because the control process exited with error code. See "systemctl status mysqld.service" and "journalctl -xe" for details. [root@yoyo ~]# systemctl status mysql.service
● mysqld.service - MySQL Community Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since Tue 2019-01-15 09:53:42 CST; 47s ago
Main PID: 946 (mysqld_safe)
CGroup: /system.slice/mysqld.service
├─ 946 /bin/sh /usr/bin/mysqld_safe --basedir=/usr
└─1282 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --log-error=/v... Jan 15 09:53:39 yoyo systemd[1]: Starting MySQL Community Server...
Jan 15 09:53:40 yoyo mysqld_safe[946]: 190115 09:53:40 mysqld_safe Logging to '/var/log/mysqld.log'.
Jan 15 09:53:40 yoyo mysqld_safe[946]: 190115 09:53:40 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
Jan 15 09:53:42 yoyo systemd[1]: Started MySQL Community Server.

mysql重置密码

方法一:

初次安装使用mysql,root账户默认是没设置密码的,系统会给个临时密码,在/var/log/mysqld.log可以查看

[root@yoyo local]# grep 'temporary password' /var/log/mysqld.log

如下图所示,出现的就是临时密码,复制出来就可以登录mysql了

[root@yoyo local]# mysql -u root -p

看到Enter password: 输入密码,重置密码后exit退出mysql

[root@yoyo local]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 26
Server version: 5.6.42 MySQL Community Server (GPL) Copyright (c) 2000, 2018, 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> update user set password = Password('root') where User = 'root';
# 回车后执行(刷新MySQL系统权限相关的表):
mysql> flush privileges;
# 再执行exit退出:
mysql> exit;
Bye
[root@yoyo local]#

方法二:

要是上一步找不到临时密码,那就用此方法,先停掉mysql,以安全方式启动

[root@yoyo local]# systemctl stop mysql.service

以安全方式启动mysql:

[root@yoyo local]# /usr/bin/mysqld_safe --skip-grant-tables >/dev/null 2>&1 &

然后执行

[root@yoyo local]# /usr/bin/mysql -u root mysql

出现“mysql>”提示符后输入:

mysql> update user set password = Password('root') where User = 'root';
回车后执行(刷新MySQL系统权限相关的表):
mysql> flush privileges;
再执行exit退出:
mysql> exit;

退出后,使用以下命令登陆mysql,试试是否成功:

[root@yoyo local]#mysql -u root -p

按提示输入密码:root

[root@yoyo local]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 27
Server version: 5.6.42 MySQL Community Server (GPL) Copyright (c) 2000, 2018, 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>

出现Welcome to the MySQL那就是登录成功了

查看mysql端口号

mysql默认端口是3306,如何查看msyql端口号呢?可以用root账号登录后,执行show variables like 'port';

[root@yoyo local]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 33
Server version: 5.6.42 MySQL Community Server (GPL) Copyright (c) 2000, 2018, 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 variables like 'port';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| port | 3306 |
+---------------+-------+
1 row in set (0.00 sec) mysql>

授权mysql远程连接

mysql在linux上安装完成后,为了方便的查看,可以在本地电脑上安装一个远程连接数据库的客户端,远程连上mysql

方法一:

先创建一个root新用户,登录密码为password,可以自己随便命名

mysql> create user 'root'@'%' identified by 'password';

[root@yoyo sysconfig]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g. mysql> create user 'root'@'%' identified by 'password';
ERROR 1396 (HY000): Operation CREATE USER failed for 'root'@'%'
mysql> exit

方法二:

查看user表,把host为localhost,user为root的记录更新host为%就是允许远程访问了

操作mysql时候,先执行use mysql

[root@yoyo sysconfig]# mysql -u root -p
Enter password:
mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
\Database changed
mysql> select user,host,password from user;
+-------+-----------+-------------------------------------------+
| user | host | password |
+-------+-----------+-------------------------------------------+
| root | yoyo | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
| root | 127.0.0.1 | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
| root | ::1 | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
| | yoyo | |
| root1 | % | *668425423DB5193AF921380129F465A6425216D0 |
| root | localhost | *2470C0C06DEE42FD1618BB99005ADCA2EC9D1E19 |
+-------+-----------+-------------------------------------------+
6 rows in set (0.00 sec)

如果看到root后面的host对应的是%说明有远程访问权限,显示localhost就 update更新它,如何flush privileges刷新系统权限

mysql> use mysql
mysql> update user set host = '%' where user = 'root';
ERROR 1062 (23000): Duplicate entry '%-root' for key 'PRIMARY'
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec) mysql> select user,host,password from user;
+-------+-----------+-------------------------------------------+
| user | host | password |
+-------+-----------+-------------------------------------------+
| root | yoyo | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
| root | 127.0.0.1 | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
| root | ::1 | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
| | yoyo | |
| root1 | % | *668425423DB5193AF921380129F465A6425216D0 |
| root | % | *2470C0C06DEE42FD1618BB99005ADCA2EC9D1E19 |
+-------+-----------+-------------------------------------------+
6 rows in set (0.00 sec)
mysql> exit

方法三:

授权法,给root用户远程登录的权限

# 想root使用123456从任何主机连接到mysql服务器的话
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123546' WITH GRANT OPTION; # 如果你想允许用户root从ip为192.168.1.3的主机连接到mysql服务器,并使用123456作为密码 mysql>GRANT ALL PRIVILEGES ON *.* TO 'root'@'192.168.1.3' IDENTIFIED BY '123456' WITH GRANT OPTION;

接下来去阿里云ECS服务后台安全组-添加规则,新增3306端口访问权限,使用Navicat远程工具就可以连上了

这里的账号密码就是前面方法一里面设置的“root” 和“password”

开启与关闭服务

1启动mysql

service mysqld start

2查看mysql运行状态

service mysqld status # 或者 systemctl status mysql.service

3停掉mysql服务

service mysqld stop # 或者 systemctl stop mysql.service

4重启mysql

systemctl restart mysql.service

5查看运行进程

ps -ef | grep mysqld

[root@yoyo sysconfig]# ps -ef | grep mysql
mysql 2506 1 0 12:51 ? 00:00:00 /bin/sh /usr/bin/mysqld_safe --basedir=/usr
mysql 2674 2506 0 12:51 ? 00:00:00 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64mysql/plugin --log-error=/var/log/mysqld.log --pid-file=/var/run/mysqld/mysqld.pid --socket=/var/lib/mysql/mysql.sock
root 2748 1668 0 12:55 pts/0 00:00:00 grep --color=auto mysql

6查看mysql端口

netstat -tulpn |grep mysql

[root@yoyo sysconfig]# netstat -tulpn |grep mysql
tcp6 0 0 :::3306 :::* LISTEN 2674/mysqld

遇到问题

启动mysql的时候,出现Failed to start MySQL Community Server.具体报错如下

[root@yoyo ~]# systemctl status mysql.service
● mysqld.service - MySQL Community Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: failed (Result: start-limit) since Mon 2019-01-14 20:31:27 CST; 13h ago
Process: 26800 ExecStartPost=/usr/bin/mysql-systemd-start post (code=exited, status=0/SUCCESS)
Process: 26799 ExecStart=/usr/bin/mysqld_safe --basedir=/usr (code=exited, status=1/FAILURE)
Process: 26786 ExecStartPre=/usr/bin/mysql-systemd-start pre (code=exited, status=0/SUCCESS)
Main PID: 26799 (code=exited, status=1/FAILURE) Jan 14 20:31:27 yoyo systemd[1]: Failed to start MySQL Community Server.
Jan 14 20:31:27 yoyo systemd[1]: Unit mysqld.service entered failed state.
Jan 14 20:31:27 yoyo systemd[1]: mysqld.service failed.
Jan 14 20:31:27 yoyo systemd[1]: mysqld.service holdoff time over, scheduling restart.
Jan 14 20:31:27 yoyo systemd[1]: start request repeated too quickly for mysqld.service
Jan 14 20:31:27 yoyo systemd[1]: Failed to start MySQL Community Server.
Jan 14 20:31:27 yoyo systemd[1]: Unit mysqld.service entered failed state.
Jan 14 20:31:27 yoyo systemd[1]: mysqld.service failed.

刚开始以为是哪里配置有问题,后来百度搜了下,reboot重启linux服务器就解决了。

交流QQ群:779429633

Linux学习15-CentOS安装mysql5.6环境的更多相关文章

  1. Linux学习(一)------CentOs安装mysql5.5 数据库

    具体方法和步骤如下所示: 1.第一步就是看linu是否安装了mysql,经过rpm -qa|grep mysql查看到centos下安装了mysql5.1,那就开始卸载咯 2.接下来就是卸载mysql ...

  2. Linux学习之CentOS(一)--CentOS6.4环境搭建

    Linux学习之CentOS(一)--CentOS6.4环境搭建http://www.cnblogs.com/xiaoluo501395377/archive/2013/03/31/CentOs.ht ...

  3. Linux学习之CentOS(一)--CentOS6.5环境搭建

    一.前言 作为一个从事运维工作好几年的老运维来说,linux系统怎能不学呢?所以,这几天自己准备学习一下linux操作系统.废话不多说,直奔主题. 要学linux开发,首先得要安装linux系统吧,这 ...

  4. [转] Linux学习之CentOS(十三)--CentOS6.4下Mysql数据库的安装与配置

    from:  http://www.cnblogs.com/xiaoluo501395377/archive/2013/04/07/3003278.html 如果要在Linux上做j2ee开发,首先得 ...

  5. Linux学习之CentOS(十三)--CentOS6.4下Mysql数据库的安装与配置

    原文:http://www.cnblogs.com/xiaoluo501395377/archive/2013/04/07/3003278.html 如果要在Linux上做j2ee开发,首先得搭建好j ...

  6. linux学习之centos(三):mysql数据库的安装和配置

    前言:mysql简介 说到数据库,我们大多想到的是关系型数据库,比如mysql.oracle.sqlserver等等,这些数据库软件在windows上安装都非常的方便,在Linux上如果要安装数据库, ...

  7. Linux学习之CentOS(十三)--CentOS6.4下Mysql数据库的安装与配置(转)

    原文地址:http://www.cnblogs.com/xiaoluo501395377/archive/2013/04/07/3003278.html 如果要在Linux上做j2ee开发,首先得搭建 ...

  8. Linux学习之CentOS(五)--CentOS下VMware-Tools安装

    已经进入到了Linux学习之CentOS的第六篇随笔了,所以这里就介绍一下VMware-Tools的安装. VMware-Tools的安装 VMware-Tools 主要的功能就是让用户在虚拟机和真实 ...

  9. linux学习之centos(三):网卡配置

    Linux系统版本:Centos 6.5 在linux学习之centos(二):虚拟网络三种连接方式和SecureCRT的使用中,使用远程工具SecureCRT,通过“ifconfig eth0 + ...

  10. Linux - VMware和Centos安装

    目录 Linux - VMware和Centos安装 选择性 下载centos系统ISO镜像 安装虚拟机VMware虚拟机 1. 准备vmware软件 2. 解压软件包, 当前选择vm12 3. vm ...

随机推荐

  1. wpf 千位符 格式化字符串

    StringFormat={}{0:N2}}//格式话字符串,增加千位符,2保留小数点后2位 StringFormat={}{0:N0}}//格式话字符串,增加千位符,无小数点后

  2. Java 接口关键字 interface

    interface这个关键字产生一个完全抽象的类,它根本就没有提供任何具体的实现,它允许创建者确定方法名.参数列表和返回类型,但没有任何方法体,接口只提供了形式,而未提供任何具体实现 一个接口表示:& ...

  3. KnockoutJs学习笔记(十二)

    value binding一般适用于input.select.textarea等form elements中,能够将view model中的属性和相关联的DOM element的值(value)连接起 ...

  4. hdu 1754 线段树(单点替换 区间最值)

    Sample Input5 61 2 3 4 5Q 1 5 //1-5结点的最大值U 3 6 //将点3的数值换成6Q 3 4Q 4 5U 2 9Q 1 5 Sample Output5659 # i ...

  5. 图学ES6-5.正则的扩展

  6. (转阮一峰)深入理解OAuth 2.0

    OAuth是一个关于授权(authorization)的开放网络标准,在全世界得到广泛应用,目前的版本是2.0版. 本文对OAuth 2.0的设计思路和运行流程,做一个简明通俗的解释,主要参考材料为R ...

  7. 【LOJ】#2264. 「CTSC2017」吉夫特

    题解 根据一番认真严肃的猜结论和打表证明之后 我们可以得到 \(f[i] = (\sum_{a[i] \& a[j] == a[j]} f[j]) + 1\) 统计所有的\(f[i] - 1\ ...

  8. 【LOJ】#2077. 「JSOI2016」飞机调度

    题解 考虑一架飞机飞完自己之后还能飞到哪些航线,用floyd求两点最短路 这个图建出来是个DAG,求最小路径覆盖即可,二分图匹配 注意判断时是航班的起飞时刻+直飞时间+加油时间+最短路时间 代码 #i ...

  9. kebab HDU2883

    题意:现在有n个人要烤肉,有m个烤肉架,然后给出每个人的烤肉开始时间si,结束时间ei,以及要烤肉的串数num,还有拷一串的时间ti,然后问你能不能满足所有人的要求. 为3572的进阶题 每个人为一个 ...

  10. RN组件可用属性整理