关于MySql安装和使用遇到的问题

今天,新拿了一台服务器,准备做测试服务器,一般来说,简单的使用yum安装mysql就可以用了。

但这台比较特殊。 

这台服务器上原来安装过mysql,但是启动不起来了,启动的时候报错,具体什么错误,没有具体去分析解决了,怕花费的时间太长,耽误环境部署。 于是决定重装mysql 

把原有的mysql卸载掉,把相关文件全部删掉,使用以下命令安装

yum -y  install mysql mysql-server mysql-devel

安装过程没有问题,但就是启动不起来,还是报错

于是换一下思路,用rpm包来安装

但这样就导致一个问题

总是会提示文件跟之前的有冲突,但实际上文件系统已经不存在那些文件了。 

为了不在这个问题上面纠结,我再换种方式,使用官网提供的二进制程序进行安装和配置

过程如下:

1.下载tar包 (以下mysql 均以5.7版本为例)

https://dev.mysql.com/downloads/mysql/  

选择linux -generic 版本  选择匹配位数的程序进行下载

2.解压并放置到/usr/local中

 tar xf  xxx.tar -C /usr/local 解出xxx.tar.gz的包

 tar -zxvf xxx.tar.gz 再解压一次,解压出mysql程序 

 mv xxx  mysql  将目录名重置为mysql

3.配置

  因为本机已经设置好了/etc/my.cnf  所以,这里不做my.cnf的配置说明

  复制程序

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

 添加mysqld服务 

 chkconfig --add mysqld  

 开启mysql服务 

chkconfig mysqld on 

4.设置权限,前提是已经设置好了mysql用户组,以及mysql用户

要对mysql文件夹和mysql的数据文件夹进行权限设置

   比如数据存放在   /home/data

   那么需要执行

   chown -R mysql:mysql /home/data

   chown -R mysql:mysql /usr/local/mysql/

   如果还有报错,请给一下目录授权

   日志目录   /var/log

    pid文件所在目录 ,如果提示找不到pid文件,先确认路径,路径无误的话,可以自建一个  主机名.pid文件,来代替。

5.执行安装脚本。 本人亲测,在以上这些事情都做完之后,执行service mysqld start命令时,出现以下错误

ERROR! The server quit without updating PID file (/usr/local/mysql/c.pid).

因为这个错误出现过很多次了,我直接去看日志文件内容,发现有这样一句话

mysqld: Table 'mysql.plugin' doesn't exist
 

我回过头看安装过程,发现少做了一件事情,执行mysql_install_db这个脚本,于是就去执行该脚本。

[root@c bin]# ./mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/home/data

2017-01-08 01:13:17 [WARNING] mysql_install_db is deprecated. Please consider switching to mysqld --initialize
2017-01-08 01:13:17 [ERROR]   The data directory '/home/data' already exist and is not empty.
看到这里,把/home/data清理以下,清除所有文件。再执行。

还是有警告,但是执行成功了。于是遵循这个警告,去执行

[root@c bin]# ./mysqld --initialize
2017-01-07T17:17:18.084687Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2017-01-07T17:17:18.088298Z 0 [ERROR] --initialize specified but the data directory has files in it. Aborting.
2017-01-07T17:17:18.088332Z 0 [ERROR] Aborting
这里的error是因为我当时配置文件中数据目录(datadir)写错了导致的,改为正常使用的目录,就没问题。

再改进 ,并执行

[root@c bin]# ./mysqld --initialize --explicit_defaults_for_timestamp

