linux安装MySQL

今天在虚拟机上装了MySQL,安装过程稍有曲折,反复装了三次。参照网上帖子装,前两次安装完成后,不能成功启动MySQL,后来查到另一个教程,删除虚拟机自带的Mariadb后再次安装,就成功了。

参考教程:http://blog.csdn.net/zhou920786312/article/details/77750604

参考教程2:http://blog.csdn.net/qq_32331073/article/details/76252559

1:下载

[root@localhost soft]# wget https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.19-linux-glibc2.12-x86_64.tar.gz
  • 1

2:解压文件

[root@dbserver /]# tar -xzvf mysql-5.7.19-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
  • 1

3修改文件

[root@dbserver local]# mv  mysql-5.7.19-linux-glibc2.12-x86_64 mysql
  • 1

4:检查库文件是否有删除,若有便删除(linux系统自带的)

    [root@dbserver mysql]# rpm -qa | grep mysql
  • 1
  删除
   [root@dbserver mysql]# rm -e –-nodeps mysql-libs-5.1.52.x86_64
  • 1

5:检查mysql组和用户是否存在,如无创建

      [root@dbserver ~]# cat /etc/group | grep mysql
    [root@dbserver ~]# cat /etc/passwd |grep mysql
  • 1
  • 2

创建

    [root@dbserver ~]#groupadd mysql
    [root@dbserver ~]#useradd -r -g mysql mysql
        //useradd -r参数表示mysql用户是系统用户,不可用于登录系统
  • 1
  • 2
  • 3

6:在mysql下添加data目录

      [root@dbserver mysql]# mkdir data
  • 1

7:更改mysql目录下所有的目录及文件夹所属组合用户

[root@dbserver mysql]# cd /usr/local/
[root@dbserver local]# chown -R mysql mysql/
[root@dbserver local]# chgrp -R mysql mysql/
[root@dbserver local]# cd mysql/
[root@dbserver mysql]# ls -l
total 40
drwxr-xr-x.  2 mysql mysql  4096 Aug 31 16:45 bin
-rw-r--r--.  1 mysql mysql 17987 Jun 22 22:13 COPYING
drwxr-xr-x.  2 mysql mysql     6 Aug 31 16:48 data
drwxr-xr-x.  2 mysql mysql    52 Aug 31 16:45 docs
drwxr-xr-x.  3 mysql mysql  4096 Aug 31 16:44 include
drwxr-xr-x.  5 mysql mysql  4096 Aug 31 16:45 lib
drwxr-xr-x.  4 mysql mysql    28 Aug 31 16:45 man
-rw-r--r--.  1 mysql mysql  2478 Jun 22 22:13 README
drwxr-xr-x. 28 mysql mysql  4096 Aug 31 16:45 share
drwxr-xr-x.  2 mysql mysql    86 Aug 31 16:45 support-files
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

8:安装和初始化数据库
安装

   ./mysqld --initialize --user=mysql --basedir=/usr/local/mysql/--datadir=/usr/local/mysql/data/


2017-08-31T08:50:23.910440Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2017-08-31T08:50:23.910635Z 0 [ERROR] Can't find error-message file '/usr/local/mysql/--datadir=/usr/local/mysql/data/share/errmsg.sys'. Check error-message file location and 'lc-messages-dir' con
figuration directive.2017-08-31T08:50:24.709286Z 0 [Warning] InnoDB: New log files created, LSN=45790
2017-08-31T08:50:24.767540Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2017-08-31T08:50:24.892629Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 6e083b8f-8e29-11e7-88b1-
005056b427be.2017-08-31T08:50:24.895674Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2017-08-31T08:50:24.896645Z 1 [Note] A temporary password is generated for root@localhost: gFamcspKm2+u
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

报错[ERROR] Can’t find error-message file ‘/usr/local/mysql/–datadir=/usr/local/mysql/data/share/errmsg.sys’. Check error-message file location and ‘lc-messages-dir’ con

解决

