15

我已经设法让我的安装搞砸了,因为我是一个新手,搞砸是我擅长的。

1)任何人都可以为这些东西参考一套可靠的设置说明吗?

2)我可以通过macports卸载并从头开始(密码和所有)吗?我试过了,卸载后一堆mysql目录卡住了,WTF!?

3) mysqld.sock 需要驻留在哪里以及我需要应用哪些文件设置才能使其与 php5 和 apache2 一起使用?它是在 /tmp 还是 /opt/local/var/run/mysql5/ 中?我需要配置/opt/local/etc/php5/php.ini 吗?选择/本地/var/db/php5/mysql.ini?/opt/local/etc/mysql5/my.cnf?

4) 我已经运行 > sudo -u mysql mysql_install_db5。如果我再次运行它会搞砸我吗?我运行了 mysqladmin -u root password 'mypw' 并得到一个错误,说 mysql 无法通过套接字连接。那么这是否意味着我的密码现在已设置?有没有办法告诉我?

5) 密码语句的语法因博客而异。我的密码是“mypw”还是 mypw(不带引号)?(我的密码实际上不是 mypw)

6)启动和停止mysql的适当命令是什么?我不希望它在我的机器启动时启动。


谢谢 Jergason,这非常有帮助。

我真的很想与 mysql 的 macports 安装保持一致,因为它限制了我可以对我的机器造成的损害。

所以现在悬而未决的问题是......

1) macports mysql 安装的默认套接字位置是什么?

2)启动和停止mysql的适当命令是什么?我不希望它在我的机器启动时启动。一些博客说使用launchctl -load 其他人说sudo /path/mysql_safe5。如果您没有设置密码(因为我不打算这样做),您是否需要使用 -user -p 参数?

3) 使 macports mysql 工作所需的目录所有权和权限是什么。macports 似乎没有设置任何这些。


书呆子

检查 /tmp 以获取 MacPorts mysql 套接字

没有帮助。如前所述,我了解 /tmp 是套接字的默认位置。我的问题是,当我尝试启动 mysql 时,套接字没有在那里实现,也没有出现在 /opt/local/var/run/mysql5/ 中。这告诉我我在启动 mysql 时遇到问题,这意味着设置有问题。可能是什么,我不知道。恐怕我生来就没有对这些事情有先验知识。

- 在 Snow Leopard 下使用 macports 安装 mysql5 的套接字应该在哪个特定目录中?

如果您正在使用 5 个不同站点的有关如何运行 mysql 的提示,并且您很惊讶自己迷路了……好吧,我将尝试一次只关注一个站点并退出之前的所有更改去下一个。

没有帮助。我完全按照你的建议开始了。你认为我做了什么,混合和匹配来自 5 个博客的说明?这就是我询问干净卸载的原因。我想知道以前安装的状态更改是否会影响未来的“干净”安装。

-What SPECIFIC blog描述了在Snow Leopard下使用macports确认mysql5的正确安装过程。

我认为您在使用 MacPorts mysql 时遇到的唯一问题(您遇到什么问题??)是它有一个您不期望的套接字,并且它使用的是默认配置。

没有帮助。我也是,看我的原帖。我认为问题在于 mysql 的启动方式(或未启动)。

-您对如何纠正问题有任何具体建议吗?

这应该在 MacPorts 完成安装后解释。

没有帮助。不是,或者如果是的话我没看到。也许是因为我使用的是 Porticus。

-你可以再详细一点吗?我应该在什么时候看到什么信息?

请参阅 ${prefix}/share/doc/mysql5/ 获取想法和/或联系开发人员列表。也可以考虑联系端口的维护者;自从他们管理它以来,他们可能最了解它。

没有帮助。RTM,谷歌是你的朋友,等等。是的,先去了那里。虽然我承认我可能忽略了一些事情,因为当我确定我的问题是一个简单的问题时,当我有其他工作要做时,我不倾向于花几天时间阅读一些写得不好的文档。

  • 如果你想给我一份我可能忽略的具体文件,那会很有帮助。

我希望像 Jergason 提供的那样提供一些简单、集中的帮助。我(和其他阅读本文的人)很欣赏 Jergason 愿意传达他的知识,以加快人们对各种技术的学习过程。对他来说,这些知识是基本的,但对新手来说,这是一个巨大的节省时间。智能的真正标志是理解技术知识不是先验的,即使是基本的帮助也有很大的帮助。再次感谢J-man。

4

7 回答 7

13

I've just installed mysql5 myself, and since this is always a pain I've noted the process that I followed. This was done on Leopard, but I imagine the process is the same on Snow Leopard and Lion. It doesn't answer all the original questions, but at least it's something of a guide.

Installing Mysql

First, install mysql5 server with:

sudo port install mysql5-server

Just installing mysql5 doesn't install the server.

Pay attention to the console output, it includes instructions for setting up macports. You might want to copy and paste it to a text file. The following is based on it.

