今天,新拿了一台服务器,准备作测试服务器,通常来讲,简单的使用yum安装mysql就能够用了。mysql
但这台比较特殊。 linux
这台服务器上原来安装过mysql,可是启动不起来了,启动的时候报错,具体什么错误,没有具体去分析解决了,怕花费的时间太长,耽误环境部署。 因而决定重装mysql sql
把原有的mysql卸载掉,把相关文件所有删掉,使用如下命令安装数据库
yum -y install mysql mysql-server mysql-develbash
安装过程没有问题,但就是启动不起来,仍是报错服务器
因而换一下思路,用rpm包来安装socket
但这样就致使一个问题ide
老是会提示文件跟以前的有冲突,但实际上文件系统已经不存在那些文件了。 测试
为了避免在这个问题上面纠结,我再换种方式,使用官网提供的二进制程序进行安装和配置ui
过程以下:
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数据库作业务设计了。
但愿个人笔录也能帮到您,感谢您的阅读。