2017-01-07T17:25:23.122239Z 0 [Warning] InnoDB: New log files created, LSN=45790
2017-01-07T17:25:23.524486Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2017-01-07T17:25:23.738581Z 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: 45b0f5ce-d4fe-11e6-830f-f4ce46b3a1d8.
2017-01-07T17:25:23.779377Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2017-01-07T17:25:23.780359Z 1 [Note] A temporary password is generated for root@localhost: Ju<+)oguk1aA
2017-01-07T17:25:39.770568Z 1 [Warning] 'user' entry 'root@localhost' ignored in --skip-name-resolve mode.
2017-01-07T17:25:39.770631Z 1 [Warning] 'user' entry 'mysql.sys@localhost' ignored in --skip-name-resolve mode.
2017-01-07T17:25:39.770666Z 1 [Warning] 'db' entry 'sys mysql.sys@localhost' ignored in --skip-name-resolve mode.
2017-01-07T17:25:39.770689Z 1 [Warning] 'proxies_priv' entry '@ root@localhost' ignored in --skip-name-resolve mode.
2017-01-07T17:25:39.770755Z 1 [Warning] 'tables_priv' entry 'sys_config mysql.sys@localhost' ignored in --skip-name-resolve mode.
 

我们可以看到,出现了一些警告,并且同时随机生成了一个密码。在这个过程中,本来想加--inital-insecure这个参数试一下,结果发现不能识别这个参数。

[root@c bin]# ./mysqld --initialize --explicit_defaults_for_timestamp --inital-insecure
2017-01-07T17:24:22.109446Z 0 [Warning] InnoDB: New log files created, LSN=45790
2017-01-07T17:24:22.539774Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2017-01-07T17:24:22.738196Z 0 [ERROR] unknown option '--inital-insecure'
2017-01-07T17:24:22.738223Z 0 [ERROR] Aborting
 

这时候,我们试一下   service mysqld start 

[root@c bin]# service mysqld start
Starting MySQL. SUCCESS!

查看一下mysql进程

[root@c support-files]# ps aux|grep mysql
root      4995  0.0  0.0  11440  1532 pts/0    S    01:26   0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/home/data --pid-file=/home/data/c.pid
mysql     5188  0.0  0.7 1296856 189552 pts/0  Sl   01:26   0:01 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/home/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/var/log/mysqld.log --pid-file=/home/data/c.pid --socket=/var/lib/mysql/mysql.sock
root      5246  0.0  0.0 103324   900 pts/0    S+   02:06   0:00 grep mysql
 

终于成功搞定了。接下来试下登陆

[root@c support-files]# mysql -uroot -p
-bash: mysql: command not found
没有mysql命令? 怎么办?

linux系统默认会从/usr/bin   来查找命令,所以我们可以通过以下方法来解决

[root@c support-files]# ln -s /usr/local/mysql/bin/mysql /usr/bin

再接着登陆

[root@c support-files]# mysql -uroot -p
Enter password: 
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

又报错了。错误的原因在于路径/tmp/mysql.sock不存在

解决方法

找到mysql.sock路径,默认是:/var/lib/mysql/mysql.sock,建立关系

[root@c /]# ln -s /var/lib/mysql/mysql.sock /tmp/mysql.sock

接下再登陆

[root@c /]# mysql -uroot -p
Enter password: 
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
 

出现了一个常见错误

解决此错误,分为三步

1.停止服务

[root@c /]# service mysqld stop 

2.修改密码

[root@c /]# cd /usr/local/mysql/bin/
[root@c bin]# ./mysqld_safe --user=mysql --skip-grant-tables --skip-networking &
 

[root@c bin]# mysql -uroot
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.17 MySQL Community Server (GPL)

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.

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> update user set authentication_string=Password('admin123.0') where user='root';
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 1

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

3.重启服务再登陆

  关闭mysql相关的进程,然后执行

[root@c bin]# service mysqld start
Starting MySQL. SUCCESS! 
 

再次登陆

[root@c bin]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.17

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.

mysql> 

登陆成功!

至此,所有关于mysql安装和使用的问题,都解决了,接下来就可以愉快的使用mysql数据库做业务设计了。

希望我的笔录也能帮到您,感谢您的阅读。


 

 

 

 

 

 

 

 

 

 

  

 

 

 

转载于:https://my.oschina.net/u/2457585/blog/820772

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值