Instead of mysql5-server, you could use a port such as mysql55-server, mysql56-server, mariadb-server or percona-server to get a more recent version of mysql, or a fork. If you do, pay attention to the console output, as the following instructions are based on mysql5-server and will need to be adjusted to use the correct executables and paths.

If this is a new install, set up the database:

sudo -u _mysql mysql_install_db5

That outputs some generic instructions, which I don't think are entirely appropriate for macports. In my opinion the best way to load mysql5 as a daemon is to use macport's method:

sudo port load mysql5-server

As well as starting mysql5, this permanently loads it - it will run on boot up. To stop this later:

sudo port unload mysql5-server

If you don't want to run it as a daemon, you can run it at the command line:

sudo /opt/local/lib/mysql5/bin/mysqld_safe

Check that it's running by logging in at the command line:

mysql5 -u root -p

By default, the password is empty, so just press enter when prompted. To set a root password:

/opt/local/lib/mysql5/bin/mysqladmin -u root password 'correct horse battery staple'

Instructions for setting up both macports php and the native php installation follow.

Setup Macports PHP

Assuming you've already got macports php installed and running. You need to install php5-mysql (or something like php54-mysql depending on which version of php you're using):

sudo port install php5-mysql

This installs the mysql, mysqli and pdo drivers.

Now look in your /opt/local/etc/php5 directory, if you don't already have a php.ini configuration file copy either php.ini-development or php.ini-production to php.ini. Now edit php.ini and search for the appropriate lines to add:

pdo_mysql.default_socket=/opt/local/var/run/mysql5/mysqld.sock

and:

mysql.default_socket = /opt/local/var/run/mysql5/mysqld.sock

and:

mysqli.default_socket = /opt/local/var/run/mysql5/mysqld.sock

If you don't want to configure these, you can set them explicitly in your php script when you connect.

If you're having trouble connecting you might want to look at the other nearby settings, and compare with php.ini-development and php.ini-production to see what's been changed.

Then use the script below, or something similar to test that you can connect with php.

Setup System PHP

OS X's php comes with mysql and mysqli support built in (but not pdo), so all you need to do is set the macport unix socket. The default location is /opt/local/var/run/mysql5/mysqld.sock. Find the correct place in /etc/php.ini (if you don't have it already, copy it from /etc/php.ini.default) to add:

mysql.default_socket = /opt/local/var/run/mysql5/mysqld.sock

and:

mysqli.default_socket = /opt/local/var/run/mysql5/mysqld.sock

If you don't want to configure these, you can set them explicitly in your php script when you connect.

Test Script

Here's a php script to check that it can connect. Obviously, you won't normally use the root account in your php scripts so you might want to first create another mysql account for testing the connection. The PDO connection won't work for native php since that doesn't have PDO drivers.

<?php

$username = 'root';
$password = 'correct horse battery staple';

/* Try mysql: */

$connection = mysql_connect('localhost', $username, $password);
if ($connection === FALSE) {
    echo "Error connecting using mysql.\n\n";
    echo "Error ".mysql_errno().": ".mysql_error()."\n\n";

}
else {
    echo "Connected using mysql.\n\n";
    mysql_close($connection);
}

/* Try mysqli: */

$connection = mysqli_connect('localhost', $username, $password);
if ($connection->connect_error) {
    echo "Error connecting using mysqli:\n\n";
    echo "Error ".$connection->connect_errno.": ".$connection->connect_error."\n\n";
}
else {
    echo "Connected using mysqli.\n\n";
    $connection->close();
}

/* Try pdo:
 * Won't work for the version of php supplied with OS X. */

try {
    $pdo = new PDO('mysql:host=localhost', $username, $password);
    echo "Connected using PDO.\n\n";
    $pdo = null;
}
catch(PDOException $e) {
    echo "Error connecting using PDO:\n\n";
    echo "Error ".$e->getCode().": ".$e->getMessage()."\n\n";
}
于 2011-08-21T22:19:28.743 回答
4

我自己也遇到了同样的问题。为了让我的 MySQL 正常工作,我只需要这样做

sudo chmod -Rf 777 /opt/local/var/db/mysql5

尝试 sudo -u _mysql mysql_db5 失败,但我可以这样做

mysqladmin -u root  password the_new_password

没有任何问题。

如果您不希望 MySQL 在您打开机器时自动启动,只需执行以下操作:

sudo launchctl unload -w /Library/LaunchDaemons/org.macports.mysql5.plist

然后启动/停止 MySQL:

开始:

sudo /opt/local/etc/LaunchDaemons/org.macports.mysql5/mysql5.wrapper start

停止:

sudo /opt/local/etc/LaunchDaemons/org.macports.mysql5/mysql5.wrapper stop

希望这可以帮助。

于 2010-01-12T20:34:32.820 回答
2