[root@dbserver bin]# ./mysqld --initialize --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/ --lc_messages_dir=/usr/local/mysql/share --lc_messages=en_US
2017-08-31T09:00:54.941514Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2017-08-31T09:00:56.364312Z 0 [Warning] InnoDB: New log files created, LSN=45790
2017-08-31T09:00:56.602211Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2017-08-31T09:00:56.668145Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: e69986d2-8e2a-11e7-a335-
005056b427be.2017-08-31T09:00:56.671464Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2017-08-31T09:00:56.672453Z 1 [Note] A temporary password is generated for root@localhost: qfuqvCsHb2!.
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

9配置my.cnf
接下来进入/usr/local/mysql/support-files/目录下
查看是否存在my-default.cnf文件,如果存在直接copy到/etc/my.cnf文件中

    [root@dbserver mysql]# cp -a ./support-files/my-default.cnf /etc/my.cnf
  • 1

如果不存在my-default.cnf文件,则在/etc/目录下创建my.cnf,并写入以下内容

#[mysql]  
#basedir=/usr/local/mysql/  
#datadir=/usr/local/mysql/data/  
  • 1
  • 2
  • 3

10启动服务

[root@dbserver mysql]# cd bin/
[root@dbserver bin]# ./mysqld_safe --user=mysql &
[2] 10436
[root@dbserver bin]# Logging to '/var/log/mysql/mysql.log'.
2017-08-31T09:52:15.806633Z mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
2017-08-31T09:52:16.292949Z mysqld_safe mysqld from pid file /var/run/mysql/mysql.pid ended
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

11将mysqld服务加入开机自启动项。
将{mysql}/ support-files/mysql.server 拷贝为/etc/init.d/mysql并设置运行权限,这样就可以使用service mysql命令启动/停止服务,
否则就只能使用{mysql}/bin/mysqld_safe &命令来启动服务
还需要把mysql.server中basedir的相关路径,改为自定义的路径,默认路径是/usr/local/mysql

[root@dbserver support-files]# cp mysql.server /etc/init.d/mysql  
[root@dbserver support-files]# chmod +x /etc/init.d/mysql 
-- 把mysql注册为开机启动的服务
[root@dbserver support-files]# chkconfig --add mysql  
-- 查看是否添加成功
[root@dbserver support-files]#  chkconfig --list mysql  
Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.

      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.

mysql           0:off   1:off   2:on    3:on    4:on    5:on    6:off

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

12启动服务

[root@dbserver bin]# service mysql start
Starting MySQL.Logging to '/var/log/mysql/mysql.log'.
 ERROR! The server quit without updating PID file (/var/lib/mysql/dbserver.pid).
  • 1
  • 2
  • 3
  • 4

解决

[root@dbserver mysql]# rm  /etc/my.cnf
rm: remove regular file '/etc/my.cnf'? y
[root@dbserver mysql]# /etc/init.d/mysql start 
Starting MySQL.Logging to '/usr/local/mysql/data/dbserver.err'.
 SUCCESS! 
[root@dbserver mysql]# service mysql start
Starting MySQL SUCCESS! 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

13登录mysql

[root@dbserver bin]# ./mysql -u root -p
密码是第八步产生的密码
  • 1
  • 2

14设置密码

mysql>  set password=password("root");
Query OK, 0 rows affected, 1 warning (0.00 sec)
注意不要使用单引号,为什么?你自己试试就知道了
  • 1
  • 2
  • 3

15设置远程登录权限

mysql>  grant all privileges on *.* to'root' @'%' identified by 'root';
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.06 sec)

mysql> quit
Bye



参考教程2:


Linux<CentOS>服务器上安装MySQL,由于Centos自身的yum源中用mysql的分支Mariadb代替了MySQL,所以不得不选择rpm或tar.gz包的方式安装,但是为了以后在其他linux如Ubuntu中也能熟练安装MySQL,所以推荐使用Linux Generic版本的tar.gz包

         

1、卸载系统自带的Mariadb
[root@localhost~]# rpm -qa|grep mariadb //查询出已安装的mariadb
[root@localhost~]# rpm -e --nodeps 文件名 //一一卸载

