zoukankan      html  css  js  c++  java
  • centos6.5 mysql安装+远程访问+备份恢复+基本操作+卸载

    参考博文:

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

    MySQL修改root密码的多种方法

    MySQL的备份与还原

    解决mysql导入还原时乱码的问题

    Mysql开启远程连接方法

    mysql 赋予用户权限 grant命令 

    Mysql数据库操作常用命令

    CentOS下如何完全卸载MySQL?解决卸载不干净的问题

    一、安装

    1、yum进行mysql的安装

    我是通过yum的方式来进行mysql的数据库安装,首先我们可以输入 yum list | grep mysql 命令来查看yum上提供的mysql数据库可下载的版本:

    [root@xiaoluo ~]# yum list | grep mysql

    就可以得到yum服务器上mysql数据库的可下载版本信息: 

     

    然后我们可以通过输入 yum install -y mysql-server mysql mysql-devel 命令将mysql mysql-server mysql-devel都安装好(注意:安装mysql时我们并不是安装了mysql客户端就相当于安装好了mysql数据库了,我们还需要安装mysql-server服务端才行

    [root@xiaoluo ~]# yum install -y mysql-server mysql mysql-devel

    1.2 mysql数据库的初始化及相关配置

    我们在安装完mysql数据库以后,会发现会多出一个mysqld的服务,这个就是咱们的数据库服务,我们通过输入 service mysqld start 命令就可以启动我们的mysql服务。

    注意:如果我们是第一次启动mysql服务,mysql服务器首先会进行初始化的配置,如:

    [root@xiaoluo ~]# service mysqld start
    初始化 MySQL 数据库: WARNING: The host 'xiaoluo' could not be looked up with resolveip.
    This probably means that your libc libraries are not 100 % compatible
    with this binary MySQL version. The MySQL daemon, mysqld, should work
    normally with the exception that host name resolving will not work.
    This means that you should use IP addresses instead of hostnames
    when specifying MySQL privileges !
    Installing MySQL system tables...
    OK
    Filling help tables...
    OK
    
    To start mysqld at boot time you have to copy
    support-files/mysql.server to the right place for your system
    
    PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
    To do so, start the server, then issue the following commands:
    
    /usr/bin/mysqladmin -u root password 'new-password'
    /usr/bin/mysqladmin -u root -h xiaoluo password 'new-password'
    
    Alternatively you can run:
    /usr/bin/mysql_secure_installation
    
    which will also give you the option of removing the test
    databases and anonymous user created by default.  This is
    strongly recommended for production servers.
    
    See the manual for more instructions.
    
    You can start the MySQL daemon with:
    cd /usr ; /usr/bin/mysqld_safe &
    
    You can test the MySQL daemon with mysql-test-run.pl
    cd /usr/mysql-test ; perl mysql-test-run.pl
    
    Please report any problems with the /usr/bin/mysqlbug script![确定]
    正在启动 mysqld:                                            [确定]

    这时我们会看到第一次启动mysql服务器以后会提示非常多的信息,目的就是对mysql数据库进行初始化操作,当我们再次重新启动mysql服务时,就不会提示这么多信息了,如: 

    [root@xiaoluo ~]# service mysqld restart
    停止 mysqld:                                             [确定]
    正在启动 mysqld:                                          [确定]

     我们在使用mysql数据库时,都得首先启动mysqld服务,我们可以 通过  chkconfig --list | grep mysqld 命令来查看mysql服务是不是开机自动启动,如: 

    [root@xiaoluo ~]# chkconfig --list | grep mysqld
    mysqld             0:关闭    1:关闭    2:关闭    3:关闭    4:关闭    5:关闭    6:关闭

     我们发现mysqld服务并没有开机自动启动,我们当然可以通过 chkconfig mysqld on 命令来将其设置成开机启动,这样就不用每次都去手动启动了 

    [root@xiaoluo ~]# chkconfig mysqld on
    [root@xiaoluo ~]# chkconfig --list | grep mysql
    mysqld             0:关闭    1:关闭    2:启用    3:启用    4:启用    5:启用    6:关闭

     mysql数据库安装完以后只会有一个root管理员账号,但是此时的root账号还并没有为其设置密码,在第一次启动mysql服务时,会进行数据库的一些初始化工作,在输出的一大串信息中,我们看到有这样一行信息 : 

    /usr/bin/mysqladmin -u root password 'new-password'  // 为root账号设置密码
    

     所以我们可以通过 该命令来给我们的root账号设置密码(注意:这个root账号是mysql的root账号,非Linux的root账号) 

    [root@xiaoluo ~]# mysqladmin -u root password 'root'  // 通过该命令给root账号设置密码为 root

     此时我们就可以通过 mysql -u root -p 命令来登录我们的mysql数据库了

    1.3 mysql数据库的主要配置文件

    1.3.1./etc/my.cnf 这是mysql的主配置文件

    我们可以查看一下这个文件的一些信息

    [root@xiaoluo etc]# ls my.cnf 
    my.cnf
    
    [root@xiaoluo etc]# cat my.cnf 
    [mysqld]
    datadir=/var/lib/mysql
    socket=/var/lib/mysql/mysql.sock
    user=mysql
    # Disabling symbolic-links is recommended to prevent assorted security risks
    symbolic-links=0
    
    [mysqld_safe]
    log-error=/var/log/mysqld.log
    pid-file=/var/run/mysqld/mysqld.pid

    1.3.2./var/lib/mysql   mysql数据库的数据库文件存放位置

    我们的mysql数据库的数据库文件通常是存放在了/ver/lib/mysql这个目录下 

    [root@xiaoluo ~]# cd /var/lib/mysql/
    [root@xiaoluo mysql]# ls -l
    总用量 20488
    -rw-rw----. 1 mysql mysql 10485760 4月   6 22:01 ibdata1
    -rw-rw----. 1 mysql mysql  5242880 4月   6 22:01 ib_logfile0
    -rw-rw----. 1 mysql mysql  5242880 4月   6 21:59 ib_logfile1
    drwx------. 2 mysql mysql     4096 4月   6 21:59 mysql  // 这两个是mysql数据库安装时默认的两个数据库文件
    srwxrwxrwx. 1 mysql mysql        0 4月   6 22:01 mysql.sock
    drwx------. 2 mysql mysql     4096 4月   6 21:59 test  // 这两个是mysql数据库安装时默认的两个数据库文件

    我们可以自己创建一个数据库,来验证一下该数据库文件的存放位置 

    创建一个我们自己的数据库:
    mysql> create database xiaoluo;
    Query OK, 1 row affected (0.00 sec)
    
    [root@xiaoluo mysql]# ls -l
    总用量 20492
    -rw-rw----. 1 mysql mysql 10485760 4月   6 22:01 ibdata1
    -rw-rw----. 1 mysql mysql  5242880 4月   6 22:01 ib_logfile0
    -rw-rw----. 1 mysql mysql  5242880 4月   6 21:59 ib_logfile1
    drwx------. 2 mysql mysql     4096 4月   6 21:59 mysql
    srwxrwxrwx. 1 mysql mysql        0 4月   6 22:01 mysql.sock
    drwx------. 2 mysql mysql     4096 4月   6 21:59 test
    drwx------. 2 mysql mysql     4096 4月   6 22:15 xiaoluo  // 这个就是我们刚自己创建的xiaoluo数据库
    [root@xiaoluo mysql]# cd xiaoluo/
    [root@xiaoluo xiaoluo]# ls
    db.opt

    3./var/log mysql数据库的日志输出存放位置

    我们的mysql数据库的一些日志输出存放位置都是在/var/log这个目录下

    [root@xiaoluo xiaoluo]# cd 
    [root@xiaoluo ~]# cd /var/log
    [root@xiaoluo log]# ls
    amanda                cron           maillog-20130331   spice-vdagent.log
    anaconda.ifcfg.log    cron-20130331  mcelog             spooler
    anaconda.log          cups           messages           spooler-20130331
    anaconda.program.log  dirsrv         messages-20130331  sssd
    anaconda.storage.log  dmesg          mysqld.log         tallylog
    anaconda.syslog       dmesg.old      ntpstats           tomcat6
    anaconda.xlog         dracut.log     piranha            wpa_supplicant.log
    anaconda.yum.log      gdm            pm-powersave.log   wtmp
    audit                 httpd          ppp                Xorg.0.log
    boot.log              ibacm.log      prelink            Xorg.0.log.old
    btmp                  lastlog        sa                 Xorg.1.log
    btmp-20130401         libvirt        samba              Xorg.2.log
    cluster               luci           secure             Xorg.9.log
    ConsoleKit            maillog        secure-20130331    yum.log

    其中mysqld.log 这个文件就是我们存放我们跟mysql数据库进行操作而产生的一些日志信息,通过查看该日志文件,我们可以从中获得很多信息

     因为我们的mysql数据库是可以通过网络访问的,并不是一个单机版数据库,其中使用的协议是 tcp/ip 协议,我们都知道mysql数据库绑定的端口号是 3306 ,所以我们可以通过 netstat -anp 命令来查看一下,Linux系统是否在监听 3306 这个端口号:

    结果如上所示,Linux系统监听的3306端口号就是我们的mysql数据库!!!!

    二、修改root密码的4种方法

    方法1: 用SET PASSWORD命令
    
      mysql -u root
    
      mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('newpass');
    
    方法2:用mysqladmin
    
      mysqladmin -u root password "newpass"
    
      如果root已经设置过密码,采用如下方法
    
      mysqladmin -u root password oldpass "newpass"
    
    方法3: 用UPDATE直接编辑user表
    
      mysql -u root
    
      mysql> use mysql;
    
      mysql> UPDATE user SET Password = PASSWORD('newpass') WHERE user = 'root';
    
      mysql> FLUSH PRIVILEGES;
    
    在丢失root密码的时候,可以这样
    
      mysqld_safe --skip-grant-tables&
    
      mysql -u root mysql
    
      mysql> UPDATE user SET password=PASSWORD("new password") WHERE user='root';
    
      mysql> FLUSH PRIVILEGES;

    三、MySQL的备份与还原+中文乱码问题

    MySQL备份和还原,都是利用mysqldump、mysql和source命令来完成的。 
    1.Win32下MySQL的备份与还原 
    1.1 备份 
    开始菜单 | 运行 | cmd |利用“cd /Program Files/MySQL/MySQL Server 5.0/bin”命令进入bin文件夹 | 利用“mysqldump  -u 用户名 -p databasename >exportfilename”导出数据库到文件,如mysqldump -u root -p voice>voice.sql,然后输入密码即可开始导出。 
      
    1.2 还原 
    进入MySQL Command Line Client,输入密码,进入到“mysql>”,输入命令"show databases;",回车,看看有些什么数据库;建立你要还原的数据库,输入"create database voice;",回车;切换到刚建立的数据库,输入"use voice;",回车;导入数据,输入"source voice.sql;",回车,开始导入,再次出现"mysql>"并且没有提示错误即还原成功。 
      
    2.Linux下MySQL的备份与还原 
    2.1 备份 
    [root@localhost ~]# cd /var/lib/mysql (进入到MySQL库目录,根据自己的MySQL的安装情况调整目录) 
    [root@localhost mysql]# mysqldump -u root -p voice>voice.sql,输入密码即可。
    注意:服务器运行时 加上  --skip-opt选项 否则备份不全记录
    2.2 还原 法一: [root@localhost ~]# mysql -u root -p 回车,输入密码,进入MySQL的控制台"mysql>",同1.2还原。 法二: [root@localhost ~]# cd /var/lib/mysql (进入到MySQL库目录,根据自己的MySQL的安装情况调整目录) [root@localhost mysql]# mysql -u root -p voice<voice.sql,输入密码即可。

    还原时的中文乱码问题:

    解决方法: 
    第一步:还原时建立数据库和表都为utf8格式(建议utf8,当然gbk或者gb2312也行) 
    
    复制代码 代码如下:
    
    CREATE DATABASE `shegongku` DEFAULT CHARACTER SET utf8 
    CREATE TABLE `members` ( 
    `uid` mediumint(8) unsigned NOT NULL default '0', 
    `username` varchar(15) NOT NULL default '', 
    `password` varchar(40) NOT NULL default '', 
    `salt` varchar(16) default NULL, 
    `email` varchar(60) NOT NULL default '' 
    ) ENGINE=MyISAM DEFAULT CHARSET=utf8; 
    
    
    第二步,将sql文件的编码转换下,转成utf8格式,可以notepad2,也可以用UltraEditor进行转换,再在第一行sql文件的添加一行 set names utf8 保存。 
    
    第三步,导入。一般就不会出现乱码了,这时如果你在 终端cmd下再次查询发现还是乱码的话,这个不是数据库问题,而是显示的问题,在终端里输入 set names gbk,这样中文就显示正常了
  • 相关阅读:
    spring data jpa 分页查询(小结)
    java 工作日判断
    Map的merge方法(java8)
    java8 常用JVM 参数修改
    ubuntu docker client 安装
    scala io 读写文件
    Grafana 系统资源监测
    Java8Stream Collectors收集器
    EhCacheUtils 缓存 ehche (将文件临时保存在磁盘)
    springboot2.X 整合scala
  • 原文地址:https://www.cnblogs.com/wuling129/p/4809547.html
Copyright © 2011-2022 走看看