CentOS7启动、停止MySQL

一、启动

[root@localhost ~]#service mysqld start

Redirecting to /bin/systemctl start  mysql.service

 

说明:

1.如果没报错就说明启动成功!

 

注意:有时候启动MySQL会报错,比如:

得到错误:ERROR!The server quit without updating PID file

我们这里主要是因为:selinux惹的祸,如果是centos系统,默认会开启selinux。解决方法是关闭它,打开/etc/selinux/config,把SELINUX=enforcing改为SELINUX=disabled后存盘退出重启机器。

然后再启动mysql就没问题了。

 

 

二、查看MySQL运行状态

[root@localhost ~]#service mysqld status

Redirecting to /bin/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 2016-07-25 11:37:04CST; 15s ago

  Process:14808 ExecStartPost=/usr/bin/mysql-systemd-start post (code=exited,status=0/SUCCESS)

  Process:14701 ExecStartPre=/usr/bin/mysql-systemd-start pre (code=exited,status=0/SUCCESS)

 Main PID:14807 (mysqld_safe)

   CGroup:/system.slice/mysqld.service

          ├─14807 /bin/sh /usr/bin/mysqld_safe

          └─14958 /usr/sbin/mysqld --basedir=/usr--datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin--log-error=/var/log/mysqld.log --pid-fil...

 

7 25 11:36:59localhost.localdomain mysql-systemd-start[14701]: 2016-07-25 11:36:59 14782[Note] InnoDB: 5.6.31 started; log sequence number 1625977

7 25 11:36:59localhost.localdomain mysql-systemd-start[14701]: 2016-07-25 11:36:59 14782[Note] Binlog end

7 25 11:36:59localhost.localdomain mysql-systemd-start[14701]: 2016-07-25 11:36:59 14782[Note] InnoDB: FTS optimize thread exiting.

7 25 11:36:59localhost.localdomain mysql-systemd-start[14701]: 2016-07-25 11:36:59 14782[Note] InnoDB: Starting shutdown...

7 25 11:37:00localhost.localdomain mysql-systemd-start[14701]: 2016-07-25 11:37:00 14782[Note] InnoDB: Shutdown completed; log sequence number 1625987

7 25 11:37:00localhost.localdomain mysql-systemd-start[14701]: PLEASE REMEMBER TO SET APASSWORD FOR THE MySQL root USER !

7 25 11:37:00localhost.localdomain mysql-systemd-start[14701]: To do so, start the server,then issue the following commands:

7 25 11:37:01localhost.localdomain mysqld_safe[14807]: 160725 11:37:01 mysqld_safe Loggingto '/var/log/mysqld.log'.

7 25 11:37:01localhost.localdomain mysqld_safe[14807]: 160725 11:37:01 mysqld_safe Startingmysqld daemon with databases from /var/lib/mysql

7 25 11:37:04localhost.localdomain systemd[1]: Started MySQL Community Server.

三、连接MySQL服务

[root@localhost ~]# mysql -u root -p

Enter password:

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

 

注意:错误原因是初次安装mysql是root账户是没有密码的,需要设置密码

 

解决这个问题,需安装以下步骤顺序执行:

1.停止MySQL服务

[root@localhost ~]#service mysqldstop

Redirecting to /bin/systemctl stop  mysql.service

 

注意:如果mysql没有被停止,则使用kill -9命令杀掉MySQL进程。

 

2.输入绕过密码认证命令

[root@localhost ~]# mysqld_safe--user=mysql --skip-grant-tables--skip-networking &

[1] 16835

[root@localhost ~]# 160725 12:03:25 mysqld_safeLogging to '/var/log/mysqld.log'.

160725 12:03:25 mysqld_safe Starting mysqld daemonwith databases from /var/lib/mysql

160725 12:03:43 mysqld_safe mysqld from pid file/var/run/mysqld/mysqld.pid ended

[root@localhost ~]#

[1]+ 完成                  mysqld_safe --user=mysql--skip-grant-tables--skip-networking

 

说明:此时MySQL被启动,可以通过命令ps -ef | grep mysql查看。

 

3.使用root用户登录mysql数据库

[root@localhost ~]# mysql -u rootmysql

Reading table information for completion of table andcolumn names

You can turn off this feature to get a quickerstartup with -A

 

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 1

Server version: 5.6.31 MySQL Community Server (GPL)

 

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

 

Oracle is a registered trademark of OracleCorporation and/or its

affiliates. Other names may be trademarks of theirrespective

owners.

 

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

 

mysql>

 

4.更新root用户密码

mysql> UPDATE user SETPassword=PASSWORD('rootroot') where USER='root';

Query OK, 4 rows affected (0.01 sec)

Rows matched: 4 Changed: 4  Warnings: 0

 

mysql> flush privileges;

Query OK, 0 rows affected (0.00 sec)

 

5.刷新权限

mysql> flush privileges;

Query OK, 0 rows affected (0.00 sec)

 

6.退出MySQL

mysql> quit

Bye

 

7.重启MySQL

[root@localhost ~]# service mysqlrestart

Redirecting to /bin/systemctl restart  mysql.service

Job for mysqld.service failed because the controlprocess exited with error code. See "systemctl status mysqld.service"and "journalctl -xe" for details.

 

8.连接MySQL

[root@localhost ~]# mysql -u root -p

Enter password:

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 7

Server version: 5.6.31 MySQL Community Server (GPL)

 

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

 

Oracle is a registered trademark of OracleCorporation and/or its

affiliates. Other names may be trademarks of theirrespective

owners.

 

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

 

mysql>

 

----------------------------------------------以下部分为验证部分,非必须步骤------------------------------------------

 

9.查看数据库

mysql> show databases;

+-------------------------------+

| Database                        |

+-------------------------------+

| information_schema   |

| mysql                              |

| performance_schema |

+-------------------------------+

3 rows in set (0.00 sec)

 

10.使用mysql数据库

mysql> use mysql;

Reading table information for completion of table andcolumn names

You can turn off this feature to get a quickerstartup with -A

 

Database changed

 

11.查看mysql数据库表

mysql> show tables;

+------------------------------------+

| Tables_in_mysql                  |

+------------------------------------+

| columns_priv                        |

| db                                            |

| event                                     |

| func                                       |

| general_log                          |

| help_category                      |

| help_keyword                     |

| help_relation                       |

| help_topic                            |

| innodb_index_stats           |

| innodb_table_stats           |

| ndb_binlog_index              |

| plugin                                   |

| proc                                      |

| procs_priv                           |

| proxies_priv                        |

| servers                                 |

| slave_master_info             |

| slave_relay_log_info         |

| slave_worker_info             |

| slow_log                              |

| tables_priv                          |

| time_zone                           |

| time_zone_leap_second  |

| time_zone_name              |

| time_zone_transition       |

| time_zone_transition_type |

| user                                      |

+------------------------------------+

28 rows in set (0.00 sec)

四、停止MySQL服务

[root@localhost ~]# service mysqlstop

Redirecting to /bin/systemctl stop  mysql.service

 

  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值