3、删除etc目录下的my.cnf文件

[root@localhost~]# rm /etc/my.cnf

4、 执行以下命令来创建mysql用户组

[root@localhost~]# groupadd mysql

5、执行以下命令来创建一个用户名为mysql的用户并加入mysql用户组

[root@localhost ~]# useradd -g mysql mysql

  如有疑问请访问链接:useradd详解

6、解压安装包到指定目录

[root@localhost ~]# tar -zxvf mysql-advanced-5.5.07-linux-glibc2.5-x86_64.tar.gz -C /usr/local

7、将解压好的文件夹重命名为mysql

[root@localhost local]# mv mysql-advanced-5.5.07-linux-glibc2.5-x86_64 mysql

8、在etc下新建配置文件my.cnf,并在该文件内添加以下代码:

复制代码
复制代码
[mysql]
# 设置mysql客户端默认字符集
default-character-set=utf8 
socket=/var/lib/mysql/mysql.sock
[mysqld]
skip-name-resolve
#设置3306端口
port = 3306 
socket=/var/lib/mysql/mysql.sock
# 设置mysql的安装目录
basedir=/usr/local/mysql
# 设置mysql数据库的数据的存放目录
datadir=/usr/local/mysql/data
# 允许最大连接数
max_connections=200
# 服务端使用的字符集默认为8比特编码的latin1字符集
character-set-server=utf8
# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB 
lower_case_table_names=1
max_allowed_packet=16M
复制代码
复制代码

9、进入安装mysql软件目录

复制代码
[root@localhost ~]# cd /usr/local/mysql
[root@localhost mysql]# chown -R mysql:mysql ./  修改当前目录拥有者为mysql用户
[root@localhost mysql]# ./scripts/mysql_install_db --user=mysql 安装数据库

Data::Dumper   
解决方法 :安装autoconf库
  命令:yum install -y autoconf   //此包安装时会安装Data:Dumper模块

[root@localhost mysql]# chown -R mysql:mysql data 修改当前data目录拥有者为mysql用户
复制代码

   如有疑问请访问链接:chown详解

到此数据库安装完毕!


二、配置MySQL

1、授予my.cnf的最大权限。

[root@localhost ~]# chown 777 /etc/my.cnf

设置开机自启动服务控制脚本:

2、复制启动脚本到资源目录

[root@localhost mysql]# cp ./support-files/mysql.server /etc/rc.d/init.d/mysqld

3、增加mysqld服务控制脚本执行权限

[root@localhost mysql]# chmod +x /etc/rc.d/init.d/mysqld

4、将mysqld服务加入到系统服务

[root@localhost mysql]# chkconfig --add mysqld

5、检查mysqld服务是否已经生效

[root@localhost mysql]# chkconfig --list mysqld

命令输出类似下面的结果:

mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off
表明mysqld服务已经生效,在2、3、4、5运行级别随系统启动而自动启动,以后可以使用service命令控制mysql的启动和停止。

6、启动msql(停止|重启:service mysqld stop|restart)

[root@localhost mysql]# service mysqld start

一般情况下,此步骤会出现异常mysqld_safe Directory '/var/lib/mysql' for UNIX socket file don't exists.

7、将mysql的bin目录加入PATH环境变量,编辑/etc/profile文件

[root@localhost mysql]# vim /etc/profile

在文件最后添加如下信息:

 export PATH=$PATH:/usr/local/mysql/bin

执行下面的命令使所做的更改生效:

[root@localhost mysql]# source /etc/profile

8、以root账户登陆mysql,默认是没有密码

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

9、设置root账户密码 注意下面的you password改成你的要修改的密码

[root@localhost mysql]# update mysql.user set password=password('you password') where user='root';

10、设置远程主机登录(我用的是Navicat)注意下面的your username 和 your password改成你需要设置的用户和密码

[root@localhost mysql]# GRANT ALL PRIVILEGES ON *.* TO 'your username'@'%' IDENTIFIED BY 'your password' WITH GRANT OPTION;
Flush Privileges;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值