avatar
童琦杰
向死而生
githubmusic
2018-02-03编辑

MySQL Installation

Ubuntu下安装

bash
sudo apt-get install mysql-server

登录MYSQL

根用户登录

bash
mysql -u root -p

指定用户名密码登录

bash
mysql --user=user_name --password=your_password

创建用户

bash
CREATE USER 'user_name'@'localhost' IDENTIFIED BY 'password';
CREATE USER 'user_name'@'%' IDENTIFIED BY 'password';

创建数据库、表

bash
CREATE SCHEMA `database_name`;
USE `database_name`;
CREATE TABLE `table_name` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `username` varchar(100) NOT NULL,
  `password` varchar(100) NOT NULL,
  `createTime` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `index_username` (`username`)
) ENGINE=InnoDB AUTO_INCREMENT=10001 DEFAULT CHARSET=utf8mb4;

权限设置

bash
GRANT ALL PRIVILEGES ON `database_name`.* TO 'username'@'localhost';

远程访问

设置静态IP

bash
vim /etc/network/interfaces

修改配置文件

/etc/network/interfaces
# The primary network interface
auto eth0
iface eth0 inet static
address 192.168.0.200    # local IP Address
gateway 192.168.0.1
netmask 255.255.255.0
dns-nameservers 8.8.8.8  # specify dns server

修改MYSQL监听IP

bash
vim /etc/mysql/my.cnf
/etc/mysql/my.cnf
[mysqld]
bind-address=192.168.0.200;    # local IP Address

重启服务

bash
sudo /etc/init.d/mysql restart

日志查看

bash
vim /var/log/mysql/error.log

字符集

sql
CREATE TABLE `database`.`table` (
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

ConnectionString: Server=;Database=;Uid=;Pwd=;Charset=utf8

Windows下二进制文件安装

解压二进制文件

初始化数据目录

bash
bin\mysqld --initialize --console

连接MYSQL

bash
mysql -u root -p
Enter password: (enter the random root password here)

重置根用户密码

bash
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password';

启动服务

bash
mysqld --console

停止服务

bash
mysqladmin -u root -p shutdown