1) Hivelogic有一个很好的从源代码为 Snow Leopard 编译 mysql 的演练。这实际上并不难,如果你自己做,你就会知道它在哪里。

2)你应该能够运行sudo port uninstall mysql5. 它可能会因为依赖关系而对你大喊大叫。如果是这样,您将必须先卸载这些。卸载 MySQL 应该删除所有配置设置,包括密码。

3) mysqld.sock 需要驻留在哪里以及我需要应用哪些文件设置才能使其与 php5 和 apache2 一起使用?mysqld.sock 的位置不如它的路径重要。在 php.ini 文件中设置 mysqld.sock 的路径。如果您在 /opt/local/bin 中有一个 php.ini 文件,那么您安装了 macports 版本的 php。您可以使用那个,也可以使用默认的 Apple 版本。无论哪种方式,您都需要配置 php.ini 文件,以便告诉您的 php 安装在哪里使用它。你的 php.ini 文件中应该有一行写着

mysql.default_socket = <whatever>

如果它不存在,您可以添加它。您应该将此行设置为等于 mysqld.sock 的位置。我的看起来像这样:

mysql.default_socket = /Applications/MAMP/tmp/mysql/mysql.sock

您的 Apache 配置

4) 我已经运行 > sudo -u mysql mysql_install_db5。如果我再次运行它会搞砸我吗?我运行了 mysqladmin -u root password 'mypw' 并得到一个错误,说 mysql 无法通过套接字连接。那么这是否意味着我的密码现在已设置?有没有办法告诉我?如果您无法连接到套接字,则说明您的密码未设置。

5) 密码语句的语法因博客而异。我的密码是“mypw”还是 mypw(不带引号)?(我的密码实际上不是 mypw)命令行中的密码不需要引号。

于 2009-10-20T01:24:01.197 回答
2

检查 /tmp 以获取 MacPorts mysql 套接字。

如果您正在使用 5 个不同站点的有关如何运行 mysql 的提示,并且您很惊讶自己迷路了……好吧,我将尝试一次只关注一个站点并退出之前的所有更改去下一个。

我认为您在使用 MacPorts mysql 时遇到的唯一问题(您遇到什么问题??)是它有一个您不期望的套接字,并且它使用的是默认配置。这应该在 MacPorts 完成安装后解释。请查看${prefix}/share/doc/mysql5/想法和/或联系开发人员列表

也可以考虑联系端口的维护者;自从他们管理它以来,他们可能最了解它。

于 2009-10-23T04:31:02.250 回答
1

命令行上使用的 php.ini 可能与 apache 使用的不同 您可以使用 phpinfo.php 进行检查

<?php
    phpinfo();
?

通过做一个

php phpinfo.php | grep php.ini

如果它显示

Configuration File (php.ini) Path => /opt/local/etc/php5

并且 Daniel James 回答的测试脚本(另存为 testconnect.php)也失败了

那么你可能想通过创建一个符号链接来修复:

/opt/local/etc/php5>sudo ln -s /etc/php.ini php.ini

之后测试连接应该工作

php testconnect.php 
Connected using mysql.
Connected using mysqli.
Connected using PDO.
于 2013-07-28T15:08:53.217 回答
0

通过 macports 安装 php5 时,该软件包不会自动附带 mysql 扩展名,因此它无法通过 php 连接到 mysql(但您可能可以通过命令行)。

我们先检查一下你的mysql是否运行正常:

mysqladmin5 -uroot -pYOURROOTPASSWORD ping

如果它还活着,您可以通过以下方式验证所有变量:

mysqladmin5 -uroot -pYOURROOTPASSWORD variables

您应该看到您的“套接字”位置定义为

/opt/local/var/run/mysql5/mysqld.sock

使用 macports 在 2 分钟内安装缺少的组件……方法如下:

sudo port install php5-mysql

要将 mysqlnd 与本地 MySQL 服务器一起使用,请编辑/opt/local/etc/php5/php.ini并将 mysql.default_socket、mysqli.default_socket 和 pdo_mysql.default_socket 设置为/opt/local/var/run/mysql5/mysqld.sock

然后重新启动您的 Apache 类型:

sudo /opt/local/apache2/bin/apachectl -k restart

现在一切都应该正常运行。希望有帮助。

于 2012-03-01T03:02:42.347 回答
0

我解决了这个问题。尽管 Apache 没有运行,但出于某种原因,Apache 阻止了 MySQL 的运行(因此阻止了创建套接字)。(是的,很奇怪吧?)在我运行安装之前,我发出了停止 apache 的命令,然后安装就顺利进行了。即使是现在,在我启动 MySQL 之前,我首先必须告诉 Apache(它没有运行)停止。

只是为了记录,我知道它没有运行,因为我得到“httpd(无 pid 文件)没有运行”我没有让它在启动时自动运行或任何类似的愚蠢废话。

我会尝试写一个演练,这样其他人就不必通过这个 BS。

于 2010-01-20T18:20:23.223 回答