安装篇
mysql 在centOS7 下安装,要用编译安装
- 下载代码
wget https://cdn.mysql.com//Downloads/MySQL-8.0/mysql-boost-8.0.11.tar.gz
- 创建mysql用户组和用户
groupadd mysql
useradd -r -g mysql -s /bin/false mysql
- 创建mysql的工作目录
# 创建安装目录
mkdir -p /usr/local/mysql
# 创建数据目录
mkdir -p /usr/local/src/data/mysql
# 创建socket目录
mkdir -p /var/lib/mysql
# 创建safe模式日志目录
mkdir -p /var/log/mariadb
# 创建safe模式pid目录
mkdir -p /var/run/mariadb/
# 创建空的safe模式日志
touch /var/log/mariadb/mariadb.log
- 解压MySQL安装包
tar -zxf mysql-boost-8.0.13.tar.gz
- cmake创建安装文件
cd mysql-8.0.13/
cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/usr/local/src/data/mysql -DSYSCONFDIR=/etc -DMYSQL_TCP_PORT=3306 -DWITH_BOOST=/usr/local/src/mysql8/mysql-8.0.13/boost -DDOWNLOAD_BOOST=1
- make安装
make && make install
- 修改mysql工作目录的权限
chown -R mysql:msyql /var/lib/mysql
chown -R mysql:mysql /usr/local/src/data/mysql
chown -R mysql:mysql /var/lib/mysql
chown -R mysql:mysql /var/log/mariadb
chown -R mysql:mysql /var/run/mariadb/
chown -R mysql:mysql /var/log/mariadb/mariadb.log
chmod 755 /var/lib/mysql
chmod 755 /usr/local/src/data/mysql
chmod 755 /var/lib/mysql
chmod 755 /var/log/mariadb
chmod 755 /var/run/mariadb/
chmod a+w /var/log/mariadb/mariadb.log
- 修改mysql配置
vi /etc/my.cnf
# 改成下面这样,添加没有的,修改已有的,不要删除其他的
[client]
socket = /var/lib/mysql/mysql.sock
[mysqld]
server-id=1
basedir=/usr/local/mysql
datadir=/usr/local/src/data/mysql
- 将mysql/bin软连接到/usr/bin,方便实用mysql相关命令
ln -s /usr/local/mysql/bin/* /usr/bin/
- 初始化数据库
mysqld --initialize --user=mysql --datadir=/usr/local/src/data/mysql/
# (注意这里会有一个root账号的初始密码,记下来,一会儿要用)
- 启动mysqld_safe,修改默认密码
mysqld_safe --user=mysql
# mysql客户端登陆,实施修改
mysql -uroot -p
# (输入刚才记录的root账号的初始密码)
mysql> alter user 'root'@'localhost' identified by "新密码";
mysql> create user root@'%' identified by '123456';
Query OK, 0 rows affected (0.08 sec)
mysql> grant all privileges on *.* to root@'%';
Query OK, 0 rows affected (0.04 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
mysql> exit;
Bye;
- 停掉mysqld_safe
ps -ef | grep mysqld
- 注意要kill mysql 账号启动的mysql进程,kill root账号启动没有用
kill 1234
准备正常启动mysql 8
- mysql 8 不允许root账号命令行启动mysqld,所以要安装成服务
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
# 以服务方式启动
service mysql start
# 将mysql加入系统启动
chkconfig --add mysql
chkconfig mysql on
- 用新的root密码登陆
mysql -uroot -p
(输入新密码)
安装成功!
创建数据库篇
创建数据库时,提示1064错误
mysql> create database "testdb";
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '"testdb"' at line 1
原因是 mysql 的数据库名要用 反单引号包含,修改后成功
mysql> create database `testdb`;
Query OK, 1 row affected (0.04 sec)
连接数据库篇
- 用低版本的客户端如 navicat 连接mysql 8 ,提示如下:
ERROR: Client does not support authentication protocol requested by server; consider upgrading MySQL client
修改如下:
mysql> ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '(你的密码)';
Query OK, 0 rows affected (0.07 sec)
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '(你的密码)';
Query OK, 0 rows affected (0.08 sec)
- 打开root账号远程登陆
use mysql;
GRANT ALL PRIVILEGES ON *.* TO 'itoffice'@'%' IDENTIFIED BY 'itoffice' WITH GRANT OPTION;
#(第一个itoffice表示用户名,%表示所有的电脑都可以连接,也可以设置某个ip地址运行连接,第二个itoffice表示密码)
select host,user from user;
+-----------+---------------+
| host | user |
+-----------+---------------+
| % | httpwww |
| % | root |
| localhost | mysql.session |
| localhost | mysql.sys |
| localhost | root |
+-----------+---------------+
5 rows in set (0.00 sec)
flush privileges; # 执行 flush privileges;命令立即生效