使用Homebrew安装MySQL
brew install mysql
配置自启动
mkdir -p ~/Library/LaunchAgents
ln -sfv /usr/local/opt/mysql/*.plist ~/Library/LaunchAgents
find /usr/local/Cellar/mysql/ -name "homebrew.mxcl.mysql.plist" -exec cp {} ~/Library/LaunchAgents/ \;
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
修改mysql密码
先启动mysql服务
mysql.server start
继续执行mysql_secure_installation
mysql_secure_installation
查看现有的密码策略
mysql> SHOW VARIABLES LIKE 'validate_password%';
参数解释:
validate_password_dictionary_file 指定密码验证的文件路径;
validate_password_length 密码最小长度
validate_password_mixed_case_count 密码至少要包含的小写字母个数和大写字母个数;
validate_password_number_count 密码至少要包含的数字个数
validate_password_policy 密码强度检查等级,对应等级为:0/LOW、1/MEDIUM、2/STRONG,默认为1。
注意:
0/LOW:只检查长度;
1/MEDIUM:检查长度、数字、大小写、特殊字符;
2/STRONG:检查长度、数字、大小写、特殊字符字典文件。
validate_password_special_char_count密码至少要包含的特殊字符数
修改mysql参数配置
mysql> set global validate_password_policy=0;
Query OK, 0 rows affected (0.05 sec)
mysql>
mysql>
mysql> set global validate_password_mixed_case_count=0;
Query OK, 0 rows affected (0.00 sec)
mysql> set global validate_password_number_count=3;
Query OK, 0 rows affected (0.00 sec)
mysql> set global validate_password_special_char_count=0;
Query OK, 0 rows affected (0.00 sec)
mysql> set global validate_password_length=3;
Query OK, 0 rows affected (0.00 sec)
修改简单密码:
mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('123');
Query OK, 0 rows affected, 1 warning (0.00 sec)
更新MySQL
brew upgrade mysql
关闭MySQL服务
mysql.server stop
重启MySQL服务
mysql.server restart
查看版本
mysql --version
屏幕快照 2018-01-29 16.41.49.png
进入数据库,数据库默认无密码直接回车两次就好
mysql -u root -p
修改mysql密码
苹果->系统偏好设置->最下边点mysql 在弹出页面中 关闭mysql服务(点击stop mysql server)
进入终端输入:cd /usr/local/mysql/bin/
回车后 登录管理员权限 sudo su
回车后输入以下命令来禁止mysql验证功能 ./mysqld_safe --skip-grant-tables &
回车后mysql会自动重启(偏好设置中mysql的状态会变成running)
输入命令 ./mysql
回车后,输入命令 FLUSH PRIVILEGES
回车后,输入命令 SET PASSWORD FOR 'root'@'localhost' = PASSWORD('你的新密码')
至此,密码修改完成,可以成功登陆
卸载
brew remove mysql
brew cleanup
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*
launchctl unload -w~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
edit /etc/hostconfig and remove the line MYSQLCOM=-YES-
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*
/usr/local/Cellar 里的mysql文件
/usr/local/var 里的mysql文件
/tmp 里的mysql.sock, mysql.sock.lock, my.cnf文件
pid文件和err文件都在/usr/local/var/mysql里确保删除了
brew安装的安装包存储在/usr/local/Library/Cache/Homebrew也可以一并删除
执行brew cleanup
我们打开Navicat添加一个数据库连接
77AA30F1-566B-4629-B44E-513FFE696ADB.png
配置MySQLProvider
在运行项目前,和PostgreSQLProvider需要CPostgreSQL库一样,MySQLProvider也需要为系统安装CMySQL库,这样项目才能够被成功编译:
MacOS:
brew install vapor/tap/cmysql
Ubuntu:
sudo apt-get install cmysql
添加MySQL库的依赖
import PackageDescription
let package = Package(
name: "Ranking_Service", // Perfect的工程名 可自行修改
targets: [],
dependencies: [
.Package(url: "https://github.com/PerfectlySoft/Perfect-HTTPServer.git", majorVersion: 2),
//MySql数据库依赖包
.Package(url:"https://github.com/PerfectlySoft/Perfect-MySQL.git", majorVersion: 2, minor: 0)
]
)
重新运行命令
swift package generate-xcodeproj
访问MySQL
我们在main.swift文件添加如下代码
添加 import MySQL
let testHost = "127.0.0.1"
let testUser = "root"
// PLEASE change to whatever your actual password is before running these tests
let testPassword = "123456"
let testSchema = "Test"
请求MySQL,下面官方请求MySQL的例子
let dataMysql = MySQL()
public func useMysql(_ request: HTTPRequest, response: HTTPResponse) {
// need to make sure something is available.
guard dataMysql.connect(host: testHost, user: testUser, password: testPassword ) else {
Log.info(message: "Failure connecting to data server \(testHost)")
return
}
defer {
dataMysql.close() // defer ensures we close our db connection at the end of this request
}
//set database to be used, this example assumes presence of a users table and run a raw query, return failure message on a error
guard dataMysql.selectDatabase(named: testSchema) && dataMysql.query(statement: "select * from Person limit 1") else {
Log.info(message: "Failure: \(dataMysql.errorCode()) \(dataMysql.errorMessage())")
return
}
//store complete result set
let results = dataMysql.storeResults()
//setup an array to store results
var resultArray = [[String?]]()
while let row = results?.next() {
resultArray.append(row)
}
//return array to http response
response.appendBody(string: "
Mysql Test\(resultArray.debugDescription)")response.completed()
}
添加路演
var routes = Routes()
routes.add(method: .get, uri: "/t", handler: {
request, response in
useMysql(request, response: response)
}
)
运行工程,打开MySQL,浏览器中打开,访问http://localhost:8181/t
就看到了:[[Optional("id")]]