xampp 中 mysql的相关配置

最近开始接触PHP,而一般搭建PHP环境使用的都是xampp 这个集成环境,由于之前我的系统中已经安装了mysql服务,所以在启动mysql的时候出现一些列错误,我通过查询各种资料解决了这个问题,现在记录一下,方便日后遇到同样的问题时能够快速解决,也为遇到同样问题的朋友提供一种解决的思路。

启动

刚开始时我在点击启动mysql的时候发现它一直卡在尝试启动mysql这个位置,xampp提示内容如下:

Attempting to start MySQL service...

它启动不成功但是也不提示出错,而且查询日志发现没有错误的日志,这个时候我想到应该是我本地之前安装了mysql,导致失败。而且我还将mysql安装成为了服务,后来查询相关资料,有网友说需要将mysql服务的地址改为xampp下mysql所在地址,具体怎么改我就不写了,一般都可以找到,但是我想说的是,这个方式好像在我这边不起作用。那么就干脆一点直接删除服务就好了。

sc delete mysql

上述命令直接删除mysql这个服务。然后重启xampp,再次启动mysql,它终于报错了。只要报错就好说了,现在来查询日志,发现日志如下:

2018-10-13 22:52:19 37d0 InnoDB: Warning: Using innodb_additional_mem_pool_size is DEPRECATED. This option may be removed in future releases, together with the option innodb_use_sys_malloc and with the InnoDB's internal memory allocator.
2018-10-13 22:52:19 14288 [Note] InnoDB: innodb_empty_free_list_algorithm has been changed to legacy because of small buffer pool size. In order to use backoff, increase buffer pool at least up to 20MB.

