环境说明 CentOS7+php7+nginx1.12.2+mysql5.6
1.启动数据库并查看数据库状态
[root@bogon etc]# systemctl start mysql.service
[root@bogon etc]# ps aux | grep mysql
root 104146 0.1 0.0 115436 1728 ? S 11:41 0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/usr/local/mysql/data --pid-file=/usr/local/mysql/data/bogon.pid
mysql 104243 20.0 24.0 1296356 447960 ? Sl 11:41 0:01 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=bogon.err --pid-file=/usr/local/mysql/data/bogon.pid
root 104269 0.0 0.0 112724 988 pts/0 S+ 11:41 0:00 grep --color=auto mysql
2.链接数据库
[root@bogon etc]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.38 Source distribution
3.查看数据库用户
mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select host,user from mysql.user;
+-----------+------+
| host | user |
+-----------+------+
| 127.0.0.1 | root |
| ::1 | root |
| bogon | |
| bogon | root |
| localhost | |
| localhost | root |
+-----------+------+
4.创建用户可以远程访问
mysql> grant all privileges on *.* to 'root'@'%' identified by '123456' with grant option;
Query OK, 0 rows affected (0.00 sec)
允许任何ip地址(%表示允许任何ip地址)的电脑用root帐户和密码(123456)来访问这个mysql server。
注意root账户不一定要存在。
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
flush privileges 命令本质上的作用是将当前user和privilige表中的用户信息/权限设置从mysql库(MySQL数据库的内置库)中提取到内存里。MySQL用户数据和权限有修改后,希望在"不重启MySQL服务"的情况下直接生效,那么就需要执行这个命令。通常是在修改ROOT帐号的设置后,怕重启后无法再登录进来,那么直接flush之后就可以看权限设置是否生效。而不必冒太大风险。
5.查看数据库
mysql> select host,user from mysql.user;
+-----------+------+
| host | user |
+-----------+------+
| % | root |
| 127.0.0.1 | root |
| ::1 | root |
| bogon | |
| bogon | root |
| localhost | |
| localhost | root |
+-----------+------+
7 rows in set (0.00 sec)