zoukankan      html  css  js  c++  java
  • mysql 数据库管理

    1、启动数据库

    1、1、mysql启动原理

       在mysql服务器启动后,有2个进程mysqld_safe,mysqld,其中mysqld_safe是mysqld的父进程。

       /etc/init.d/mysql 中针对start的执行,执行的mysqld_safe,而mysqld_safe是一个脚本;通过查看mysqld_safe的选项信息以及脚本内容,确定mysqld_safe启动的是mysqld;mysql的配置文件my.cnf,它包含的选项其实有mysqld_safe的部分,也有mysqld的部分,也就是说my.cnf其实是mysqld_safe的配置文件。

       mysqld_safe完成mysqld的启动和数据库文件的搜索,也就是mysqld_safe中的--ledir与--datadir  

       mysql的启动和重启的运行错误日志hostname.err实际上是mysqld_safe来写的,mysqld_safe实际上还完成如下功能:

       (1)启动和检测mysqld。

      (2)收集mysqld的错误消息,并将消息写入到--datadir中的hostname.err文件中。

       (3)检查mysql的配置文件my.cnf和系统。

    [root@DB02 bin]# ps -ef |grep mysql
    root       5601   5246  0 22:05 pts/1    00:00:00 /bin/sh /application/mysql/bin/mysqld_safe --skip-grant-table --user=mysql
    mysql      5717   5601  0 22:05 pts/1    00:00:00 /application/mysql-5.6.34/bin/mysqld --basedir=/application/mysql-5.6.34 --datadir=/application/mysql-5.6.34/data 
    --plugin-dir=/application/mysql-5.6.34/lib/plugin --user=mysql --skip-grant-table --log-error=/application/mysql-5.6.34/data/DB02.err --pid-file=/application/mysql-5.6.34/data/
    DB02.pid root 5859 5246 0 22:21 pts/1 00:00:00 grep mysql 当使用命令查询mysql进程时会发现有两个进程:mysqld_safe和mysqld。这相当于Nginx启动时的master(管理进程)和worker(工作进程)。 当我们使用/etc/init.d/mysqld start启动时,实际上是调用mysqld_safe,我们可以直接使用 /application/mysql/bin/mysqld_safe --skip-grant-table --user=mysql 启动数据库;实际上最后提供服务的是上面黄色部分内容; 启动原理:/etc/init.d/mysqld是一个shell启动脚步,启动后会调用mysql_safe脚本,最后调用mysqld主程序启动mysql。

    1.2、启动数据库方法

    在初始化数据库的时候有这么一条命令:

    cp support-files/mysql.server /etc/init.d/mysqld

    这条命令就是把数据库自带的启动脚步,拷贝到/etc/init.d目录下实现管理MySQL服务的启动和停止。

    下面为大家介绍单实例数据库的两种启动数据库的方法:

    #第一种利用数据库自带的脚步启动数据库

    [root@DB02 ~]# /etc/init.d/mysqld start
    Starting MySQL. SUCCESS!
    [root@DB02 ~]#

    第二种:利用脚步启动MySQL

    [root@DB02 ~]# mysqld_safe --user=mysql &
    [1] 4533
    [root@DB02 ~]# 170620 19:24:46 mysqld_safe Logging to '/application/mysql-5.6.34/data/DB02.err'.
    170620 19:24:46 mysqld_safe Starting mysqld daemon with databases from /application/mysql-5.6.34/data
    [root@DB02 ~]# netstat -lnut|egrep 3306
    tcp        0      0 :::3306                     :::*                        LISTEN  

    2、关闭数据库

    关闭MySQL数据库的方法选择顺序如下:

    #第一种最先使用MySQL自带的管理脚步,实例如下:

    [root@DB02 ~]# /etc/init.d/mysqld stop
    Shutting down MySQL.. SUCCESS!
    [root@DB02 ~]#

    第二种使用MySQladmin管理方法

    [root@DB02 ~]# mysqladmin -uroot -pdadong123 shutdown
    Warning: Using a password on the command line interface can be insecure.
    [root@DB02 ~]# netstat -lnutp|grep 330

    说明:这个命令最大的障碍就是必须实现知道密码。

    第三种利用系统进程管理命令关闭MySQL

    kill pid        ###这里的pid为数据库服务对应的进程号。
    killall mysqld    ##这里的mysqld是数据库服务对应的进程名字
    pkill  mysqld    ###这里的mysqlds是数据库服务对应的进程名字。
    千万不能用kill  -9  ,因为会造成数据丢失,数据库启动不了

    3、修改数据库密码

    方法1: 用SET PASSWORD命令 

    首先登录MySQL。 
    格式:mysql> set password for 用户名@localhost = password('新密码');
    例子:mysql> set password for root@localhost = password('123');

    方法2:用mysqladmin

    格式:mysqladmin -u用户名 -p旧密码 password 新密码 
    例子:mysqladmin -uroot -p123456 password 123

    方法3:用UPDATE直接编辑user表 

    首先登录MySQL。 
    mysql> use mysql;
    mysql> update user set password=password('123') where user='root' and host='localhost';
    mysql> flush privileges;    ###必须要重新刷新,否则退出再登陆的时候会出现使用修改后的密码登陆不进去的情况。

    方法4:在忘记root密码的时候,可以这样

    使用这种方法必须先关闭数据库/etc/init.d/mysqld stop

    [root@DB02 bin]# mysqld_safe --skip-grant-table --user=mysql &
    [1] 5327
    --skip-grant-tables 的意思是启动MySQL服务的时候跳过权限表认证。
    [root@DB02 bin]# 170620 21:50:59 mysqld_safe Logging to '/application/mysql-5.6.34/data/DB02.err'.
    170620 21:50:59 mysqld_safe Starting mysqld daemon with databases from /application/mysql-5.6.34/data
    [root@DB02 bin]# mysql
    Welcome to the MySQL monitor.  Commands end with ; or g.
    Your MySQL connection id is 1
    Server version: 5.6.34 Source distribution
    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.
    改密码:update user set password=password("123") where user="root";(别忘了最后加分号)
    mysql> update mysql.user set password=password('dadong123') where user='root' and host='localhost';
    Query OK, 1 row affected (0.04 sec)
    Rows matched: 1  Changed: 1  Warnings: 0
    刷新权限(必须步骤):flush privileges; 
    mysql> flush privileges;
    Query OK, 0 rows affected (0.00 sec)
    退出 quit。 
    mysql> exit
    Bye
    注销系统,再进入,使用用户名root和刚才设置的新密码123登录。
    [root@DB02 bin]# mysql -uroot -p123

    当我们使用跳过授权表登陆数据库修改完密码,若想使用/etc/init.d/mysqld start登陆时,需要先关闭数据库。

    有时候重启数据库后,会出现上面的的错误,没有任何明显的错误信息,这个时候可以检查mysql目录文件下文件的属主和属组是否是mysql。

  • 相关阅读:
    python 列表、元组、字典总结
    python 字典
    python 元组
    python 列表
    JMeter的作用域与执行顺序
    JMeter第一个实战
    JMeter录制的两种方法
    JMeter常用功能
    初识jmeter2
    handler的拒绝策略:
  • 原文地址:https://www.cnblogs.com/dadonggg/p/8625651.html
Copyright © 2011-2022 走看看