2018-10-13 22:52:19 14288 [Note] InnoDB: Using mutexes to ref count buffer pool pages
2018-10-13 22:52:19 14288 [Note] InnoDB: The InnoDB memory heap is disabled
2018-10-13 22:52:19 14288 [Note] InnoDB: Mutexes and rw_locks use Windows interlocked functions
2018-10-13 22:52:19 14288 [Note] InnoDB: _mm_lfence() and _mm_sfence() are used for memory barrier
2018-10-13 22:52:19 14288 [Note] InnoDB: Compressed tables use zlib 1.2.3
2018-10-13 22:52:19 14288 [Note] InnoDB: Using generic crc32 instructions
2018-10-13 22:52:19 14288 [Note] InnoDB: Initializing buffer pool, size = 16.0M
2018-10-13 22:52:19 14288 [Note] InnoDB: Completed initialization of buffer pool
2018-10-13 22:52:19 14288 [Note] InnoDB: Highest supported file format is Barracuda.
2018-10-13 22:52:19 14288 [Warning] InnoDB: Resizing redo log from 2*3072 to 2*320 pages, LSN=1600607
2018-10-13 22:52:19 14288 [Warning] InnoDB: Starting to delete and rewrite log files.
2018-10-13 22:52:19 14288 [Note] InnoDB: Setting log file C:\xampp\mysql\data\ib_logfile101 size to 5 MB
2018-10-13 22:52:19 14288 [Note] InnoDB: Setting log file C:\xampp\mysql\data\ib_logfile1 size to 5 MB
2018-10-13 22:52:19 14288 [Note] InnoDB: Renaming log file C:\xampp\mysql\data\ib_logfile101 to C:\xampp\mysql\data\ib_logfile0
2018-10-13 22:52:19 14288 [Warning] InnoDB: New log files created, LSN=1601036
2018-10-13 22:52:19 14288 [Note] InnoDB: 128 rollback segment(s) are active.
2018-10-13 22:52:19 14288 [Note] InnoDB: Waiting for purge to start
2018-10-13 22:52:19 14288 [Note] InnoDB:  Percona XtraDB (http://www.percona.com) 5.6.39-83.1 started; log sequence number 1600607
2018-10-13 22:52:20 3508 [Note] InnoDB: Dumping buffer pool(s) not yet started
2018-10-13 22:52:20 14288 [Note] Plugin 'FEEDBACK' is disabled.
2018-10-13 22:52:20 14288 [ERROR] Could not open mysql.plugin table. Some plugins may be not loaded
2018-10-13 22:52:20 14288 [ERROR] Can't open and lock privilege tables: Table 'mysql.servers' doesn't exist
2018-10-13 22:52:20 14288 [Note] Server socket created on IP: '::'.
2018-10-13 22:52:20 14288 [ERROR] Fatal error: Can't open and lock privilege tables: Table 'mysql.user' doesn't exist
2018-10-13 22:55:18 3024 InnoDB: Warning: Using innodb_additional_mem_pool_size is DEPRECATED. This option may be removed in future releases, together with the option innodb_use_sys_malloc and with the InnoDB's internal memory allocator.
2018-10-13 22:55:18 12324 [Note] InnoDB: innodb_empty_free_list_algorithm has been changed to legacy because of small buffer pool size. In order to use backoff, increase buffer pool at least up to 20MB.

找到其中的ERROR项,发现它提示mysql.user这个表不存在,这个表保存的是mysql的账号信息,如果没有这个,它无法知道哪些是合法用户,合法用户又有哪些权限,因此这里就需要创建这个表。通过查询资料发现这是由于未进行mysql数据初始化的缘故,这个错误经常见于通过源码包在编译安装的时候。这个时候需要使用命令 mysql_install_db 来初始化数据库表

mysql_install_db --user=mysql -d C:\xampp\mysql\data\

-d 后面跟上mysql表数据所在路径

执行之后发现程序又报错了,这次提示mysql的版本不对

Can't find messagefile "D:\mysql-8.0.11-winx64\share\errmsg.sys". Probably from another version of MariaDB

这个时候就很奇怪了,我启动的是xampp中的mysql,为何它给我定位的是之前安装的MySQL所在路径呢?出现这种现象肯定是系统中的相关配置的路径不对,之前已经删掉了mysql服务,那么应该不可能会是服务配置导致的,剩下的应该就是环境变量了,通过一个个的查看环境变量,终于发现了 MYSQL_HOME这个变量给的是 D:\mysql-8.0.11-winx64 这个路径,我们将这个环境变量的值修改为xampp中mysql的路径然后再执行命令初始化mysql表数据,这个时候成功了。

完成了这些操作,我这边就可以通过xampp面板启动mysql了。

数据库配置

刚开始时使用root账户登录是不需要密码的,这样是很危险的操作,容易发生数据泄露,为了安全起见,首先给root账户输入一个复杂的密码

mysqladmin -uroot -p password

回车之后它会让你输入新的密码,如果是修改密码可以使用下面的命令

mysqladmin -uroot -p"test" password

其中test为原始密码
在回车之后它会让你输入新的密码
修改与配置密码

我们为root设置了一个相对复杂的密码,但是与Linux系统相似,为了安全一般不能随便给出root账户,这个时候就需要一个非root账户并为它设置相关权限,我们可以在进入mysql后,使用grant 命令来创建账户以及分配权限

grant all privileges on *.* to masimaro@localhost identified by "masimarotest";
flush privileges;

它的语法格式为: grant 权限 on 数据库.表 to 用户名@主机 identified by "密码"

  • 权限,all privileges 表示所有权限,如果不想分配所有权限,可以考虑使用 select,insert,update,delete,create,drop,index,alter,grant,references,reload,shutdown,process,file 权限中的任意一个或者多个。
  • 数据库,表:我们可以指定具体的用户对具体的数据库表有何种权限
  • 主机:主机可以是localhost,%(任意主机),或者具体的主机名、ip等等,表示这个账户只能通过对应的主机来登录

分配完成之后通过 flush privileges; 语句来保存我们分配的账户和权限

为了方便操作,还可以对phpmyadmin进行配置,以便能够使用phpmyadmin来连接并操作mysql数据库。

可以在phpmyadmin目录中找到 config.inc.php 文件,找到这么几行

$cfg['Servers'][$i]['user'] = ''; //连接数据库的用户
$cfg['Servers'][$i]['password'] = ''; //连接数据库的用户密码

$cfg['Servers'][$i]['host'] = '127.0.0.1'; //数据库所在主机

$cfg['Servers'][$i]['controluser'] = 'root'; //phpmyadmin 所使用的配置账户
$cfg['Servers'][$i]['controlpass'] = ''; //配置账户的密码

根据具体情况配置这些信息之后,就可以直接连上PHPmyadmin了,然后根据它的提示来初始化相关数据库和表即可


posted @ 2018-10-14 11:48  masimaro  阅读(2385)  评论(0编辑  收藏  举报