CentOS7下mysql安装
卸载系统自带的Mariadb
rpm -qa|grep mariadb
mariadb-libs-5.5.44-2.el7.centos.x86_64
rpm -e --nodeps mariadb-libs-5.5.44-2.el7.centos.x86_64
检查mysql是否存在
rpm -qa | grep mysql
下载MySQL压缩包mysql-5.7.27-linux-glibc2.12-x86_64.tar
解压并改名为mysql,在此文件夹下新建data目录
tar -zxvf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
mv mysql-5.7.22-linux-glibc2.12-x86_64 mysql
cd mysql
mkdir data
在etc下新建配置文件my.cnf,并在该文件内添加以下配置
vi /etc/my.cnf
[mysql]
#设置mysql客户端默认字符集
default-character-set=utf8mb4
[mysqld]
skip-name-resolve
#设置3306端口
port = 3306
# 设置mysql的安装目录
basedir=/usr/local/mysql/mysql-5.7.27-linux-glibc2.12-x86_64/
# 设置mysql数据库的数据的存放目录
datadir=/usr/local/mysql/mysql-5.7.27-linux-glibc2.12-x86_64/data
# 允许最大连接数
max_connections=200
# 服务端使用的字符集默认为8比特编码的latin1字符集
character-set-server=utf8mb4
# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB
lower_case_table_names=1
max_allowed_packet=16M
log-error = /usr/local/mysql/mysql-5.7.27-linux-glibc2.12-x86_64/data/error.log
pid-file = /usr/local/mysql/mysql-5.7.27-linux-glibc2.12-x86_64/data/mysql.pid
user = root
tmpdir = /tmp
socket = /tmp/mysql.sock
安装和初始化
cd /apps/mysql
bin/mysql_install_db --user=root --basedir=/apps/mysql --datadir=/apps/mysql/data
cp ./support-files/mysql.server /etc/init.d/mysqld
chown 777 /etc/my.cnf
chmod +x /etc/init.d/mysqld
环境变量
在/etc/profile中添加:export PATH=$PATH:/usr/local/mysql/bin
使配置生效source /etc/profile
启动服务
/etc/init.d/mysqld start
Starting MySQL. SUCCESS!
#重启命令
service mysqld restart
查看端口运行情况(3306)
netstat -ntlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 1/systemd
tcp 0 0 192.168.122.1:53 0.0.0.0:* LISTEN 1326/dnsmasq
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 983/sshd
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 986/cupsd
tcp6 0 0 :::111 :::* LISTEN 1/systemd
tcp6 0 0 :::22 :::* LISTEN 983/sshd
tcp6 0 0 ::1:631 :::* LISTEN 986/cupsd
tcp6 0 0 :::3306 :::* LISTEN 16421/mysqld
登录MySQL
查看生成的初始密码
cat /root/.mysql_secret
# Password set for user 'root@localhost' at 2018-07-06 10:36:35
#Lws4rXsnhbA
密码登录(可能会报错)
mysql -u root -p
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
#如果登录成功,显示成这样
mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.22 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
安装完mysql 之后,登陆以后,不管运行任何命令,总是提示这个
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
step 1: SET PASSWORD = PASSWORD('your new password');
step 2: ALTER USER 'root'@'localhost' PASSWORD EXPIRE NEVER;
step 3: flush privileges;
完成以上三步退出再登,使用新设置的密码就行了,后两部原样设置即可
mysql> SET PASSWORD = PASSWORD('******');
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> ALTER USER 'root'@'localhost' PASSWORD EXPIRE NEVER;
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
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> show databases
-> ;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
mysql> exit
Bye
开放远程登录
update mysql.user set host='%' where user='root' and host='localhost';
quit;
service mysqld restart