作者:Chloemw | 2023-01-16 16:58
项目需要就在现有的服务器上面重新安装了个mysql服务器,还挺费劲儿呢,因为之前都是在我的笔记本上面试验的,它的系统是Ubuntu的,什么路径啊,启动方式啊。都不一样所以这次还是让我纠结了一把:
下面把我安装过程中遇到的问题给传上来,
首先rpm格式安装就不多说了,主要是mysql的配置文件在:/etc/my.cnf这里,需要修改:
复制代码 代码如下:
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Default to using old password format for compatibility with mysql 3.x
# clients (those using the mysqlclient10 compatibility package).
old_passwords=1 ← 找到这一行,在这一行的下面添加新的规则,让MySQL的默认编码为UTF-8
default-character-set = utf8 ← 添加这一行
然后在配置文件的文尾填加如下语句:
[mysql]
default-character-set = utf8
然后我就直接启动mysql了,还以为是Ubuntu的那样呢,安装后自动启动,可是centos不一样,服务没有启动所以就抱错了。
复制代码 代码如下:
[root@fsailing1 init.d]# mysql -u root
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
这个错误之前遇到过,就是因为错误或者其他的什么原因,mysql服务器没有启动造成的。
接着启动mysql服务:
复制代码 代码如下:
root@fsailing1 init.d]# /etc/rc.d/init.d/mysqld start
启动 MySQL: [确定]
[root@fsailing1 init.d]# ps -ef |grep mysql
root 1949 1 0 22:21 pts/1 00:00:00 /bin/sh /usr/bin/mysqld_safe --datadir=/var/lib/mysql --socket=/var/lib/mysql/mysql.sock --log-error=/var/log/mysqld.log --pid-file=/var/run/mysqld/mysqld.pid --user=mysql
mysql 2002 1949 1 22:21 pts/1 00:00:00 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --user=mysql --pid-file=/var/run/mysqld/mysqld.pid --skip-external-locking --socket=/var/lib/mysql/mysql.sock
root 2020 1101 0 22:21 pts/1 00:00:00 grep mysql
启动方式有很多种:service方式service mysqld start 和安全方式:/usr/bin/mysqld_safe &
通过启动服务的后缀我们可以清楚的看到,数据库放在那里,错误日志在哪里,
完事之后,要看看字符集的问题(这个问题至今我还没有搞清楚呢)。是否匹配:
复制代码 代码如下:
mysql> show variables like 'character%';
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | latin1 |
| character_set_connection | latin1 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | latin1 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)
这种情况是不行的,我们只是改变了服务器的字符集,没有改变客户端的字符集。
修改my.cnf文件后,重启mysql服务器后得知:
复制代码 代码如下:
mysql> show variables like 'character%';
+--------------------------+----------------------------+
| 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> select host, user ,password from user;
+-----------+------+----------+
| host | user | password |
+-----------+------+----------+
| localhost | root | |
| fsailing1 | root | |
| 127.0.0.1 | root | |
| localhost | | |
| fsailing1 | | |
+-----------+------+----------+
5 rows in set (0.00 sec)
这里用户和密码有的是空的,怪不得不需要任何验证都能够登录到服务器上去,这是很不安全的。所以要删除这些不安全的用户
复制代码 代码如下:
mysql> delete from user where user='';
Query OK, 2 rows affected (0.00 sec)
mysql> select host, user ,password from user;
+-----------+------+----------+
| host | user | password |
+-----------+------+----------+
| localhost | root | |
| fsailing1 | root | |
| 127.0.0.1 | root | |
+-----------+------+----------+
3 rows in set (0.00 sec)
然后是设置现有的用户密码:这里可以update,set都行。
复制代码 代码如下:
mysql> update user set password='123' where host='localhost';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select host, user ,password from user;
+-----------+------+----------+
| host | user | password |
+-----------+------+----------+
| localhost | root | 123 |
| fsailing1 | root | |
| 127.0.0.1 | root | |
+-----------+------+----------+
3 rows in set (0.00 sec)
这里我一看傻眼了,没有经过md5码加密呢,算了这里还只能用set进行设置密码呢。
复制代码 代码如下:
mysql> set password for root@localhost=password('123');
Query OK, 0 rows affected (0.00 sec)
mysql> select host, user ,password from user;
+-----------+------+------------------+
| host | user | password |
+-----------+------+------------------+
| localhost | root | 773359240eb9a1d9 |
| fsailing1 | root | |
| 127.0.0.1 | root | |
+-----------+------+------------------+
3 rows in set (0.00 sec)
到这里该做的基本上就算是完事了。