mac上使用NodeJS与安装MySQL遇到的一些坑

1. Remove MySQL completely

转自https://gist.github.com/vitorbritto/0555879fe4414d18569d
1. Open the Terminal
2. Use mysqldump to backup your databases
3. Check for MySQL processes with: ps -ax | grep mysql
4. Stop and kill any MySQL processes
5. Analyze MySQL on HomeBrew:

```
brew remove mysql
brew cleanup
```
  1. Remove files:

    sudo rm /usr/local/mysql
    sudo rm -rf /usr/local/var/mysql
    sudo rm -rf /usr/local/mysql*
    sudo rm ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
    sudo rm -rf /Library/StartupItems/MySQLCOM
    sudo rm -rf /Library/PreferencePanes/My*
  2. Unload previous MySQL Auto-Login:

    launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
  3. Remove previous MySQL Configuration:

    subl /etc/hostconfig` 
    
    # Remove the line MYSQLCOM=-YES-
    
  4. Remove previous MySQL Preferences:

    rm -rf ~/Library/PreferencePanes/My*
    sudo rm -rf /Library/Receipts/mysql*
    sudo rm -rf /Library/Receipts/MySQL*
    sudo rm -rf /private/var/db/receipts/*mysql*
  5. Restart your computer just to ensure any MySQL processes are killed

  6. Try to run mysql, it shouldn’t work

2. brew install mysql

用homebrew安装,注意按照brew的指示进行初始化
1. brew services start mysql
2. mysql_secure_installation
3.安装选项如下:
Securing the MySQL server deployment.

Connecting to MySQL using a blank password.

VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?

Press y|Y for Yes, any other key for No: n
Please set the password for root here.

New password:

Re-enter new password:
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : n

… skipping.

Normally, root should only be allowed to connect from
‘localhost’. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : n

… skipping.
By default, MySQL comes with a database named ‘test’ that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.

Remove test database and access to it? (Press y|Y for Yes, any other key for No) : n

… skipping.
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.

All done!

3..遇到Error: ER_NOT_SUPPORTED_AUTH_MODE with auth_socket

登入mysql,输入命令
1. use mysql;
2. update user set authentication_string=password(”), plugin=’mysql_native_password’ where user=’root’;
如果提示错误,试试update user set plugin=’mysql_native_password’ where user=’root’;
之后应当如下图:
这里写图片描述
plugin为native_password
这个命令似乎也可以,忘了测试的结果了QAQ:
ALTER USER ‘root’@’localhost’ IDENTIFIED WITH mysql_native_password BY ‘password’

4.关于重置root密码的参考

https://stackoverflow.com/questions/33467337/reset-mysql-root-password-using-alter-user-statement-after-install-on-mac(这个应该就可以work了)
https://stackoverflow.com/questions/6474775/setting-the-mysql-root-user-password-on-os-x
https://stackoverflow.com/questions/33326065/unable-to-access-mysql-after-it-automatically-generated-a-temporary-password

5.问题:caching-sha2-password

参考https://blog.csdn.net/u010026255/article/details/80062153

6.一个简单的测试

connection.connect();

connection.query('insert into bs_user (id,name,password) values(2,\'test1\',\'123\')');


connection.end();

var mysql      = require('mysql');
var connection = mysql.createConnection({
    host     : 'localhost',
    user     : 'root',
    password : '',//你自己的密码
    database : 'bs_word_db'//你的数据库名字
});

connection.connect();

connection.query('insert into bs_user (id,name,password) values(2,\'test1\',\'123\')');//测试一条插入指令


connection.end();`

之后在mysql重新查看表格内容,应能看到改变。

猜你喜欢

转载自blog.csdn.net/ken_for_learning/article/details/80813892