wget https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.22-1.el7.x86_64.rpm-bundle.tar
解压:
# tar xvf mysql-5.7.22-1.el7.x86_64.rpm-bundle.tar
得到:
mysql-community-client-5.7.22-1.el7.x86_64.rpm
mysql-community-common-5.7.22-1.el7.x86_64.rpm
mysql-community-devel-5.7.22-1.el7.x86_64.rpm
mysql-community-embedded-5.7.22-1.el7.x86_64.rpm
mysql-community-embedded-compat-5.7.22-1.el7.x86_64.rpm
mysql-community-embedded-devel-5.7.22-1.el7.x86_64.rpm
mysql-community-libs-5.7.22-1.el7.x86_64.rpm
mysql-community-libs-compat-5.7.22-1.el7.x86_64.rpm
mysql-community-minimal-debuginfo-5.7.22-1.el7.x86_64.rpm
mysql-community-server-5.7.22-1.el7.x86_64.rpm
mysql-community-server-minimal-5.7.22-1.el7.x86_64.rpm
mysql-community-test-5.7.22-1.el7.x86_64.rpm
# rpm -qa|grep mysql
qt-mysql-4.8.5-8.el7.x86_64
# rpm -e --nodeps qt-mysql-4.8.5-8.el7.x86_64
# rpm -qa|grep mysql
(void)
# rpm -qa|grep mariadb
mariadb-libs-5.5.41-2.el7_0.x86_64
mariadb-5.5.41-2.el7_0.x86_64
mariadb-devel-5.5.41-2.el7_0.x86_64
mariadb-server-5.5.41-2.el7_0.x86_64
# rpm -e --nodeps mariadb-server-5.5.41-2.el7_0.x86_64
# rpm -e --nodeps mariadb-devel-5.5.41-2.el7_0.x86_64
# rpm -e --nodeps mariadb-5.5.41-2.el7_0.x86_64
# rpm -e --nodeps mariadb-libs-5.5.41-2.el7_0.x86_64
# rpm -qa|grep mariadb
(void)
# rpm -ivh mysql-community-common-5.7.22-1.el7.x86_64.rpm
# rpm -ivh mysql-community-libs-5.7.22-1.el7.x86_64.rpm
# rpm -ivh mysql-community-client-5.7.22-1.el7.x86_64.rpm
# rpm -ivh mysql-community-server-5.7.22-1.el7.x86_64.rpm
如果兼做开发环境,还需要安装:
# rpm -ivh mysql-community-devel-5.7.22-1.el7.x86_64.rpm
检查安装结果:
# rpm -qa|grep mysql
mysql-community-devel-5.7.22-1.el7.x86_64
mysql-community-libs-5.7.22-1.el7.x86_64
mysql-community-client-5.7.22-1.el7.x86_64
mysql-community-common-5.7.22-1.el7.x86_64
mysql-community-server-5.7.22-1.el7.x86_64
至此 mysql 服务端安装完毕。可以在需要访问数据库的机器上安装 mysql 客户端。
参考 4. 配置和启动 mysql。
删除 mysql 参考 2.1。
删除 mariadb 参考 2.2。
安装客户端:
# rpm -ivh mysql-community-common-5.7.22-1.el7.x86_64.rpm
# rpm -ivh mysql-community-libs-5.7.22-1.el7.x86_64.rpm
# rpm -ivh mysql-community-client-5.7.22-1.el7.x86_64.rpm
开发环境还需要安装:
# rpm -ivh mysql-community-devel-5.7.22-1.el7.x86_64.rpm
登录到 mysql 服务端计算机上:
启动|停止|状态查询
# systemctl start|stop|status mysqld
开机自启动
# systemctl enable mysqld
查看日志
# cat /var/log/mysqld.log
找到临时密码
# cat /var/log/mysqld.log |grep password|grep root@localhost
... [Note] A temporary password is generated for root@localhost: YaTrf6d?VyG0
修改 mysql root 密码和配置项:
# mysql_secure_installation
...
输入临时密码:YaTrf6d?VyG0 (本例)
设置新密码:YourNewPass (本例)
设置好之后,用新密码登录:
# mysql -u root -p'YourNewPass'
登录到 mysql 服务端计算机上。如果没有启动,需要先启动 mysql:
# systemctl start mysqld
查询数据文件目录:
# mysqladmin -u root -p'YourNewPass' variables | grep datadir
| datadir | /var/lib/mysql/
查询 mysql 使用的配置文件次序:
# mysql --help|grep 'my.cnf'
order of preference, my.cnf, $MYSQL_TCP_PORT,
/etc/my.cnf /etc/mysql/my.cnf /usr/etc/my.cnf ~/.my.cnf
按照上面的次序依次查找,找到第一个出现的 my.cnf,即 mysql 服务使用的配置文件。本例为:
/etc/my.cnf
修改配置文件:
# cp /etc/my.cnf /etc/my.cnf.old
# vi /etc/my.cnf
[client]
port=3306
socket=/home/workspace/mysql/mysql.sock
default-character-set=utf8
[mysqld]
character-set-server=utf8
#!--datadir=/var/lib/mysql
datadir=/home/workspace/mysql
#!--socket=/var/lib/mysql/mysql.sock
socket=/home/workspace/mysql/mysql.sock
symbolic-links=0
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
移动目录位置:
# systemctl stop mysqld
# mv /var/lib/mysql /home/workspace/
# systemctl start mysqld
查询数据文件目录:
# mysqladmin -u root -p'YourNewPass' variables | grep datadir
| datadir | /home/workspace/mysql/
删除临时配置文件 /etc/my.cnf.old。
统一使用utf8会避免很多错误。登录 mysql 查询字符集:
# mysql -u root -p'YourNewPass'
mysql> show variables like 'character_set_%';
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)
修改配置文件 /etc/my.cnf,如下:
[client]
default-character-set=utf8
...
[mysqld]
character-set-server=utf8
...
重启 mysql 服务,再登录查询:
# systemctl restart mysqld
# mysql -u root -p'YourNewPass'
mysql> show variables like 'character_set_%';
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)
mysql> show variables like 'collation_%';
+----------------------+-----------------+
| Variable_name | Value |
+----------------------+-----------------+
| collation_connection | utf8_general_ci |
| collation_database | utf8_general_ci |
| collation_server | utf8_general_ci |
+----------------------+-----------------+
3 rows in set (0.00 sec)
执行下面的命令:
mysql> delimiter |
SET GLOBAL character_set_client = utf8 |
SET GLOBAL character_set_connection = utf8 |
SET GLOBAL character_set_database = utf8 |
SET GLOBAL character_set_results = utf8 |
SET GLOBAL character_set_server = utf8 |
SET GLOBAL collation_connection = utf8_general_ci |
SET GLOBAL collation_database = utf8_general_ci |
SET GLOBAL collation_server = utf8_general_ci |
delimiter ;
这之后,所有的数据文件,程序都统一为utf8编码。
mysql 的 root 用户仅为管理员在特定的host上(如:localhost)上使用。
对于不同用途的数据库,应该创建相应的数据库和用户,并分配权限。
以 root 帐号登录 mysql:
创建一个名为 yii2basic 的数据库
mysql> create database yii2basic;
mysql> use yii2basic
mysql> delimiter |
CREATE TABLE `country` (
`code` CHAR(2) NOT NULL PRIMARY KEY,
`name` CHAR(52) NOT NULL,
`population` INT(11) NOT NULL DEFAULT '0'
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
INSERT INTO `country` VALUES ('AU','Australia',18886000) |
INSERT INTO `country` VALUES ('BR','Brazil',170115000) |
INSERT INTO `country` VALUES ('CA','Canada',1147000) |
INSERT INTO `country` VALUES ('CN','China',1277558000) |
INSERT INTO `country` VALUES ('DE','Germany',82164700) |
INSERT INTO `country` VALUES ('FR','France',59225700) |
INSERT INTO `country` VALUES ('GB','United Kingdom',59623400) |
INSERT INTO `country` VALUES ('IN','India',1013662000) |
INSERT INTO `country` VALUES ('RU','Russia',146934000) |
INSERT INTO `country` VALUES ('US','United States',278357000) |
delimiter ;
mysql> show tables;
下面创建一个用户 yii@localhost,即该用户只能从本机(localhost)访问 yii2basic:
mysql> create user 'yii'@'localhost' identified by 'Abc123456!';
mysql> revoke all privileges, grant option from 'yii'@'localhost';
mysql> grant all privileges on yii2basic.* to 'yii'@'localhost';
mysql> flush privileges;
mysql> quit
# mysql -u yii -p'Abc123456!'
mysql> use sys;
ERROR 1044 (42000): Access denied for user 'yii'@'localhost' to database 'sys'
上面这个错误出现才表示正确!
mysql> use yii2basic;
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> show tables;
+---------------------+
| Tables_in_yii2basic |
+---------------------+
| country |
+---------------------+
1 row in set (0.00 sec)
mysql> insert into country(code,name,population) values('JP','Japan',126910000);
(结束)